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.
Methods
Public Class
Public Instance
Attributes
| banner | [RW] |
A usage banner for the command or subcommands. |
| command_path | [RW] |
An array of command names that represent a path to the current command. Empty for the root command. |
| desc | [RW] |
A description for the command |
| num_args | [RW] |
The number of arguments the run block will accept. Should be either an integer or a range of integers. |
| option_key | [RW] |
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. |
| option_parser | [RW] |
The option parser for the current command. May be nil, in which case the default option parser is used. |
| run_block | [RW] |
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. |
| subcommands | [R] |
A hash of subcommands for the command. Keys are subcommand name strings. |
Public Class methods
# File lib/rodish/command.rb 48 def initialize(command_path) 49 @command_path = command_path 50 @command_name = _command_name(command_path) 51 @subcommands = {} 52 @num_args = 0 53 end
Public Instance methods
Yield each banner string (if any) to the block.
# File lib/rodish/command.rb 117 def each_banner 118 yield banner if banner 119 nil 120 end
This yields the current command and all subcommands, recursively.
# File lib/rodish/command.rb 111 def each_subcommand(names = [].freeze, &block) 112 yield names, self 113 _each_subcommand(names, @subcommands, &block) 114 end
Freeze all subcommands and option parsers in addition to the command itself.
# File lib/rodish/command.rb 57 def freeze 58 @subcommands.each_value(&:freeze) 59 @subcommands.freeze 60 @option_parser.freeze 61 super 62 end
Return a help string for the command.
# File lib/rodish/command.rb 65 def help 66 help_lines.join("\n") 67 end
Return an array of help strings for the command.
# File lib/rodish/command.rb 70 def help_lines 71 output = [] 72 help_order.each do |type| 73 send(:"_help_#{type}", output) 74 end 75 output 76 end
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.
# File lib/rodish/command.rb 87 def process(context, options, argv) 88 process_command_options(context, options, argv) 89 90 arg = argv[0] 91 if argv && @subcommands[arg] 92 process_subcommand(@subcommands, context, options, argv) 93 elsif run_block 94 if valid_args?(argv) 95 if @num_args.is_a?(Integer) 96 context.instance_exec(*argv, options, self, &run_block) 97 else 98 context.instance_exec(argv, options, self, &run_block) 99 end 100 else 101 raise_invalid_args_failure(argv) 102 end 103 else 104 process_command_failure(arg, @subcommands, "") 105 end 106 rescue ::OptionParser::InvalidOption => e 107 raise_failure(e.message) 108 end
Process options for the command using the option key and parser.
# File lib/rodish/command.rb 79 def process_command_options(context, options, argv) 80 process_options(argv, options, @option_key, @option_parser) 81 end
Raise a CommandFailure with the given error and the given option parsers.
# File lib/rodish/command.rb 124 def raise_failure(message) 125 raise CommandFailure.new(message, self) 126 end
Returns a Command instance for the named subcommand. This will autoload the subcommand if not already loaded.
# File lib/rodish/command.rb 130 def subcommand(name) 131 _subcommand(@subcommands, name) 132 end