Class: SpecForge::CLI::Command
- Inherits:
-
Object
- Object
- SpecForge::CLI::Command
- Includes:
- Actions
- Defined in:
- lib/spec_forge/cli/command.rb
Overview
Base class for CLI commands that provides common functionality and defines the DSL for declaring command properties.
Class Attribute Summary collapse
-
.command_name(name) ⇒ Object
Sets the command's name.
-
.description(description) ⇒ Object
Sets the command's description, displayed in detailed help.
-
.options ⇒ Object
writeonly
Sets the command's available options.
-
.summary(summary) ⇒ Object
Sets the command's summary, displayed in command list.
-
.syntax(syntax) ⇒ Object
Sets the command's syntax.
Instance Attribute Summary collapse
-
#arguments ⇒ Array
readonly
Command arguments passed from the command line.
-
#options ⇒ Hash
readonly
Command options passed from the command line.
Class Method Summary collapse
-
.aliases(*aliases) ⇒ Object
Adds command aliases.
-
.example(command, description) ⇒ Object
Adds an example of how to use the command.
-
.option(*args) {|value| ... } ⇒ Object
Adds a command line option.
-
.register(context) ⇒ Object
Registers the command with Commander.
Instance Method Summary collapse
-
#initialize(arguments, options) ⇒ Command
constructor
Creates a new command instance.
Methods included from Actions
Constructor Details
#initialize(arguments, options) ⇒ Command
Creates a new command instance
175 176 177 178 |
# File 'lib/spec_forge/cli/command.rb', line 175 def initialize(arguments, ) @arguments = arguments @options = end |
Class Attribute Details
.command_name(name) ⇒ Object
Sets the command's name
57 58 59 |
# File 'lib/spec_forge/cli/command.rb', line 57 def command_name(name) self.command_name = name end |
.description(description) ⇒ Object
Sets the command's description, displayed in detailed help
75 76 77 |
# File 'lib/spec_forge/cli/command.rb', line 75 def description(description) self.description = description end |
.options=(value) ⇒ Object (writeonly)
Sets the command's available options
50 51 52 |
# File 'lib/spec_forge/cli/command.rb', line 50 def (value) @options = value end |
.summary(summary) ⇒ Object
Sets the command's summary, displayed in command list
84 85 86 |
# File 'lib/spec_forge/cli/command.rb', line 84 def summary(summary) self.summary = summary end |
.syntax(syntax) ⇒ Object
Sets the command's syntax
66 67 68 |
# File 'lib/spec_forge/cli/command.rb', line 66 def syntax(syntax) self.syntax = syntax end |
Instance Attribute Details
#arguments ⇒ Array (readonly)
Command arguments passed from the command line
158 159 160 |
# File 'lib/spec_forge/cli/command.rb', line 158 def arguments @arguments end |
#options ⇒ Hash (readonly)
Command options passed from the command line
165 166 167 |
# File 'lib/spec_forge/cli/command.rb', line 165 def @options end |
Class Method Details
.aliases(*aliases) ⇒ Object
Adds command aliases
118 119 120 121 122 |
# File 'lib/spec_forge/cli/command.rb', line 118 def aliases(*aliases) @aliases ||= [] @aliases += aliases end |
.example(command, description) ⇒ Object
Adds an example of how to use the command
94 95 96 97 98 99 |
# File 'lib/spec_forge/cli/command.rb', line 94 def example(command, description) @examples ||= [] # Commander wants it backwards @examples << [description, command] end |
.option(*args) {|value| ... } ⇒ Object
Adds a command line option
107 108 109 110 111 |
# File 'lib/spec_forge/cli/command.rb', line 107 def option(*args, &block) @options ||= [] @options << [args, block] end |
.register(context) ⇒ Object
Registers the command with Commander
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/spec_forge/cli/command.rb', line 131 def register(context) raise "Missing command name" if @command_name.nil? context.command(@command_name) do |c| c.syntax = @syntax c.summary = @summary c.description = @description c.examples = @examples if @examples @options&.each do |opts, block| c.option(*opts, &block) end c.action { |args, opts| new(args, opts).call } end @aliases&.each do |alii| context.alias_command(alii, @command_name) end end |