Class: Clamp::Command
- Inherits:
-
Object
- Object
- Clamp::Command
- Extended by:
- Help, Option::Declaration, Parameter::Declaration, Subcommand::Declaration
- Includes:
- Option::Parsing, Parameter::Parsing, Subcommand::Execution
- Defined in:
- lib/clamp/command.rb
Overview
Command models a shell command. Each command invocation is a new object. Command options and parameters are represented as attributes (see Command::Declaration).
The main entry-point is #run, which uses #parse to populate attributes based on an array of command-line arguments, then calls #execute (which you provide) to make it go.
Constant Summary
Constants included from Option::Declaration
Option::Declaration::HELP_OPTION
Instance Attribute Summary collapse
-
#invocation_path ⇒ String
readonly
The path used to invoke this command.
Attributes included from Help
#declared_usage_descriptions, #description
Class Method Summary collapse
-
.run(invocation_path = File.basename($0), arguments = ARGV, context = {}) ⇒ Object
Create an instance of this command class, and run it.
Instance Method Summary collapse
-
#execute ⇒ Object
Execute the command (assuming that all options/parameters have been set).
-
#help ⇒ String
Usage documentation for this command.
-
#initialize(invocation_path, context = {}) ⇒ Command
constructor
Create a command execution.
-
#parse(arguments) ⇒ Array<String>
Parse command-line arguments.
-
#remaining_arguments ⇒ Array<String>
Unconsumed command-line arguments.
-
#run(arguments) ⇒ Object
Run the command, with the specified arguments.
Methods included from Help
derived_usage_description, usage, usage_descriptions
Methods included from Subcommand::Declaration
default_subcommand, find_subcommand, has_subcommands!, has_subcommands?, recognised_subcommands, subcommand
Methods included from Parameter::Declaration
has_parameters?, parameter, parameters
Methods included from Option::Declaration
declared_options, documented_options, find_option, has_options?, option, recognised_options, standard_options
Constructor Details
#initialize(invocation_path, context = {}) ⇒ Command
Create a command execution.
27 28 29 30 |
# File 'lib/clamp/command.rb', line 27 def initialize(invocation_path, context = {}) @invocation_path = invocation_path @context = context end |
Instance Attribute Details
#invocation_path ⇒ String (readonly)
Returns the path used to invoke this command.
34 35 36 |
# File 'lib/clamp/command.rb', line 34 def invocation_path @invocation_path end |
Class Method Details
.run(invocation_path = File.basename($0), arguments = ARGV, context = {}) ⇒ Object
Create an instance of this command class, and run it.
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/clamp/command.rb', line 117 def run(invocation_path = File.basename($0), arguments = ARGV, context = {}) begin new(invocation_path, context).run(arguments) rescue Clamp::UsageError => e $stderr.puts "ERROR: #{e.}" $stderr.puts "" $stderr.puts "See: '#{e.command.invocation_path} --help'" exit(1) rescue Clamp::HelpWanted => e puts e.command.help end end |
Instance Method Details
#execute ⇒ Object
Execute the command (assuming that all options/parameters have been set).
This method is designed to be overridden in sub-classes.
70 71 72 73 74 75 76 |
# File 'lib/clamp/command.rb', line 70 def execute if self.class.has_subcommands? execute_subcommand else raise "you need to define #execute" end end |
#help ⇒ String
Returns usage documentation for this command.
80 81 82 |
# File 'lib/clamp/command.rb', line 80 def help self.class.help(invocation_path) end |
#parse(arguments) ⇒ Array<String>
Parse command-line arguments.
47 48 49 50 51 52 |
# File 'lib/clamp/command.rb', line 47 def parse(arguments) @remaining_arguments = arguments.dup parse_parameters @remaining_arguments end |
#remaining_arguments ⇒ Array<String>
Returns unconsumed command-line arguments.
38 39 40 |
# File 'lib/clamp/command.rb', line 38 def remaining_arguments @remaining_arguments end |