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 ⇒ Object
Returns all command ancestors of this command.
-
#command_path ⇒ Object
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(*arguments) ⇒ Object
Computes the full command line of this command, which takes parent commands into account.
-
#invoke(arguments = [], options = []) ⇒ Object
Parses the argument array.
-
#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
The options this command can take.
-
#parse_options_in!(arguments) ⇒ Object
Parses the argument array using this command’s set of options.
-
#parse_subcommands_in!(arguments) ⇒ Object
Looks for this command’s subcommands in the argument array.
-
#root ⇒ Object
Returns the root of the command hierarchy.
-
#root? ⇒ Boolean
True if this is a top-level command.
-
#run(*arguments) ⇒ Object
Invokes this command with a fresh set of option values.
-
#subcommands ⇒ Array
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.
60 61 62 63 |
# File 'lib/acclaim/command/dsl.rb', line 60 def action(&block) @action = block if block.respond_to? :call @action end |
#command_ancestors ⇒ Object
Returns all command ancestors of this command.
118 119 120 |
# File 'lib/acclaim/command/dsl.rb', line 118 def command_ancestors ancestors - Acclaim::Command.ancestors end |
#command_path ⇒ Object
Returns the sequence of commands from #root that leads to this command.
128 129 130 |
# File 'lib/acclaim/command/dsl.rb', line 128 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.
106 107 108 |
# File 'lib/acclaim/command/dsl.rb', line 106 def execute(, arguments) @action.call , arguments if @action.respond_to? :call end |
#full_line(*arguments) ⇒ 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"
147 148 149 150 151 152 |
# File 'lib/acclaim/command/dsl.rb', line 147 def full_line(*arguments) = arguments.extract_ribbon! command_path.tap do |path| path.shift unless .include_root? end.map(&:line).join ' ' end |
#invoke(arguments = [], options = []) ⇒ 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.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/acclaim/command/dsl.rb', line 92 def invoke(arguments = [], = []) = + self. subcommand = parse_subcommands_in! arguments if subcommand.nil? = 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.
25 26 27 28 |
# File 'lib/acclaim/command/dsl.rb', line 25 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.
47 48 49 |
# File 'lib/acclaim/command/dsl.rb', line 47 def option(*arguments, &block) << Option.new(*arguments, &block) end |
#options ⇒ Array
The options this command can take.
40 41 42 |
# File 'lib/acclaim/command/dsl.rb', line 40 def @options ||= [] end |
#parse_options_in!(arguments) ⇒ Object
Parses the argument array using this command’s set of options.
68 69 70 |
# File 'lib/acclaim/command/dsl.rb', line 68 def (arguments) Option::Parser.new(arguments, ).parse! end |
#parse_subcommands_in!(arguments) ⇒ Object
Looks for this command’s subcommands in the argument array.
73 74 75 |
# File 'lib/acclaim/command/dsl.rb', line 73 def parse_subcommands_in!(arguments) Command::Parser.new(arguments, subcommands).parse! end |
#root ⇒ Object
Returns the root of the command hierarchy.
123 124 125 |
# File 'lib/acclaim/command/dsl.rb', line 123 def root command_ancestors.last end |
#root? ⇒ Boolean
True if this is a top-level command.
113 114 115 |
# File 'lib/acclaim/command/dsl.rb', line 113 def root? superclass == Acclaim::Command end |
#run(*arguments) ⇒ Object
Invokes this command with a fresh set of option values.
78 79 80 81 82 |
# File 'lib/acclaim/command/dsl.rb', line 78 def run(*arguments) invoke arguments rescue Option::Parser::Error => error $stderr.puts error. end |
#subcommands ⇒ Array
Commands which may be given to this command.
33 34 35 |
# File 'lib/acclaim/command/dsl.rb', line 33 def subcommands @subcommands ||= [] end |