class Commando

@example

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

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

Public Class Methods

new(argv, options, after_options_name) click to toggle source

Parse any recognized command line options, removing

anything parsed from ARGV.

@param argv [Array<String>] ARGV as provided by the interpreter. @param options [Array [CommandoVar] an array of option descriptions @param after_options_name [String] the description of

what comes after the options parsed.
# 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

Public Instance Methods

usage() click to toggle source

Print a usage message to stdout. @return [void]

# 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