Class: RVGP::Base::Command
- Inherits:
-
Object
- Object
- RVGP::Base::Command
- Includes:
- Application::DescendantRegistry
- Defined in:
- lib/rvgp/base/command.rb
Overview
If you’re looking to write your own rvgp commands, or if you wish to add a rake task - this is the start of that endeavor.
All of the built-in rvgp commands are descendants of this Base class. And, the easiest way to get started in writing your own, is simply to emulate one of these examples. You can see links to these examples listed under Commands.
When you’re ready to start typing out your code, just place this code in a .rb file under the app/commands directory of your project - and rvgp will pick it up from there. An instance of a Command, that inherits from this base, is initialized with the parsed contents of the command line, for any case when a user invokes the command by its name on the CLI.
The content below documents the argument handling, rake workflow, and related functionality available to you in your commands.
Defined Under Namespace
Modules: RakeTask Classes: Option, PlotTarget, ReconcilerTarget, Target
Constant Summary collapse
- OPTION_ALL =
This is shortcut to a –all/-a option, which is common across the built-in rvgp commands
%i[all a].freeze
- OPTION_LIST =
This is shortcut to a –list/-l option, which is common across the built-in rvgp commands
%i[list l].freeze
Instance Attribute Summary collapse
-
#errors ⇒ Array<String>
readonly
This array contains any errors that were encountered, when attempting to initialize this command.
-
#options ⇒ Hash<Symbol,TrueClass, String>
readonly
A hash of pairs, with keys being set to the ‘long’ form of any options that were passed on the command line.
-
#targets ⇒ <Object>
readonly
The parsed targets, that were encountered, for this command.
Class Method Summary collapse
-
.accepts_options(*args) ⇒ Object
This method exists as a shortcut for inheriting classes, to use, in defining what options their command supports.
-
.options ⇒ Array<RVGP::Base::Command::Option] the options this command handles
Return the options that have been defined for this command.
Instance Method Summary collapse
-
#execute! ⇒ void
Executes the command, using the provided options, for each of the targets provided.
-
#initialize(*args) ⇒ Command
constructor
Create a new Command, suitable for execution, and initialized with command line arguments.
-
#valid? ⇒ TrueClass, FalseClass
Indicates whether we can execute this command, given the provided arguments.
Constructor Details
#initialize(*args) ⇒ Command
Create a new Command, suitable for execution, and initialized with command line arguments.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/rvgp/base/command.rb', line 281 def initialize(*args) @errors = [] @options = {} @targets = [] # We'll cast the arguments to one of these, instead of storing strings target_klass = self.class.const_get('Target') @options, remainders = Option. self.class., args missing_targets = [] remainders.each do |remainder| if target_klass targets = target_klass.from_s remainder if targets @targets += targets else missing_targets << remainder end else @targets << remainder end end if [:list] && target_klass indent = I18n.t 'status.indicators.indent' puts ([RVGP.pastel.bold(I18n.t(format('commands.%s.list_targets', self.class.name)))] + target_klass.all.map { |target| indent + target.name }).join("\n") exit end @targets = target_klass.all if [:all] && target_klass @errors << I18n.t('error.no_targets') if @targets.empty? @errors << I18n.t('error.missing_target', targets: missing_targets.join(', ')) unless missing_targets.empty? end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
This array contains any errors that were encountered, when attempting to initialize this command.
32 33 34 |
# File 'lib/rvgp/base/command.rb', line 32 def errors @errors end |
#options ⇒ Hash<Symbol,TrueClass, String> (readonly)
A hash of pairs, with keys being set to the ‘long’ form of any options that were passed on the command line. And with values consisting of either ‘string’ (for the case of a ”–option=value’) or ‘True’ for the prescense of an option in the short or long form (“-l” or “–long”)
32 33 34 |
# File 'lib/rvgp/base/command.rb', line 32 def @options end |
#targets ⇒ <Object> (readonly)
The parsed targets, that were encountered, for this command. Note that this Array may contain just about any object whatsoever, depending on how the Target for a command is written.
32 33 34 |
# File 'lib/rvgp/base/command.rb', line 32 def targets @targets end |
Class Method Details
.accepts_options(*args) ⇒ Object
This method exists as a shortcut for inheriting classes, to use, in defining what options their command supports. This method expects a variable amount of arrays. With, each of those arrays expected to contain a :short and :long symbol, and optionally a third Hash element, specifying initialize options.
Each of these arguments are supplied to RVGP::Base::Command::Option#initialize. OPTION_ALL and OPTION_LIST are common parameters to supply as arguments to this method.
349 350 351 |
# File 'lib/rvgp/base/command.rb', line 349 def (*args) @options = args.map { |option_args| Option.new(*option_args) } end |
.options ⇒ Array<RVGP::Base::Command::Option] the options this command handles
Return the options that have been defined for this command
355 356 357 |
# File 'lib/rvgp/base/command.rb', line 355 def @options || [] end |
Instance Method Details
#execute! ⇒ void
This method returns an undefined value.
Executes the command, using the provided options, for each of the targets provided.
327 328 329 |
# File 'lib/rvgp/base/command.rb', line 327 def execute! execute_each_target end |
#valid? ⇒ TrueClass, FalseClass
Indicates whether we can execute this command, given the provided arguments
321 322 323 |
# File 'lib/rvgp/base/command.rb', line 321 def valid? errors.empty? end |