Class: Bogo::Cli::Parser::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/bogo-cli/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Command

Create a new command

Parameters:

  • name (String, Symbol)

    name of command



112
113
114
115
116
117
118
# File 'lib/bogo-cli/parser.rb', line 112

def initialize(name)
  @name = name.to_sym
  @commands = []
  @flags = []
  @callable = nil
  @options = OptionValues.new
end

Instance Attribute Details

#callableProc (readonly)

Returns callable to be executed.

Returns:

  • (Proc)

    callable to be executed



102
103
104
# File 'lib/bogo-cli/parser.rb', line 102

def callable
  @callable
end

#commandsArray<Command> (readonly)

Returns list of subcommands.

Returns:

  • (Array<Command>)

    list of subcommands



96
97
98
# File 'lib/bogo-cli/parser.rb', line 96

def commands
  @commands
end

#flagsArray<Flag> (readonly)

Returns flags for command.

Returns:

  • (Array<Flag>)

    flags for command



100
101
102
# File 'lib/bogo-cli/parser.rb', line 100

def flags
  @flags
end

#nameString (readonly)

Returns name of command.

Returns:

  • (String)

    name of command



98
99
100
# File 'lib/bogo-cli/parser.rb', line 98

def name
  @name
end

#optionsOpenStruct (readonly)

Returns flag option values.

Returns:

  • (OpenStruct)

    flag option values



104
105
106
# File 'lib/bogo-cli/parser.rb', line 104

def options
  @options
end

#parserOptionParser (readonly)

Returns command parser.

Returns:



106
107
108
# File 'lib/bogo-cli/parser.rb', line 106

def parser
  @parser
end

Instance Method Details

#command(name, &block) ⇒ Object

Add a new command

Parameters:

  • name (String, Symbol)

    name of command



153
154
155
156
157
# File 'lib/bogo-cli/parser.rb', line 153

def command(name, &block)
  Command.new(name).load(&block).tap do |c|
    @commands << c
  end
end

#description(v = UNSET) ⇒ String

Returns description of command.

Returns:

  • (String)

    description of command



160
161
162
163
# File 'lib/bogo-cli/parser.rb', line 160

def description(v=UNSET)
  @description = v unless v == UNSET
  @description
end

#generate(parents = [], add_help: true) ⇒ Hash<string,OptParser>

Generate command parsers

Parameters:

  • parents (Array<String,Symbol>) (defaults to: [])

    ancestors of command

Returns:

  • (Hash<string,OptParser>)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/bogo-cli/parser.rb', line 204

def generate(parents=[], add_help: true)
  Hash.new.tap { |cmds|
    @parser = OptionParser.new
    full_name = parents + [name]
    parser.program_name = full_name.join(' ')
    parser.banner = description unless
      description == UNSET
    if add_help
      parser.on('-h', '--help', "Display help information") do
        $stderr.puts parser.help
        exit
      end
    end
    flags.each { |f|
      if !f.boolean? && f.long_name.end_with?('=')
        short = "-#{f.short_name}" if f.short_name
        long = "--#{f.long_name[0, f.long_name.size - 1]} VALUE"
      else
        short = "-#{f.short_name}" if f.short_name
        long = "--#{f.long_name}"
      end
      parser.on(short, long, f.description, &f.callable)
    }

    commands.map { |c|
      c.generate(full_name, add_help: add_help)
    }.inject(cmds) { |memo, list|
      memo.merge!(list)
    }

    parser.subcommands = commands

    cmds[full_name.join(' ')] = self
  }
end

#helpString

Returns help output.

Returns:

  • (String)

    help output



177
178
179
# File 'lib/bogo-cli/parser.rb', line 177

def help
  parser.help
end

#load(&block) ⇒ Object

Load a command configuration block



171
172
173
174
# File 'lib/bogo-cli/parser.rb', line 171

def load(&block)
  instance_exec(&block)
  self
end

#on(short, long, description = UNSET, opts = {}, &block) ⇒ Object

Add a new flag

Parameters:

  • short (String, Symbol)

    short flag

  • long (String, Symbol)

    long flag

  • description (String) (defaults to: UNSET)

    description of flag

  • default (String)

    default flag value



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bogo-cli/parser.rb', line 126

def on(short, long, description=UNSET, opts={}, &block)
  if short.to_s.size > 1
    if description == UNSET
      description = long
      long = short
      short = nil
    elsif description.is_a?(Hash)
      opts = description
      description = long
      long = short
      short = nil
    end
  end
  Flag.new(
    short_name: short,
    long_name: long,
    description: description,
    default: opts[:default],
    callable: block,
  ).tap do |f|
    @flags << f
  end
end

#parse(arguments) ⇒ OpenStruct, Array<String>

Parse the arguments

Parameters:

  • arguments (Array<String>)

    CLI arguments

Returns:

  • (OpenStruct, Array<String>)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bogo-cli/parser.rb', line 185

def parse(arguments)
  raise "Must call #generate before #parse" if
    parser.nil?
  flags.each do |f|
    next if f.default.nil?
    options.set_default(f.option_name, f.default)
  end
  init = OpenStruct.new
  parser.parse!(arguments, into: init)
  init.each_pair do |k, v|
    options.assign(k, v)
  end
  [options, arguments]
end

#run(&block) ⇒ Object

Register callable for command



166
167
168
# File 'lib/bogo-cli/parser.rb', line 166

def run(&block)
  @callable = block
end

#to_hashObject



240
241
242
# File 'lib/bogo-cli/parser.rb', line 240

def to_hash
  options.to_h
end