Class: Dry::CLI::Command
- Inherits:
-
Object
- Object
- Dry::CLI::Command
- Extended by:
- Forwardable
- Defined in:
- lib/dry/cli/command.rb
Overview
Base class for commands
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.argument(name, options = {}) ⇒ Object
Specify an argument.
- .default_params ⇒ Object private
-
.desc(description) ⇒ Object
Set the description of the command.
-
.example(*examples) ⇒ Object
Describe the usage of the command.
- .inherited(base) ⇒ Object private
-
.option(name, options = {}) ⇒ Object
Command line option (aka optional argument).
- .optional_arguments ⇒ Object private
- .params ⇒ Object private
- .required_arguments ⇒ Object private
- .subcommands ⇒ Object private
- .superclass_arguments ⇒ Object private
- .superclass_options ⇒ Object private
- .superclass_variable_dup(var) ⇒ Object private
Class Method Details
.argument(name, options = {}) ⇒ Object
Specify an argument
197 198 199 200 201 202 203 204 |
# File 'lib/dry/cli/command.rb', line 197 def self.argument(name, = {}) new_arg = Argument.new(name, ) duplicate_index = @arguments.find_index { _1.name == new_arg.name } @arguments.delete_at(duplicate_index) unless duplicate_index.nil? @arguments << new_arg end |
.default_params ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
335 336 337 338 339 |
# File 'lib/dry/cli/command.rb', line 335 def self.default_params params.each_with_object({}) do |param, result| result[param.name] = param.default unless param.default.nil? end end |
.desc(description) ⇒ Object
Set the description of the command
67 68 69 |
# File 'lib/dry/cli/command.rb', line 67 def self.desc(description) @description = description end |
.example(*examples) ⇒ Object
Describe the usage of the command
103 104 105 |
# File 'lib/dry/cli/command.rb', line 103 def self.example(*examples) @examples += examples.flatten(1) end |
.inherited(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dry/cli/command.rb', line 14 def self.inherited(base) super base.class_eval do @_mutex = Mutex.new @description = nil @examples = [] @subcommands = [] @arguments = base.superclass_arguments || [] = base. || [] end base.extend ClassMethods end |
.option(name, options = {}) ⇒ Object
Command line option (aka optional argument)
316 317 318 319 320 321 322 323 |
# File 'lib/dry/cli/command.rb', line 316 def self.option(name, = {}) new_op = Option.new(name, ) duplicate_index = .find_index { _1.name == new_op.name } .delete_at(duplicate_index) unless duplicate_index.nil? << new_op end |
.optional_arguments ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
349 350 351 |
# File 'lib/dry/cli/command.rb', line 349 def self.optional_arguments arguments.reject(&:required?) end |
.params ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
327 328 329 330 331 |
# File 'lib/dry/cli/command.rb', line 327 def self.params @_mutex.synchronize do (@arguments + ).uniq end end |
.required_arguments ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
343 344 345 |
# File 'lib/dry/cli/command.rb', line 343 def self.required_arguments arguments.select(&:required?) end |
.subcommands ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
355 356 357 |
# File 'lib/dry/cli/command.rb', line 355 def self.subcommands subcommands end |
.superclass_arguments ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
369 370 371 |
# File 'lib/dry/cli/command.rb', line 369 def self.superclass_arguments superclass_variable_dup(:@arguments) end |
.superclass_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
375 376 377 |
# File 'lib/dry/cli/command.rb', line 375 def self. superclass_variable_dup(:@options) end |
.superclass_variable_dup(var) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
361 362 363 364 365 |
# File 'lib/dry/cli/command.rb', line 361 def self.superclass_variable_dup(var) if superclass.instance_variable_defined?(var) superclass.instance_variable_get(var).dup end end |