Class: CommandParser::CmdParser
- Inherits:
-
Object
- Object
- CommandParser::CmdParser
- Defined in:
- lib/command_parser.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#before_proc(&block) ⇒ Object
Defines a proc to be called before any command.
-
#command(name, desc, *args_format, &block) ⇒ Object
Defines a new action for the command, several actions can be defined for a command.
- #deprecated_command(name, new_command) ⇒ Object
-
#description(str) ⇒ Object
Defines the additional information of the command.
-
#exit_code(code) ⇒ Object
Defines the exit code to be returned by the command.
- #exit_with_code(code, output = nil) ⇒ Object
-
#format(format, description, &block) ⇒ Object
Defines a block that will be used to parse the arguments of the command.
-
#initialize(args = [], &block) ⇒ CmdParser
constructor
A new instance of CmdParser.
-
#main(*args_format, &block) ⇒ Object
Defines a new action for the command, several actions can be defined for a command.
-
#name(str) ⇒ Object
Defines the name of the command.
-
#option(options) ⇒ Object
Defines a global option for the command that will be used for all the actions.
- #run ⇒ Object
-
#set(e, *args, &block) ⇒ Object
DEPRECATED, use format and options instead.
-
#usage(str) ⇒ Object
Defines the usage information of the command.
-
#version(str) ⇒ Object
Defines the version the command.
Constructor Details
#initialize(args = [], &block) ⇒ CmdParser
Returns a new instance of CmdParser.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/command_parser.rb', line 56 def initialize(args=[], &block) = Array.new @commands = Hash.new @command_list = Array.new @formats = Hash.new @main = nil @exit_code = nil @args = args = Hash.new @before_proc=nil @comm_name=nil define_default_formats instance_eval(&block) addons = Dir["#{OpenNebulaHelper::CLI_ADDONS_LOCATION}/#{File.basename($0)}/*"] if defined?(addons) and !addons.nil? addons.each do |addon_path| addon_code = File.read(addon_path) instance_eval(addon_code) end end self.run end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
54 55 56 |
# File 'lib/command_parser.rb', line 54 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
54 55 56 |
# File 'lib/command_parser.rb', line 54 def end |
Instance Method Details
#before_proc(&block) ⇒ Object
Defines a proc to be called before any command
114 115 116 |
# File 'lib/command_parser.rb', line 114 def before_proc(&block) @before_proc = block end |
#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 |
#deprecated_command(name, new_command) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/command_parser.rb', line 314 def deprecated_command(name, new_command) cmd = Hash.new cmd[:desc] = "Deprecated, use #{new_command} instead" cmd[:arity] = 0 cmd[:options] = [] cmd[:args_format] = [[:string, nil]] * 20 cmd[:deprecated] = new_command cmd[:proc] = lambda do print_deprecated(new_command) end @commands[name.to_sym] = cmd end |
#description(str) ⇒ Object
Defines the additional information of the command
102 103 104 |
# File 'lib/command_parser.rb', line 102 def description(str) @description = str end |
#exit_code(code) ⇒ Object
Defines the exit code to be returned by the command
191 192 193 |
# File 'lib/command_parser.rb', line 191 def exit_code(code) @exit_code = code end |
#exit_with_code(code, output = nil) ⇒ Object
195 196 197 198 |
# File 'lib/command_parser.rb', line 195 def exit_with_code(code, output=nil) puts output if output exit code end |
#format(format, description, &block) ⇒ Object
Defines a block that will be used to parse the arguments of the command. Formats defined using this method con be used in the arguments section of the command method, when defining a new action
129 130 131 132 133 134 |
# File 'lib/command_parser.rb', line 129 def format(format, description, &block) @formats[format] = { :desc => description, :proc => block } end |
#main(*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.
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/command_parser.rb', line 407 def main(*args_format, &block) @main=Hash.new @main[:arity] = 0 @main[:args_format] = Array.new args_format.collect {|args| if args.instance_of?(Array) @main[:arity]+=1 unless args.include?(nil) @main[:args_format] << args elsif args.instance_of?(Hash) && args[:options] << args[:options] else @main[:arity]+=1 @main[:args_format] << [args] end } @main[:proc] = block end |
#name(str) ⇒ Object
Defines the name of the command
108 109 110 |
# File 'lib/command_parser.rb', line 108 def name(str) @name = str end |
#option(options) ⇒ Object
Defines a global option for the command that will be used for all the actions
181 182 183 184 185 186 187 |
# File 'lib/command_parser.rb', line 181 def option() if .instance_of?(Array) .each { |o| << o } elsif .instance_of?(Hash) << end end |
#run ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/command_parser.rb', line 437 def run comm_name="" if @main comm_name = @name comm = @main elsif if @args[0] && !@args[0].match(/^-/) while comm.nil? and @args.size > 0 do current = @args.shift if comm_name.empty? @comm_name = comm_name = "#{current}".to_sym else @comm_name = comm_name = "#{comm_name} #{current}".to_sym end comm = @commands[comm_name] end end end if comm.nil? print_help exit 0 end if comm[:deprecated] print_deprecated(comm[:deprecated]) end = comm[:options] if comm parse() if comm begin @before_proc.call if @before_proc rescue StandardError => e STDERR.puts e. exit(-1) end check_args!(comm_name, comm[:arity], comm[:args_format]) begin rc = comm[:proc].call if rc.instance_of?(Array) && rc[0] != 0 STDERR.puts rc[1] exit(rc[0]) elsif rc.instance_of?(Array) puts rc[1] exit(rc[0]) else exit(@exit_code || rc) end rescue StandardError => e STDERR.puts e. exit(-1) end end end |
#set(e, *args, &block) ⇒ Object
DEPRECATED, use format and options instead
427 428 429 430 431 432 433 434 |
# File 'lib/command_parser.rb', line 427 def set(e, *args, &block) case e when :option option(args[0]) when :format format(args[0], args[1], &block) end end |
#usage(str) ⇒ Object
Defines the usage information of the command
89 90 91 92 |
# File 'lib/command_parser.rb', line 89 def usage(str) @usage = str @name ||= @usage.split(' ').first end |
#version(str) ⇒ Object
Defines the version the command
96 97 98 |
# File 'lib/command_parser.rb', line 96 def version(str) @version = str end |