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 =
Returns represent unset value.
:__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
Instance Attribute Details
#generate_help ⇒ Boolean
Returns 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
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
326 327 328 |
# File 'lib/bogo-cli/parser.rb', line 326 def command(name, &block) @root.command(name, &block) end |
#execute ⇒ Object
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 |
#generate ⇒ Hash<String,Hash<Parser,Command>>
Generate all parsers
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
336 337 338 |
# File 'lib/bogo-cli/parser.rb', line 336 def on(short, long, description, **, &block) @root.on(short, long, description, , &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 |