Method: CommandParser::CmdParser#command

Defined in:
lib/command_parser.rb

#command(name, desc, *args_format, &block) ⇒ Object

Defines a new action for the command, several actions can be defined for a command. For example: create, delete, list. The options and args variables can be used inside the block, and they contain the parsedarguments and options.

Examples:

Definining two arguments:
    $ onetest test a1 a2

CommandParser::CmdParser.new(ARGV) do
    description "Test"
    usage "onetest <command> <args> [options]"
    version "1.0"

    options VERBOSE, HELP

    command :test, "Test", :test1, :test2, :options => XML do
        puts options[:xml]
        puts options[:verbose]
        puts args[0]
        puts args[1]
        [0, "It works"]
    end
end

Defining optional arguments: test1 is mandatory, test2 optional
    $ onetest test a1 | $ onetest test a1 a2

CommandParser::CmdParser.new(ARGV) do
    description "Test"
    usage "onetest <command> <args> [options]"
    version "1.0"

    options VERBOSE, HELP

    command :test, "Test", :test1, [:test2, nil], :options => XML do
        puts options[:xml]
        puts options[:verbose]
        puts args[0]
        puts "It works"
        0
    end
end

Defining an argument with different formats:
    $ onetest test a1 a2 | $ onetest test a1 123

CommandParser::CmdParser.new(ARGV) do
    description "Test"
    usage "onetest <command> <args> [options]"
    version "1.0"

    options VERBOSE, HELP

    format :format1, "String to Integer" do
        [0, arg.to_i]
    end

    command :test, "Test", :test1, [:format1, format2], :options => XML do
        puts options[:xml]
        puts options[:verbose]
        puts args[0]
        0
    end
end

Parameters:

  • name (Symbol)

    Name of the action (i.e: :create, :list)

  • desc (String)

    Description of the action

  • args_format (Array<Symbol, Array<Symbol, nil>>, Hash)

    arguments or specific options for this actiion Note that the first argument of the command is the action and should not be defined using this parameter. The rest of the argument must be defined using this parameter. This parameter can use formats previously defined with the format method Options are specified using a hash :options => … containing the hashes representing the options. The option method doc contains the hash that has to be used to specify an option

Yield Returns:

  • (Integer, Array[Integer, String])

    the block must return the exit_code and if a String is returned it will be printed



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
# File 'lib/command_parser.rb', line 283

def command(name, desc, *args_format, &block)
    if name.is_a? (Array)
        name = name.join(" ").to_sym
    end

    cmd = Hash.new
    cmd[:desc] = desc
    cmd[:arity] = 0
    cmd[:options] = []
    cmd[:args_format] = Array.new
    args_format.each {|args|
        if args.instance_of?(Array)
            cmd[:arity]+=1 unless args.include?(nil)
            cmd[:args_format] << args
        elsif args.instance_of?(Hash) && args[:options]
            if args[:options].is_a? Array
                args[:options].flatten!
                args[:options] = args[:options].sort_by {|o| o[:name] }
            end

            cmd[:options] << args[:options]
        else
            cmd[:arity]+=1
            cmd[:args_format] << [args]
        end
    }
    cmd[:proc] = block
    @command_list << name.to_sym
    @commands[name.to_sym] = cmd
end