Class: Commander::Command
- Defined in:
- lib/commander/command.rb
Defined Under Namespace
Classes: Options
Constant Summary
Constants included from Patches::ValidateInputs
Patches::ValidateInputs::PatchEnabled
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#examples ⇒ Object
Returns the value of attribute examples.
-
#hidden ⇒ Object
(also: #sub_command)
Returns the value of attribute hidden.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#proxy_options ⇒ Object
Returns the value of attribute proxy_options.
-
#sub_command_group ⇒ Object
Returns the value of attribute sub_command_group.
-
#summary ⇒ Object
Returns the value of attribute summary.
-
#syntax ⇒ Object
Returns the value of attribute syntax.
Attributes included from Patches::PrioritySort
Instance Method Summary collapse
-
#call(args = []) ⇒ Object
Call the commands when_called block with args.
- #configure_sub_command(runner) ⇒ Object
-
#example(description, command) ⇒ Object
Add a usage example for this command.
-
#initialize(name) ⇒ Command
constructor
Initialize new command with specified name.
- #inspect ⇒ Object
- #option(*args, &block) ⇒ Object
-
#option_proc(switches) ⇒ Object
Option proxy proc used when a block is not explicitly passed via the #option method.
-
#parse_options_and_call_procs(*args) ⇒ Object
Parses options and calls associated procs, returning the arguments remaining.
-
#proxy_option_struct ⇒ Object
Creates an Options instance populated with the option values collected by the #option_proc.
-
#run(*args) ⇒ Object
Run the command with args.
-
#sub_command_group? ⇒ Boolean
Handles displaying subcommand help.
-
#when_called(*args, &block) ⇒ Object
(also: #action)
Handle execution of command.
Methods included from Patches::PrioritySort
Constructor Details
#initialize(name) ⇒ Command
Initialize new command with specified name.
56 57 58 59 |
# File 'lib/commander/command.rb', line 56 def initialize(name) @name, @examples, @when_called = name.to_s, [], [] , = [], [] end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
19 20 21 |
# File 'lib/commander/command.rb', line 19 def description @description end |
#examples ⇒ Object
Returns the value of attribute examples.
19 20 21 |
# File 'lib/commander/command.rb', line 19 def examples @examples end |
#hidden ⇒ Object Also known as: sub_command
Returns the value of attribute hidden.
20 21 22 |
# File 'lib/commander/command.rb', line 20 def hidden @hidden end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/commander/command.rb', line 19 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
20 21 22 |
# File 'lib/commander/command.rb', line 20 def end |
#proxy_options ⇒ Object
Returns the value of attribute proxy_options.
20 21 22 |
# File 'lib/commander/command.rb', line 20 def end |
#sub_command_group ⇒ Object
Returns the value of attribute sub_command_group.
21 22 23 |
# File 'lib/commander/command.rb', line 21 def sub_command_group @sub_command_group end |
#summary ⇒ Object
Returns the value of attribute summary.
20 21 22 |
# File 'lib/commander/command.rb', line 20 def summary @summary end |
#syntax ⇒ Object
Returns the value of attribute syntax.
19 20 21 |
# File 'lib/commander/command.rb', line 19 def syntax @syntax end |
Instance Method Details
#call(args = []) ⇒ Object
Call the commands when_called block with args.
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/commander/command.rb', line 226 def call(args = []) callee = @when_called.dup object = callee.shift meth = callee.shift || :call = proxy_option_struct case object when Proc then object.call(args, ) when Class then meth != :call ? object.new.send(meth, args, ) : object.new(args, ) else object.send(meth, args, ) if object end end |
#configure_sub_command(runner) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/commander/command.rb', line 178 def configure_sub_command(runner) @sub_command_group = true if @when_called.empty? action do |args, opts| unless args.empty? raise Commander::Runner::InvalidCommandError, "unrecognized subcommand '#{args[0]}'" end runner.command('help').run(ARGV[0]) end end end |
#example(description, command) ⇒ Object
Add a usage example for this command.
Usage examples are later displayed in help documentation created by the help formatters.
Examples
command :something do |c|
c.example "Should do something", "my_command something"
end
74 75 76 |
# File 'lib/commander/command.rb', line 74 def example(description, command) @examples << [description, command] end |
#inspect ⇒ Object
260 261 262 |
# File 'lib/commander/command.rb', line 260 def inspect "<Commander::Command:#{name}>" end |
#option(*args, &block) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/commander/command.rb', line 127 def option(*args, &block) switches, description = Runner.separate_switches_from_description(*args) proc = block || option_proc(switches) << { args: args, proc: proc, switches: switches, description: description, } end |
#option_proc(switches) ⇒ Object
Option proxy proc used when a block is not explicitly passed via the #option method. This allows commander to auto-populate and work with option values.
256 257 258 |
# File 'lib/commander/command.rb', line 256 def option_proc(switches) ->(value) { << [Runner.switch_to_sym(switches.last), value] } end |
#parse_options_and_call_procs(*args) ⇒ Object
Parses options and calls associated procs, returning the arguments remaining.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/commander/command.rb', line 208 def (*args) opt = .each_with_object(OptionParser.new) do |option, opts| opts.on(*option[:args], &option[:proc]) opts end default_opt = .each_with_object([]) do |h, arr| if h.key?(:default) arr.push(h[:switches][0].split[0]) arr.push(h[:default].to_s) end end opt.parse! default_opt opt.parse! args end |
#proxy_option_struct ⇒ Object
Creates an Options instance populated with the option values collected by the #option_proc.
242 243 244 245 246 247 248 249 |
# File 'lib/commander/command.rb', line 242 def proxy_option_struct .each_with_object(Options.new) do |(option, value), | # options that are present will evaluate to true value = true if value.nil? .__send__ :"#{option}=", value end end |
#run(*args) ⇒ Object
Run the command with args.
-
parses options, call option blocks
-
invokes when_called proc
198 199 200 |
# File 'lib/commander/command.rb', line 198 def run(*args) call (*args) end |
#sub_command_group? ⇒ Boolean
Handles displaying subcommand help. By default it will set the action to display the subcommand if the action hasn’t already been set
169 170 171 |
# File 'lib/commander/command.rb', line 169 def sub_command_group? !!@sub_command_group end |
#when_called(*args, &block) ⇒ Object Also known as: action
Handle execution of command. The handler may be a class, object, or block (see examples below).
Examples
# Simple block handling
c.when_called do |args, |
# do something
end
# Create inst of Something and pass args / options
c.when_called MyLib::Command::Something
# Create inst of Something and use arbitrary method
c.when_called MyLib::Command::Something, :some_method
# Pass an object to handle callback (requires method symbol)
c.when_called SomeObject, :some_method
159 160 161 162 |
# File 'lib/commander/command.rb', line 159 def when_called(*args, &block) fail ArgumentError, 'must pass an object, class, or block.' if args.empty? && !block @when_called = block ? [block] : args end |