Class: Commando
- Inherits:
-
Object
- Object
- Commando
- 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”.
Instance Method Summary collapse
-
#initialize(argv, options, after_options_name) ⇒ Commando
constructor
Parse any recognized command line options, removing anything parsed from ARGV.
-
#usage ⇒ void
Print a usage message to stdout.
Constructor Details
#initialize(argv, options, after_options_name) ⇒ Commando
Parse any recognized command line options, removing
anything parsed from ARGV.
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 = 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. ( + [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
#usage ⇒ void
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 |