Class: Rodish::Command
- Inherits:
-
Object
- Object
- Rodish::Command
- Defined in:
- lib/rodish/command.rb
Overview
Rodish::Command is the main object in Rodish’s processing. It handles a single command, and may have one or more subcommands, forming a tree.
Rodish’s argv processing starts with the root command, processing options and deleting appropriately to subcommands, until the requested command or subcommand is located, which is then executed.
Instance Attribute Summary collapse
-
#banner ⇒ Object
A usage banner for the command or subcommands.
-
#command_path ⇒ Object
An array of command names that represent a path to the current command.
-
#desc ⇒ Object
A description for the command.
-
#num_args ⇒ Object
The number of arguments the run block will accept.
-
#option_key ⇒ Object
If set, places parsed options in a subhash of the options hash, keyed by the given value.
-
#option_parser ⇒ Object
The option parser for the current command.
-
#run_block ⇒ Object
The block to execute if this command is the requested subcommand.
-
#subcommands ⇒ Object
readonly
A hash of subcommands for the command.
Instance Method Summary collapse
-
#each_banner {|banner| ... } ⇒ Object
Yield each banner string (if any) to the block.
-
#each_subcommand(names = [].freeze) {|names, _self| ... } ⇒ Object
This yields the current command and all subcommands, recursively.
-
#freeze ⇒ Object
Freeze all subcommands and option parsers in addition to the command itself.
-
#help ⇒ Object
Return a help string for the command.
-
#help_lines ⇒ Object
Return an array of help strings for the command.
-
#initialize(command_path) ⇒ Command
constructor
A new instance of Command.
-
#process(context, options, argv) ⇒ Object
Process the current command.
-
#process_command_options(context, options, argv) ⇒ Object
Process options for the command using the option key and parser.
-
#raise_failure(message) ⇒ Object
Raise a CommandFailure with the given error and the given option parsers.
-
#subcommand(name) ⇒ Object
Returns a Command instance for the named subcommand.
Constructor Details
#initialize(command_path) ⇒ Command
Returns a new instance of Command.
48 49 50 51 52 53 |
# File 'lib/rodish/command.rb', line 48 def initialize(command_path) @command_path = command_path @command_name = _command_name(command_path) @subcommands = {} @num_args = 0 end |
Instance Attribute Details
#banner ⇒ Object
A usage banner for the command or subcommands.
46 47 48 |
# File 'lib/rodish/command.rb', line 46 def end |
#command_path ⇒ Object
An array of command names that represent a path to the current command. Empty for the root command.
27 28 29 |
# File 'lib/rodish/command.rb', line 27 def command_path @command_path end |
#desc ⇒ Object
A description for the command
43 44 45 |
# File 'lib/rodish/command.rb', line 43 def desc @desc end |
#num_args ⇒ Object
The number of arguments the run block will accept. Should be either an integer or a range of integers.
40 41 42 |
# File 'lib/rodish/command.rb', line 40 def num_args @num_args end |
#option_key ⇒ Object
If set, places parsed options in a subhash of the options hash, keyed by the given value. If nil, parsed options are placed directly in the options hash.
36 37 38 |
# File 'lib/rodish/command.rb', line 36 def option_key @option_key end |
#option_parser ⇒ Object
The option parser for the current command. May be nil, in which case the default option parser is used.
31 32 33 |
# File 'lib/rodish/command.rb', line 31 def option_parser @option_parser end |
#run_block ⇒ Object
The block to execute if this command is the requested subcommand. May be nil if this subcommand cannot be executed, and can only dispatch to subcommands.
23 24 25 |
# File 'lib/rodish/command.rb', line 23 def run_block @run_block end |
#subcommands ⇒ Object (readonly)
A hash of subcommands for the command. Keys are subcommand name strings.
18 19 20 |
# File 'lib/rodish/command.rb', line 18 def subcommands @subcommands end |
Instance Method Details
#each_banner {|banner| ... } ⇒ Object
Yield each banner string (if any) to the block.
117 118 119 120 |
# File 'lib/rodish/command.rb', line 117 def yield if nil end |
#each_subcommand(names = [].freeze) {|names, _self| ... } ⇒ Object
This yields the current command and all subcommands, recursively.
111 112 113 114 |
# File 'lib/rodish/command.rb', line 111 def each_subcommand(names = [].freeze, &block) yield names, self _each_subcommand(names, @subcommands, &block) end |
#freeze ⇒ Object
Freeze all subcommands and option parsers in addition to the command itself.
57 58 59 60 61 62 |
# File 'lib/rodish/command.rb', line 57 def freeze @subcommands.each_value(&:freeze) @subcommands.freeze @option_parser.freeze super end |
#help ⇒ Object
Return a help string for the command.
65 66 67 |
# File 'lib/rodish/command.rb', line 65 def help help_lines.join("\n") end |
#help_lines ⇒ Object
Return an array of help strings for the command.
70 71 72 73 74 75 76 |
# File 'lib/rodish/command.rb', line 70 def help_lines output = [] help_order.each do |type| send(:"_help_#{type}", output) end output end |
#process(context, options, argv) ⇒ Object
Process the current command. This first processes the options. After processing the options, it checks if the first argument in the remaining argv is a subcommand. If so, it dispatches to that subcommand. If not, it dispatches to the run block.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rodish/command.rb', line 87 def process(context, , argv) (context, , argv) arg = argv[0] if argv && @subcommands[arg] process_subcommand(@subcommands, context, , argv) elsif run_block if valid_args?(argv) if @num_args.is_a?(Integer) context.instance_exec(*argv, , self, &run_block) else context.instance_exec(argv, , self, &run_block) end else raise_invalid_args_failure(argv) end else process_command_failure(arg, @subcommands, "") end rescue ::OptionParser::ParseError => e raise_failure(e.) end |
#process_command_options(context, options, argv) ⇒ Object
Process options for the command using the option key and parser.
79 80 81 |
# File 'lib/rodish/command.rb', line 79 def (context, , argv) (argv, , @option_key, @option_parser) end |
#raise_failure(message) ⇒ Object
Raise a CommandFailure with the given error and the given option parsers.
124 125 126 |
# File 'lib/rodish/command.rb', line 124 def raise_failure() raise CommandFailure.new(, self) end |
#subcommand(name) ⇒ Object
Returns a Command instance for the named subcommand. This will autoload the subcommand if not already loaded.
130 131 132 |
# File 'lib/rodish/command.rb', line 130 def subcommand(name) _subcommand(@subcommands, name) end |