Object
The command DSL is a class that is used for building and modifying commands.
@param [Cri::Command, nil] command The command to modify, or nil if a
new command should be created
# File lib/cri/command_dsl.rb, line 10 def initialize(command=nil) @command = command || Cri::Command.new end
Sets the command aliases.
@param [String, Symbol, Array] args The new command aliases
@return [void]
# File lib/cri/command_dsl.rb, line 49 def aliases(*args) @command.aliases = args.flatten.map { |a| a.to_s } end
@return [Cri::Command] The built command
# File lib/cri/command_dsl.rb, line 15 def command @command end
Sets the command description.
@param [String] arg The new command description
@return [void]
# File lib/cri/command_dsl.rb, line 67 def description(arg) @command.description = arg 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.
@param [String, Symbol] short The short option name
@param [String, Symbol] long The long option name
@param [String] desc The option description
@return [void]
@see {option}
# File lib/cri/command_dsl.rb, line 138 def flag(short, long, desc, &block) self.add_option(short, long, desc, :forbidden, block) end
Sets the command name.
@param [String] arg The new command name
@return [void]
# File lib/cri/command_dsl.rb, line 40 def name(arg) @command.name = arg end
Adds a new option to the command. If a block is given, it will be executed when the option is successfully parsed.
@param [String, Symbol] short The short option name
@param [String, Symbol] long The long option name
@param [String] desc The option description
@option params [:forbidden, :required, :optional] :argument Whether the
argument is forbidden, required or optional
@return [void]
# File lib/cri/command_dsl.rb, line 104 def option(short, long, desc, params={}, &block) requiredness = params[:argument] || :forbidden self.add_option(short, long, desc, requiredness, block) end
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.
@param [String, Symbol] short The short option name
@param [String, Symbol] long The long option name
@param [String] desc The option description
@return [void]
@see {option}
# File lib/cri/command_dsl.rb, line 155 def optional(short, long, desc, &block) self.add_option(short, long, desc, :optional, block) end
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.
@param [String, Symbol] short The short option name
@param [String, Symbol] long The long option name
@param [String] desc The option description
@return [void]
@see {option}
# File lib/cri/command_dsl.rb, line 122 def required(short, long, desc, &block) self.add_option(short, long, desc, :required, 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).
@return [void]
# File lib/cri/command_dsl.rb, line 165 def run(&block) unless block.arity != 2 || block.arity != 3 raise ArgumentError, "The block given to Cri::Command#run expects two or three args" end @command.block = block end
Defines the runner class for this command. Calling this will override existing run block or runner declarations (using {run} and {runner}, respectively).
@param [Class<CommandRunner>] klass The command runner class (subclass
of {CommandRunner}) that is used for executing this command.
@return [void]
# File lib/cri/command_dsl.rb, line 182 def runner(klass) run do |opts, args, cmd| klass.new(opts, args, cmd).call end 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]
# File lib/cri/command_dsl.rb, line 27 def subcommand(command=nil, &block) if command.nil? command = Cri::Command.define(&block) end @command.add_command(command) end
Sets the command summary.
@param [String] arg The new command summary
@return [void]
# File lib/cri/command_dsl.rb, line 58 def summary(arg) @command.summary = arg end
Sets the command usage. The usage should not include the “usage:” prefix, nor should it include the command names of the supercommand.
@param [String] arg The new command usage
@return [void]
# File lib/cri/command_dsl.rb, line 77 def usage(arg) @command.usage = arg end
Generated with the Darkfish Rdoc Generator 2.