Class: Bogo::Cli::Parser
- Inherits:
-
Object
- Object
- Bogo::Cli::Parser
- Defined in:
- lib/bogo-cli/parser.rb
Overview
Parser for CLI arguments
Defined Under Namespace
Classes: Command, Flag, OptionParser, OptionValues
Constant Summary collapse
- UNSET =
:__unset__
Instance Attribute Summary collapse
-
#generate_help ⇒ Boolean
Generate help content.
Class Method Summary collapse
-
.parse(help: true, &block) ⇒ Object
Parse a command setup block, process arguments and execute command if match found.
Instance Method Summary collapse
-
#command(name, &block) ⇒ Object
Add a new command.
-
#execute ⇒ Object
Execute command based on CLI arguments.
-
#generate ⇒ Hash<String,Hash<Parser,Command>>
Generate all parsers.
-
#initialize(name: nil) ⇒ Parser
constructor
Create a new parser.
-
#load(&block) ⇒ Object
Load a command configuration block.
-
#on(short, long, description, **options, &block) ⇒ Object
Add a new flag.
-
#run(&block) ⇒ Object
Register callable for command.
Constructor Details
#initialize(name: nil) ⇒ Parser
Create a new parser
315 316 317 318 |
# File 'lib/bogo-cli/parser.rb', line 315 def initialize(name: nil) name = self.class.root_name unless name @root = Command.new(name) end |
Instance Attribute Details
#generate_help ⇒ Boolean
297 298 299 |
# File 'lib/bogo-cli/parser.rb', line 297 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
304 305 306 307 308 309 |
# File 'lib/bogo-cli/parser.rb', line 304 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
323 324 325 |
# File 'lib/bogo-cli/parser.rb', line 323 def command(name, &block) @root.command(name, &block) end |
#execute ⇒ Object
Execute command based on CLI arguments
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/bogo-cli/parser.rb', line 355 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 |
#generate ⇒ Hash<String,Hash<Parser,Command>>
Generate all parsers
350 351 352 |
# File 'lib/bogo-cli/parser.rb', line 350 def generate @root.generate(add_help: generate_help) end |
#load(&block) ⇒ Object
Load a command configuration block
343 344 345 |
# File 'lib/bogo-cli/parser.rb', line 343 def load(&block) @root.load(&block) end |
#on(short, long, description, **options, &block) ⇒ Object
Add a new flag
333 334 335 |
# File 'lib/bogo-cli/parser.rb', line 333 def on(short, long, description, **, &block) @root.on(short, long, description, , &block) end |
#run(&block) ⇒ Object
Register callable for command
338 339 340 |
# File 'lib/bogo-cli/parser.rb', line 338 def run(&block) @root.run(&block) end |