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



115
116
117
118
119
120
121
# File 'lib/bogo-cli/parser.rb', line 115

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



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

def callable
  @callable
end

#commandsArray<Command> (readonly)

Returns list of subcommands.

Returns:

  • (Array<Command>)

    list of subcommands



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

def commands
  @commands
end

#flagsArray<Flag> (readonly)

Returns flags for command.

Returns:

  • (Array<Flag>)

    flags for command



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

def flags
  @flags
end

#nameString (readonly)

Returns name of command.

Returns:

  • (String)

    name of command



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

def name
  @name
end

#optionsOpenStruct (readonly)

Returns flag option values.

Returns:

  • (OpenStruct)

    flag option values



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

def options
  @options
end

#parserOptionParser (readonly)

Returns command parser.

Returns:



109
110
111
# File 'lib/bogo-cli/parser.rb', line 109

def parser
  @parser
end

Instance Method Details

#command(name, &block) ⇒ Object

Add a new command

Parameters:

  • name (String, Symbol)

    name of command



156
157
158
159
160
# File 'lib/bogo-cli/parser.rb', line 156

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



163
164
165
166
# File 'lib/bogo-cli/parser.rb', line 163

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>)


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
239
240
241
# File 'lib/bogo-cli/parser.rb', line 207

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



180
181
182
# File 'lib/bogo-cli/parser.rb', line 180

def help
  parser.help
end

#load(&block) ⇒ Object

Load a command configuration block



174
175
176
177
# File 'lib/bogo-cli/parser.rb', line 174

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



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

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>)


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

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



169
170
171
# File 'lib/bogo-cli/parser.rb', line 169

def run(&block)
  @callable = block
end

#to_hashObject



243
244
245
# File 'lib/bogo-cli/parser.rb', line 243

def to_hash
  options.to_h
end