Class: Commando

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptroute/commando.rb

Overview

Main class for option parsing.

Limitations: doesn’t currently grok the single letter getopt-style arguments, so “foo.rb -v -d -u” cannot be expressed as “foo.rb -vdu”.

Examples:

require "scriptroute/commando"

c = Commando.new(ARGV,  # allows substitution by srclient.rb
                 [ CommandoVar.new( "--start-speed",  
                                   "Kbps rate to start" ,
                                   :$StartSpeedKbps, 20),
                   CommandoVar.new( "--train-length", 
                                   "Filler packets in train" ,
                                   :$TrainLength, 20 ),
                   CommandoVar.new( "--hop",          
                                   "Specific hop to study (ttl=hop and hop-1), 0 for e2e" ,
                                   :$Hop, 0 ),
                   CommandoVar.new( [ "--verbose", "-v" ], 
                                   "print gobs of messages",
                                   :$VERBOSE, false ) ],
                 "destination-host")

raise "must start with a positive rate" if($StartSpeedKbps <= 0)
if(ARGV[0] == nil) then
  c.usage
  exit
end

Instance Method Summary collapse

Constructor Details

#initialize(argv, options, after_options_name) ⇒ Commando

Parse any recognized command line options, removing

anything parsed from ARGV.

Parameters:

  • argv (Array<String>)

    ARGV as provided by the interpreter.

  • options (Array [CommandoVar] an array of option descriptions)

    ptions [Array [CommandoVar] an array of option descriptions

  • after_options_name (String)

    the description of what comes after the options parsed.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/scriptroute/commando.rb', line 212

def initialize(argv, options, after_options_name)
  @options = options
  @after_options_name = after_options_name
  helpopt = CommandoClosure.new(["--help", "-h"], "this help", 
                                lambda { |a| self.usage; exit 0 })
  begin
    # helpopt goes last, in case the programmer wrote
    # a -h option in.
    (options + [helpopt]).each { |o| o.seek(argv) }
  rescue NameError => e 
    puts "Error in command line parsing: #{e}"
    puts 
    self.usage
    exit
  end
end

Instance Method Details

#usagevoid

This method returns an undefined value.

Print a usage message to stdout.



199
200
201
202
203
204
205
# File 'lib/scriptroute/commando.rb', line 199

def usage()
  puts "Usage: #{$0} [options] #{@after_options_name}"
  puts " %18s %4s %-72s" % [ "option", "default", "description" ]
  puts @options.map { |o|
    o.help
  }.join("\n")
end