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.
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 |