Class: Rodish::Command

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

A usage banner for the command or subcommands.



46
47
48
# File 'lib/rodish/command.rb', line 46

def banner
  @banner
end

#command_pathObject

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

#descObject

A description for the command



43
44
45
# File 'lib/rodish/command.rb', line 43

def desc
  @desc
end

#num_argsObject

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_keyObject

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_parserObject

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_blockObject

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

#subcommandsObject (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.

Yields:



117
118
119
120
# File 'lib/rodish/command.rb', line 117

def each_banner
  yield banner if banner
  nil
end

#each_subcommand(names = [].freeze) {|names, _self| ... } ⇒ Object

This yields the current command and all subcommands, recursively.

Yields:

  • (names, _self)

Yield Parameters:



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

#freezeObject

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

#helpObject

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_linesObject

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, options, argv)
  process_command_options(context, options, argv)

  arg = argv[0]
  if argv && @subcommands[arg]
    process_subcommand(@subcommands, context, options, argv)
  elsif run_block
    if valid_args?(argv)
      if @num_args.is_a?(Integer)
        context.instance_exec(*argv, options, self, &run_block)
      else
        context.instance_exec(argv, options, 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.message)
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 process_command_options(context, options, argv)
  process_options(argv, options, @option_key, @option_parser)
end

#raise_failure(message) ⇒ Object

Raise a CommandFailure with the given error and the given option parsers.

Raises:



124
125
126
# File 'lib/rodish/command.rb', line 124

def raise_failure(message)
  raise CommandFailure.new(message, 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