Class: Bogo::Cli::Parser

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

Overview

Parser for CLI arguments

Defined Under Namespace

Classes: Command, Flag, OptionParser, OptionValues

Constant Summary collapse

UNSET =

Returns represent unset value.

Returns:

  • (Symbol)

    represent unset value

:__unset__

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil) ⇒ Parser

Create a new parser

Parameters:

  • name (String, Symbol) (defaults to: nil)

    name of root command



318
319
320
321
# File 'lib/bogo-cli/parser.rb', line 318

def initialize(name: nil)
  name = self.class.root_name unless name
  @root = Command.new(name)
end

Instance Attribute Details

#generate_helpBoolean

Returns generate help content.

Returns:

  • (Boolean)

    generate help content



300
301
302
# File 'lib/bogo-cli/parser.rb', line 300

def generate_help
  @generate_help
end

Class Method Details

.parse(help: true, &block) ⇒ Object

Parse a command setup block, process arguments and execute command if match found

Parameters:

  • help (Boolean) (defaults to: true)

    generate help content

Returns:

  • (Object)

    result



307
308
309
310
311
312
# File 'lib/bogo-cli/parser.rb', line 307

def self.parse(help: true, &block)
  parser = self.new
  parser.generate_help = !!help
  parser.load(&block)
  parser.execute
end

Instance Method Details

#command(name, &block) ⇒ Object

Add a new command

Parameters:

  • name (String, Symbol)

    name of command



326
327
328
# File 'lib/bogo-cli/parser.rb', line 326

def command(name, &block)
  @root.command(name, &block)
end

#executeObject

Execute command based on CLI arguments



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/bogo-cli/parser.rb', line 358

def execute
  cmds = @root.generate
  base_args = arguments
  line = base_args.join(' ')
  cmd_key = cmds.keys.find_all { |k|
    line.start_with?(k)
  }.sort_by(&:size).last
  if cmd_key.nil?
    return cmds[@root.name].parser
  end
  base_args = base_args.slice(cmd_key.split(' ').size, base_args.size)
  a = cmds[cmd_key].parse(base_args)
  return cmds[cmd_key] unless cmds[cmd_key].callable
  cmds[cmd_key].callable.call(*a)
  exit 0
end

#generateHash<String,Hash<Parser,Command>>

Generate all parsers

Returns:



353
354
355
# File 'lib/bogo-cli/parser.rb', line 353

def generate
  @root.generate(add_help: generate_help)
end

#load(&block) ⇒ Object

Load a command configuration block



346
347
348
# File 'lib/bogo-cli/parser.rb', line 346

def load(&block)
  @root.load(&block)
end

#on(short, long, description, **options, &block) ⇒ Object

Add a new flag

Parameters:

  • short (String, Symbol)

    short flag

  • long (String, Symbol)

    long flag

  • description (String)

    description of flag

  • default (String)

    default flag value



336
337
338
# File 'lib/bogo-cli/parser.rb', line 336

def on(short, long, description, **options, &block)
  @root.on(short, long, description, options, &block)
end

#run(&block) ⇒ Object

Register callable for command



341
342
343
# File 'lib/bogo-cli/parser.rb', line 341

def run(&block)
  @root.run(&block)
end