Class: CommandParser::CmdParser
- Inherits:
-
Object
- Object
- CommandParser::CmdParser
- Defined in:
- lib/command_parser.rb
Overview
CmdParser class to handle command line arguments and options
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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/command_parser.rb', line 65 def initialize(args = [], &block) @available_options = [] @commands = {} @command_list = [] @formats = {} @main = nil @exit_code = nil @args = args @options = {} @before_proc=nil @comm_name=nil define_default_formats instance_eval(&block) addons = Dir["#{OpenNebulaHelper::CLI_ADDONS_LOCATION}/#{File.basename($PROGRAM_NAME)}/*"] if defined?(addons) && !addons.nil? addons.each do |addon_path| addon_code = File.read(addon_path) instance_eval(addon_code) end end run end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
63 64 65 |
# File 'lib/command_parser.rb', line 63 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
63 64 65 |
# File 'lib/command_parser.rb', line 63 def @options end |
Instance Method Details
#before_proc(&block) ⇒ Object
Defines a proc to be called before any command
125 126 127 |
# File 'lib/command_parser.rb', line 125 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.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/command_parser.rb', line 294 def command(name, desc, *args_format, &block) if name.is_a?(Array) name = name.join(' ').to_sym end cmd = {} cmd[:desc] = desc cmd[:arity] = 0 cmd[:options] = [] cmd[:args_format] = [] args_format.each do |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 end cmd[:proc] = block @command_list << name.to_sym @commands[name.to_sym] = cmd end |
#deprecated_command(name, new_command) ⇒ Object
325 326 327 328 329 330 331 |
# File 'lib/command_parser.rb', line 325 def deprecated_command(name, new_command) cmd = @commands[name.to_sym] || {} cmd[:desc] += "\nDeprecated, use #{new_command} instead" cmd[:deprecated] = new_command @commands[name.to_sym] = cmd end |
#description(str) ⇒ Object
Defines the additional information of the command
113 114 115 |
# File 'lib/command_parser.rb', line 113 def description(str) @description = str end |
#exit_code(code) ⇒ Object
Defines the exit code to be returned by the command
202 203 204 |
# File 'lib/command_parser.rb', line 202 def exit_code(code) @exit_code = code end |
#exit_with_code(code, output = nil) ⇒ Object
206 207 208 209 |
# File 'lib/command_parser.rb', line 206 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
140 141 142 143 144 145 |
# File 'lib/command_parser.rb', line 140 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.
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/command_parser.rb', line 412 def main(*args_format, &block) @main={} @main[:arity] = 0 @main[:args_format] = [] args_format.collect do |args| if args.instance_of?(Array) @main[:arity]+=1 unless args.include?(nil) @main[:args_format] << args elsif args.instance_of?(Hash) && args[:options] @available_options << args[:options] else @main[:arity]+=1 @main[:args_format] << [args] end end @main[:proc] = block end |
#name(str) ⇒ Object
Defines the name of the command
119 120 121 |
# File 'lib/command_parser.rb', line 119 def name(str) @name = str end |
#option(options) ⇒ Object
Defines a global option for the command that will be used for all the actions
192 193 194 195 196 197 198 |
# File 'lib/command_parser.rb', line 192 def option() if .instance_of?(Array) .each {|o| @available_options << o } elsif .instance_of?(Hash) @available_options << end end |
#run ⇒ Object
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 499 500 |
# File 'lib/command_parser.rb', line 441 def run comm_name='' if @main comm_name = @name comm = @main elsif @args[0] && !@args[0].match(/^-/) while comm.nil? && !@args.empty? current = @args.shift if comm_name.empty? (@comm_name = (comm_name = current.to_s.to_sym)) else (@comm_name = (comm_name = "#{comm_name} #{current}".to_sym)) end comm = @commands[comm_name] 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
432 433 434 435 436 437 438 439 |
# File 'lib/command_parser.rb', line 432 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
98 99 100 101 102 103 |
# File 'lib/command_parser.rb', line 98 def usage(str) @usage = str # rubocop:disable Naming/MemoizedInstanceVariableName @name ||= @usage.split(' ').first # rubocop:enable Naming/MemoizedInstanceVariableName end |
#version(str) ⇒ Object
Defines the version the command
107 108 109 |
# File 'lib/command_parser.rb', line 107 def version(str) @version = str end |