Class: Commander::Command

Inherits:
Object show all
Includes:
Patches::OptionDefaults, Patches::PrioritySort, Patches::ValidateInputs
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

Attributes included from Patches::PrioritySort

#priority

Instance Method Summary collapse

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, [], []
  @options, @proxy_options = [], []
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



19
20
21
# File 'lib/commander/command.rb', line 19

def description
  @description
end

#examplesObject

Returns the value of attribute examples.



19
20
21
# File 'lib/commander/command.rb', line 19

def examples
  @examples
end

#hiddenObject 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

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/commander/command.rb', line 19

def name
  @name
end

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/commander/command.rb', line 20

def options
  @options
end

#proxy_optionsObject

Returns the value of attribute proxy_options.



20
21
22
# File 'lib/commander/command.rb', line 20

def proxy_options
  @proxy_options
end

#sub_command_groupObject

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

#summaryObject

Returns the value of attribute summary.



20
21
22
# File 'lib/commander/command.rb', line 20

def summary
  @summary
end

#syntaxObject

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
  options = proxy_option_struct
  case object
  when Proc then object.call(args, options)
  when Class then meth != :call ? object.new.send(meth, args, options) : object.new(args, options)
  else object.send(meth, args, options) 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

#inspectObject



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)
  @options << {
    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) { proxy_options << [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 parse_options_and_call_procs(*args)
  opt = @options.each_with_object(OptionParser.new) do |option, opts|
    opts.on(*option[:args], &option[:proc])
    opts
  end
  default_opt = @options.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_structObject

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
  proxy_options.each_with_object(Options.new) do |(option, value), options|
    # options that are present will evaluate to true
    value = true if value.nil?
    options.__send__ :"#{option}=", value
    options
  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 parse_options_and_call_procs(*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

Returns:

  • (Boolean)


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, options|
   # 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