lib/cri/command_dsl.rb in cri-2.5.0 vs lib/cri/command_dsl.rb in cri-2.6.0
- old
+ new
@@ -4,10 +4,14 @@
# The command DSL is a class that is used for building and modifying
# commands.
class CommandDSL
+ # Creates a new DSL, intended to be used for building a single command. A
+ # {CommandDSL} instance is not reusable; create a new instance if you want
+ # to build another command.
+ #
# @param [Cri::Command, nil] command The command to modify, or nil if a
# new command should be created
def initialize(command=nil)
@command = command || Cri::Command.new
end
@@ -99,14 +103,30 @@
# @param [String] desc The option description
#
# @option params [:forbidden, :required, :optional] :argument Whether the
# argument is forbidden, required or optional
#
+ # @option params [Boolean] :multiple Whether or not the option should
+ # be multi-valued
+ #
# @return [void]
def option(short, long, desc, params={}, &block)
- requiredness = params[:argument] || :forbidden
- self.add_option(short, long, desc, requiredness, block)
+ requiredness = params.fetch(:argument, :forbidden)
+ multiple = params.fetch(:multiple, false)
+
+ if short.nil? && long.nil?
+ raise ArgumentError, "short and long options cannot both be nil"
+ end
+
+ @command.option_definitions << {
+ :short => short.nil? ? nil : short.to_s,
+ :long => long.nil? ? nil : long.to_s,
+ :desc => desc,
+ :argument => requiredness,
+ :multiple => multiple,
+ :block => block,
+ }
end
alias_method :opt, :option
# Adds a new option with a required argument to the command. If a block is
# given, it will be executed when the option is successfully parsed.
@@ -115,15 +135,19 @@
#
# @param [String, Symbol, nil] long The long option name
#
# @param [String] desc The option description
#
+ # @option params [Boolean] :multiple Whether or not the option should
+ # be multi-valued
+ #
# @return [void]
#
# @see {#option}
- def required(short, long, desc, &block)
- self.add_option(short, long, desc, :required, block)
+ def required(short, long, desc, params={}, &block)
+ params = params.merge(:argument => :required)
+ self.option(short, long, desc, params, &block)
end
# Adds a new option with a forbidden argument to the command. If a block
# is given, it will be executed when the option is successfully parsed.
#
@@ -131,15 +155,19 @@
#
# @param [String, Symbol, nil] long The long option name
#
# @param [String] desc The option description
#
+ # @option params [Boolean] :multiple Whether or not the option should
+ # be multi-valued
+ #
# @return [void]
#
# @see {#option}
- def flag(short, long, desc, &block)
- self.add_option(short, long, desc, :forbidden, block)
+ def flag(short, long, desc, params={}, &block)
+ params = params.merge(:argument => :forbidden)
+ self.option(short, long, desc, params, &block)
end
alias_method :forbidden, :flag
# Adds a new option with an optional argument to the command. If a block
# is given, it will be executed when the option is successfully parsed.
@@ -148,25 +176,35 @@
#
# @param [String, Symbol, nil] long The long option name
#
# @param [String] desc The option description
#
+ # @option params [Boolean] :multiple Whether or not the option should
+ # be multi-valued
+ #
# @return [void]
#
# @see {#option}
- def optional(short, long, desc, &block)
- self.add_option(short, long, desc, :optional, block)
+ def optional(short, long, desc, params={}, &block)
+ params = params.merge(:argument => :optional)
+ self.option(short, long, desc, params, &block)
end
# Sets the run block to the given block. The given block should have two
# or three arguments (options, arguments, and optionally the command).
# Calling this will override existing run block or runner declarations
# (using {#run} and {#runner}, respectively).
#
+ # @yieldparam [Hash<Symbol,Object>] opts A map of option names, as defined
+ # in the option definitions, onto strings (when single-valued) or arrays
+ # (when multi-valued)
+ #
+ # @yieldparam [Array<String>] args A list of arguments
+ #
# @return [void]
def run(&block)
- unless block.arity != 2 || block.arity != 3
+ unless [2, 3].include?(block.arity)
raise ArgumentError,
"The block given to Cri::Command#run expects two or three args"
end
@command.block = block
@@ -183,24 +221,7 @@
def runner(klass)
run do |opts, args, cmd|
klass.new(opts, args, cmd).call
end
end
-
- protected
-
- def add_option(short, long, desc, argument, block)
- if short.nil? && long.nil?
- raise ArgumentError, "short and long options cannot both be nil"
- end
-
- @command.option_definitions << {
- :short => short.nil? ? nil : short.to_s,
- :long => long.nil? ? nil : long.to_s,
- :desc => desc,
- :argument => argument,
- :block => block }
- end
-
end
-
end