Module: Acclaim::Command::ClassMethods

Defined in:
lib/acclaim/command.rb

Overview

Module containing the class methods every command class should inherit.

Instance Method Summary collapse

Instance Method Details

#action(&block) ⇒ Object Also known as: when_called

The block which is executed when this command is called. It is given 2 parameters; the first is an Ribbon instance which can be queried for settings information; the second is the remaining command line.



76
77
78
# File 'lib/acclaim/command.rb', line 76

def action(&block)
  @action = block
end

#command_ancestorsObject

Returns all command ancestors of this command.



145
146
147
# File 'lib/acclaim/command.rb', line 145

def command_ancestors
  ancestors - Acclaim::Command.ancestors
end

#command_pathObject

Returns the sequence of commands from #root that leads to this command.



155
156
157
# File 'lib/acclaim/command.rb', line 155

def command_path
  command_ancestors.reverse
end

#execute(opts, args) ⇒ Object Also known as: call

Calls this command’s action block with the given option values and arguments.



132
133
134
# File 'lib/acclaim/command.rb', line 132

def execute(opts, args)
  @action.call opts, args if @action
end

#full_line(*args) ⇒ Object

Computes the full command line of this command, which takes parent commands into account.

class Command < Acclaim::Command
  class Do < Command
    class Something < Do
    end
  end
end

Command::Do::Something.full_line
 => "do something"

Command::Do::Something.full_line include_root: true
 => "command do something"


174
175
176
177
178
179
# File 'lib/acclaim/command.rb', line 174

def full_line(*args)
  options = args.extract_ribbon!
  command_path.tap do |path|
    path.shift unless options.include_root?
  end.map(&:line).join ' '
end

#help(*args) ⇒ Object

Adds help subcommand and options to this command.



84
85
86
# File 'lib/acclaim/command.rb', line 84

def help(*args)
  Help.create(self, *args)
end

#invoke(args = [], opts = {}) ⇒ Object

Parses the argument array. The argument array will be searched for subcommands; if one is found, it will be invoked, if not, this command will be executed. A subcommand may be anywhere in the array as long as it is before an argument separator, which is tipically a double dash (--) and may be omitted.

All argument separators will be deleted from the argument array before a command is executed.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/acclaim/command.rb', line 118

def invoke(args = [], opts = {})
  opts = Ribbon.wrap opts
  opts.merge! parse_options!(args)
  handle_special_options! opts, args
  if subcommand = parse_subcommands!(args)
    subcommand.invoke args, opts
  else
    delete_argument_separators_in! args
    execute opts, args
  end
end

#line(*args) ⇒ Object

String which calls this command.



50
51
52
53
# File 'lib/acclaim/command.rb', line 50

def line(*args)
  @line = args.first unless args.empty?
  @line ||= (name.gsub(/^.*::/, '').downcase rescue nil)
end

#option(*args, &block) ⇒ Object Also known as: opt

Adds an option to this command.



66
67
68
# File 'lib/acclaim/command.rb', line 66

def option(*args, &block)
  options << Option.new(*args, &block)
end

#optionsObject

The options this command can take.



61
62
63
# File 'lib/acclaim/command.rb', line 61

def options
  @options ||= []
end

#parse_options!(args) ⇒ Object

Parses the argument array using this command’s set of options.



94
95
96
# File 'lib/acclaim/command.rb', line 94

def parse_options!(args)
  Option::Parser.new(args, options).parse!
end

#parse_subcommands!(args) ⇒ Object

Looks for this command’s subcommands in the argument array.



99
100
101
# File 'lib/acclaim/command.rb', line 99

def parse_subcommands!(args)
  Command::Parser.new(args, subcommands).parse!
end

#rootObject

Returns the root of the command hierarchy.



150
151
152
# File 'lib/acclaim/command.rb', line 150

def root
  command_ancestors.last
end

#root?Boolean

True if this is a top-level command.

Returns:

  • (Boolean)


140
141
142
# File 'lib/acclaim/command.rb', line 140

def root?
  superclass == Acclaim::Command
end

#run(*args) ⇒ Object

Invokes this command with a fresh set of option values.



104
105
106
107
108
# File 'lib/acclaim/command.rb', line 104

def run(*args)
  invoke args
rescue Option::Parser::Error => error
  $stderr.puts error.message
end

#subcommandsObject

Commands which may be given to this command.



56
57
58
# File 'lib/acclaim/command.rb', line 56

def subcommands
  @subcommands ||= []
end

#version(*args) ⇒ Object

Adds help subcommand and options to this command.



89
90
91
# File 'lib/acclaim/command.rb', line 89

def version(*args)
  Version.create(self, *args)
end