Module: Acclaim::Command::DSL
- Includes:
- Root
- Defined in:
- lib/acclaim/command/dsl.rb,
lib/acclaim/command/dsl/root.rb
Overview
Module containing the methods that make up the domain-specific language used to create commands.
Defined Under Namespace
Modules: Root
Instance Method Summary collapse
-
#action {|options, arguments| ... } ⇒ Proc?
(also: #when_called)
The block which is executed when this command is called.
-
#command_ancestors ⇒ Array<Acclaim::Command::DSL] this command's parent commands
Returns all command ancestors of this command.
-
#command_path ⇒ Array<Acclaim::Command::DSL>
Returns the sequence of commands from #root that leads to this command.
-
#execute(options, arguments = []) ⇒ Object
(also: #call)
Calls this command’s action block with the given option values and arguments.
-
#full_line(options = {}) ⇒ Object
Computes the full command line of this command, taking parent commands into account.
-
#invoke(arguments = []) ⇒ Object
Searches the given arguments for subcommands and invokes it.
-
#line(*arguments) ⇒ Object
The string used to invoke this command from the command line.
-
#option(*arguments, &block) ⇒ Object
(also: #opt)
Adds an option to this command.
-
#options ⇒ Array<Acclaim::Option>
The options this command can take.
-
#parse_options_in!(arguments) ⇒ Ribbon::Wrapper
Parses the given arguments using this command’s set of options.
-
#parse_subcommands_in!(arguments) ⇒ Acclaim::Command::DSL?
Searches the given arguments for one of this command’s subcommands.
-
#root ⇒ Acclaim::Command::DSL
Returns the root of the command hierarchy.
-
#root? ⇒ true, false
Whether this is a top-level command.
-
#run(*arguments) ⇒ Object
Invokes this command with the given arguments.
-
#subcommands ⇒ Array<Acclaim::Command::DSL>
Commands which may be given to this command.
Methods included from Root
Instance Method Details
#action {|options, arguments| ... } ⇒ Proc? Also known as: when_called
The block which is executed when this command is called.
71 72 73 74 |
# File 'lib/acclaim/command/dsl.rb', line 71 def action(&block) @action = block if block.respond_to? :call @action end |
#command_ancestors ⇒ Array<Acclaim::Command::DSL] this command's parent commands
Returns all command ancestors of this command.
144 145 146 |
# File 'lib/acclaim/command/dsl.rb', line 144 def command_ancestors ancestors - Acclaim::Command.ancestors end |
#command_path ⇒ Array<Acclaim::Command::DSL>
Returns the sequence of commands from #root that leads to this command.
166 167 168 |
# File 'lib/acclaim/command/dsl.rb', line 166 def command_path command_ancestors.reverse end |
#execute(options, arguments = []) ⇒ Object Also known as: call
Calls this command’s action block with the given option values and arguments.
133 134 135 136 137 |
# File 'lib/acclaim/command/dsl.rb', line 133 def execute(, arguments = []) action.instance_eval do call , arguments if respond_to? :call end end |
#full_line(options = {}) ⇒ Object
Computes the full command line of this command, taking parent commands into account.
190 191 192 193 194 195 |
# File 'lib/acclaim/command/dsl.rb', line 190 def full_line( = {}) = Ribbon.wrap command_path.tap do |path| path.shift unless .include_root? end.map(&:line).join ' ' end |
#invoke(arguments = []) ⇒ Object
Argument separators will be deleted prior to command execution.
Searches the given arguments for subcommands and invokes it. If none were found, executes this command instead.
The arguments will be parsed using all options from this command and its parents.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/acclaim/command/dsl.rb', line 116 def invoke(arguments = []) subcommand = parse_subcommands_in! arguments if subcommand.nil? = command_ancestors.collect(&:options).flatten = Option::Parser.new(arguments, ).parse! delete_argument_separators_in! arguments execute , arguments else subcommand.invoke arguments end end |
#line ⇒ String #line(string) ⇒ String
The string used to invoke this command from the command line.
36 37 38 39 |
# File 'lib/acclaim/command/dsl.rb', line 36 def line(*arguments) @line = arguments.first unless arguments.empty? @line ||= (name.gsub(/^.*::/, '').downcase if respond_to? :name) end |
#option(*arguments, &block) ⇒ Object Also known as: opt
Adds an option to this command.
58 59 60 |
# File 'lib/acclaim/command/dsl.rb', line 58 def option(*arguments, &block) << Option.new(*arguments, &block) end |
#options ⇒ Array<Acclaim::Option>
The options this command can take.
51 52 53 |
# File 'lib/acclaim/command/dsl.rb', line 51 def @options ||= [] end |
#parse_options_in!(arguments) ⇒ Ribbon::Wrapper
Parses the given arguments using this command’s set of options.
82 83 84 |
# File 'lib/acclaim/command/dsl.rb', line 82 def (arguments) Option::Parser.new(arguments, ).parse! end |
#parse_subcommands_in!(arguments) ⇒ Acclaim::Command::DSL?
Searches the given arguments for one of this command’s subcommands.
91 92 93 |
# File 'lib/acclaim/command/dsl.rb', line 91 def parse_subcommands_in!(arguments) Command::Parser.new(arguments, subcommands).parse! end |
#root ⇒ Acclaim::Command::DSL
Returns the root of the command hierarchy.
151 152 153 |
# File 'lib/acclaim/command/dsl.rb', line 151 def root command_ancestors.last end |
#root? ⇒ true, false
Whether this is a top-level command.
159 160 161 |
# File 'lib/acclaim/command/dsl.rb', line 159 def root? self == root end |
#run(*arguments) ⇒ Object
Invokes this command with the given arguments. Outputs parser error messages to the standard error stream.
101 102 103 104 105 |
# File 'lib/acclaim/command/dsl.rb', line 101 def run(*arguments) invoke arguments rescue Option::Parser::Error => error $stderr.puts error. end |
#subcommands ⇒ Array<Acclaim::Command::DSL>
Commands which may be given to this command.
44 45 46 |
# File 'lib/acclaim/command/dsl.rb', line 44 def subcommands @subcommands ||= [] end |