lib/cri/command_dsl.rb in cri-2.6.1 vs lib/cri/command_dsl.rb in cri-2.7.0

- old
+ new

@@ -1,37 +1,33 @@ # encoding: utf-8 module Cri - # The command DSL is a class that is used for building and modifying # commands. class CommandDSL + # @return [Cri::Command] The built command + attr_reader :command # 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) + def initialize(command = nil) @command = command || Cri::Command.new end - # @return [Cri::Command] The built command - def command - @command - end - # Adds a subcommand to the current command. The command can either be # given explicitly, or a block can be given that defines the command. # # @param [Cri::Command, nil] command The command to add as a subcommand, # or nil if the block should be used to define the command that will be # added as a subcommand # # @return [void] - def subcommand(command=nil, &block) + def subcommand(command = nil, &block) if command.nil? command = Cri::Command.define(&block) end @command.add_command(command) @@ -106,26 +102,31 @@ # argument is forbidden, required or optional # # @option params [Boolean] :multiple Whether or not the option should # be multi-valued # + # @option params [Boolean] :hidden Whether or not the option should + # be printed in the help output + # # @return [void] - def option(short, long, desc, params={}, &block) + def option(short, long, desc, params = {}, &block) requiredness = params.fetch(:argument, :forbidden) multiple = params.fetch(:multiple, false) + hidden = params.fetch(:hidden, false) if short.nil? && long.nil? - raise ArgumentError, "short and long options cannot both be nil" + fail 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, + :hidden => hidden, } end alias_method :opt, :option # Adds a new option with a required argument to the command. If a block is @@ -138,16 +139,19 @@ # @param [String] desc The option description # # @option params [Boolean] :multiple Whether or not the option should # be multi-valued # + # @option params [Boolean] :hidden Whether or not the option should + # be printed in the help output + # # @return [void] # # @see {#option} - def required(short, long, desc, params={}, &block) + def required(short, long, desc, params = {}, &block) params = params.merge(:argument => :required) - self.option(short, long, desc, params, &block) + 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. # @@ -158,16 +162,19 @@ # @param [String] desc The option description # # @option params [Boolean] :multiple Whether or not the option should # be multi-valued # + # @option params [Boolean] :hidden Whether or not the option should + # be printed in the help output + # # @return [void] # # @see {#option} - def flag(short, long, desc, params={}, &block) + def flag(short, long, desc, params = {}, &block) params = params.merge(:argument => :forbidden) - self.option(short, long, desc, params, &block) + 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. @@ -179,16 +186,19 @@ # @param [String] desc The option description # # @option params [Boolean] :multiple Whether or not the option should # be multi-valued # + # @option params [Boolean] :hidden Whether or not the option should + # be printed in the help output + # # @return [void] # # @see {#option} - def optional(short, long, desc, params={}, &block) + def optional(short, long, desc, params = {}, &block) params = params.merge(:argument => :optional) - self.option(short, long, desc, params, &block) + 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 @@ -201,11 +211,11 @@ # @yieldparam [Array<String>] args A list of arguments # # @return [void] def run(&block) unless [2, 3].include?(block.arity) - raise ArgumentError, - "The block given to Cri::Command#run expects two or three args" + fail ArgumentError, + 'The block given to Cri::Command#run expects two or three args' end @command.block = block end