1 ## lib/trollop.rb -- trollop command-line processing library
2 ## Author:: William Morgan (mailto: wmorgan-trollop@masanjin.net)
3 ## Copyright:: Copyright 2007 William Morgan
4 ## License:: GNU GPL version 2
12 ## Thrown by Parser in the event of a commandline error. Not needed if
13 ## you're using the Trollop::options entry.
14 class CommandlineError < StandardError; end
16 ## Thrown by Parser if the user passes in '-h' or '--help'. Handled
17 ## automatically by Trollop#options.
18 class HelpNeeded < StandardError; end
20 ## Thrown by Parser if the user passes in '-h' or '--version'. Handled
21 ## automatically by Trollop#options.
22 class VersionNeeded < StandardError; end
24 ## Regex for floating point numbers
25 FLOAT_RE = /^-?((\d+(\.\d+)?)|(\.\d+))$/
27 ## Regex for parameters
28 PARAM_RE = /^-(-|\.$|[^\d\.])/
30 ## The commandline parser. In typical usage, the methods in this class
31 ## will be handled internally by Trollop::options. In this case, only the
32 ## #opt, #banner and #version, #depends, and #conflicts methods will
33 ## typically be called.
35 ## If it's necessary to instantiate this class (for more complicated
36 ## argument-parsing situations), be sure to call #parse to actually
37 ## produce the output hash.
40 ## The set of values that indicate a flag option when passed as the
41 ## +:type+ parameter of #opt.
42 FLAG_TYPES = [:flag, :bool, :boolean]
44 ## The set of values that indicate a single-parameter (normal) option when
45 ## passed as the +:type+ parameter of #opt.
47 ## A value of +io+ corresponds to a readable IO resource, including
48 ## a filename, URI, or the strings 'stdin' or '-'.
49 SINGLE_ARG_TYPES = [:int, :integer, :string, :double, :float, :io, :date]
51 ## The set of values that indicate a multiple-parameter option (i.e., that
52 ## takes multiple space-separated values on the commandline) when passed as
53 ## the +:type+ parameter of #opt.
54 MULTI_ARG_TYPES = [:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
56 ## The complete set of legal values for the +:type+ parameter of #opt.
57 TYPES = FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
59 INVALID_SHORT_ARG_REGEX = /[\d-]/ #:nodoc:
61 ## The values from the commandline that were not interpreted by #parse.
62 attr_reader :leftovers
64 ## The complete configuration hashes for each option. (Mainly useful
68 ## Initializes the parser, and instance-evaluates any block given.
78 @stop_on_unknown = false
80 #instance_eval(&b) if b # can't take arguments
81 cloaker(&b).bind(self).call(*a) if b
84 ## Define an option. +name+ is the option name, a unique identifier
85 ## for the option that you will use internally, which should be a
86 ## symbol or a string. +desc+ is a string description which will be
87 ## displayed in help messages.
89 ## Takes the following optional arguments:
91 ## [+:long+] Specify the long form of the argument, i.e. the form with two dashes. If unspecified, will be automatically derived based on the argument name by turning the +name+ option into a string, and replacing any _'s by -'s.
92 ## [+:short+] Specify the short form of the argument, i.e. the form with one dash. If unspecified, will be automatically derived from +name+.
93 ## [+:type+] Require that the argument take a parameter or parameters of type +type+. For a single parameter, the value can be a member of +SINGLE_ARG_TYPES+, or a corresponding Ruby class (e.g. +Integer+ for +:int+). For multiple-argument parameters, the value can be any member of +MULTI_ARG_TYPES+ constant. If unset, the default argument type is +:flag+, meaning that the argument does not take a parameter. The specification of +:type+ is not necessary if a +:default+ is given.
94 ## [+:default+] Set the default value for an argument. Without a default value, the hash returned by #parse (and thus Trollop::options) will have a +nil+ value for this key unless the argument is given on the commandline. The argument type is derived automatically from the class of the default value given, so specifying a +:type+ is not necessary if a +:default+ is given. (But see below for an important caveat when +:multi+: is specified too.) If the argument is a flag, and the default is set to +true+, then if it is specified on the the commandline the value will be +false+.
95 ## [+:required+] If set to +true+, the argument must be provided on the commandline.
96 ## [+:multi+] If set to +true+, allows multiple occurrences of the option on the commandline. Otherwise, only a single instance of the option is allowed. (Note that this is different from taking multiple parameters. See below.)
98 ## Note that there are two types of argument multiplicity: an argument
99 ## can take multiple values, e.g. "--arg 1 2 3". An argument can also
100 ## be allowed to occur multiple times, e.g. "--arg 1 --arg 2".
102 ## Arguments that take multiple values should have a +:type+ parameter
103 ## drawn from +MULTI_ARG_TYPES+ (e.g. +:strings+), or a +:default:+
104 ## value of an array of the correct type (e.g. [String]). The
105 ## value of this argument will be an array of the parameters on the
108 ## Arguments that can occur multiple times should be marked with
109 ## +:multi+ => +true+. The value of this argument will also be an array.
110 ## In contrast with regular non-multi options, if not specified on
111 ## the commandline, the default value will be [], not nil.
113 ## These two attributes can be combined (e.g. +:type+ => +:strings+,
114 ## +:multi+ => +true+), in which case the value of the argument will be
115 ## an array of arrays.
117 ## There's one ambiguous case to be aware of: when +:multi+: is true and a
118 ## +:default+ is set to an array (of something), it's ambiguous whether this
119 ## is a multi-value argument as well as a multi-occurrence argument.
120 ## In thise case, Trollop assumes that it's not a multi-value argument.
121 ## If you want a multi-value, multi-occurrence argument with a default
122 ## value, you must specify +:type+ as well.
124 def opt name, desc="", opts={}
125 raise ArgumentError, "you already have an argument named '#{name}'" if @specs.member? name
128 opts[:type] = # normalize
130 when :boolean, :bool; :flag
132 when :integers; :ints
134 when :doubles; :floats
136 case opts[:type].name
137 when 'TrueClass', 'FalseClass'; :flag
138 when 'String'; :string
144 raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'"
148 raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type])
152 ## for options with :multi => true, an array default doesn't imply
153 ## a multi-valued argument. for that you have to specify a :type
154 ## as well. (this is how we disambiguate an ambiguous situation;
155 ## see the docs for Parser#opt for details.)
156 disambiguated_default =
157 if opts[:multi] && opts[:default].is_a?(Array) && !opts[:type]
164 case disambiguated_default
167 when TrueClass, FalseClass; :flag
172 if opts[:default].empty?
173 raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'"
175 case opts[:default][0] # the first element determines the types
177 when Numeric; :floats
178 when String; :strings
182 raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'"
186 raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'"
189 raise ArgumentError, ":type specification and default type don't match (default type is #{type_from_default})" if opts[:type] && type_from_default && opts[:type] != type_from_default
191 opts[:type] = opts[:type] || type_from_default || :flag
194 opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-")
202 raise ArgumentError, "invalid long option name #{opts[:long].inspect}"
204 raise ArgumentError, "long option name #{opts[:long].inspect} is already taken; please specify a (different) :long" if @long[opts[:long]]
207 opts[:short] = opts[:short].to_s if opts[:short] unless opts[:short] == :none
208 opts[:short] = case opts[:short]
210 when nil, :none, /^.$/; opts[:short]
211 else raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'"
215 raise ArgumentError, "short option name #{opts[:short].inspect} is already taken; please specify a (different) :short" if @short[opts[:short]]
216 raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ INVALID_SHORT_ARG_REGEX
219 ## fill in :default for flags
220 opts[:default] = false if opts[:type] == :flag && opts[:default].nil?
222 ## autobox :default for :multi (multi-occurrence) arguments
223 opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].is_a?(Array)
226 opts[:multi] ||= false
229 @long[opts[:long]] = name
230 @short[opts[:short]] = name if opts[:short] && opts[:short] != :none
232 @order << [:opt, name]
235 ## Sets the version string. If set, the user can request the version
236 ## on the commandline. Should probably be of the form "<program name>
237 ## <version number>".
238 def version s=nil; @version = s if s; @version end
240 ## Adds text to the help display. Can be interspersed with calls to
241 ## #opt to build a multi-section help page.
242 def banner s; @order << [:text, s] end
245 ## Marks two (or more!) options as requiring each other. Only handles
246 ## undirected (i.e., mutual) dependencies. Directed dependencies are
247 ## better modeled with Trollop::die.
249 syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
250 @constraints << [:depends, syms]
253 ## Marks two (or more!) options as conflicting.
255 syms.each { |sym| raise ArgumentError, "unknown option '#{sym}'" unless @specs[sym] }
256 @constraints << [:conflicts, syms]
259 ## Defines a set of words which cause parsing to terminate when
260 ## encountered, such that any options to the left of the word are
261 ## parsed as usual, and options to the right of the word are left
264 ## A typical use case would be for subcommand support, where these
265 ## would be set to the list of subcommands. A subsequent Trollop
266 ## invocation would then be used to parse subcommand options, after
267 ## shifting the subcommand off of ARGV.
269 @stop_words = [*words].flatten
272 ## Similar to #stop_on, but stops on any unknown word when encountered
273 ## (unless it is a parameter for an argument). This is useful for
274 ## cases where you don't know the set of subcommands ahead of time,
275 ## i.e., without first parsing the global options.
277 @stop_on_unknown = true
280 ## Parses the commandline. Typically called by Trollop::options.
281 def parse cmdline=ARGV
285 opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
286 opt :help, "Show this message" unless @specs[:help] || @long["help"]
288 @specs.each do |sym, opts|
289 required[sym] = true if opts[:required]
290 vals[sym] = opts[:default]
291 vals[sym] = [] if opts[:multi] && !opts[:default] # multi arguments default to [], not nil
294 resolve_default_short_options
298 @leftovers = each_arg cmdline do |arg, params|
305 raise CommandlineError, "invalid argument syntax: '#{arg}'"
307 raise CommandlineError, "unknown argument '#{arg}'" unless sym
309 if given_args.include?(sym) && !@specs[sym][:multi]
310 raise CommandlineError, "option '#{arg}' specified multiple times"
313 given_args[sym] ||= {}
315 given_args[sym][:arg] = arg
316 given_args[sym][:params] ||= []
318 # The block returns the number of parameters taken.
322 if SINGLE_ARG_TYPES.include?(@specs[sym][:type])
323 given_args[sym][:params] << params[0, 1] # take the first parameter
325 elsif MULTI_ARG_TYPES.include?(@specs[sym][:type])
326 given_args[sym][:params] << params # take all the parameters
327 num_params_taken = params.size
334 ## check for version and help args
335 raise VersionNeeded if given_args.include? :version
336 raise HelpNeeded if given_args.include? :help
338 ## check constraint satisfaction
339 @constraints.each do |type, syms|
340 constraint_sym = syms.find { |sym| given_args[sym] }
341 next unless constraint_sym
345 syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} requires --#{@specs[sym][:long]}" unless given_args.include? sym }
347 syms.each { |sym| raise CommandlineError, "--#{@specs[constraint_sym][:long]} conflicts with --#{@specs[sym][:long]}" if given_args.include?(sym) && (sym != constraint_sym) }
351 required.each do |sym, val|
352 raise CommandlineError, "option '#{sym}' must be specified" unless given_args.include? sym
356 given_args.each do |sym, given_data|
357 arg = given_data[:arg]
358 params = given_data[:params]
361 raise CommandlineError, "option '#{arg}' needs a parameter" if params.empty? && opts[:type] != :flag
363 vals["#{sym}_given".intern] = true # mark argument as specified on the commandline
367 vals[sym] = !opts[:default]
369 vals[sym] = params.map { |pg| pg.map { |p| parse_integer_parameter p, arg } }
371 vals[sym] = params.map { |pg| pg.map { |p| parse_float_parameter p, arg } }
372 when :string, :strings
373 vals[sym] = params.map { |pg| pg.map { |p| p.to_s } }
375 vals[sym] = params.map { |pg| pg.map { |p| parse_io_parameter p, arg } }
377 vals[sym] = params.map { |pg| pg.map { |p| parse_date_parameter p, arg } }
380 if SINGLE_ARG_TYPES.include?(opts[:type])
381 unless opts[:multi] # single parameter
382 vals[sym] = vals[sym][0][0]
383 else # multiple options, each with a single parameter
384 vals[sym] = vals[sym].map { |p| p[0] }
386 elsif MULTI_ARG_TYPES.include?(opts[:type]) && !opts[:multi]
387 vals[sym] = vals[sym][0] # single option, with multiple parameters
389 # else: multiple options, with multiple parameters
392 ## allow openstruct-style accessors
394 def method_missing(m, *args)
395 self[m] || self[m.to_s]
401 def parse_date_parameter param, arg #:nodoc:
404 time = Chronic.parse(param)
406 # chronic is not available
408 time ? Date.new(time.year, time.month, time.day) : Date.parse(param)
409 rescue ArgumentError => e
410 raise CommandlineError, "option '#{arg}' needs a date"
414 ## Print the help message to +stream+.
415 def educate stream=$stdout
416 width # just calculate it now; otherwise we have to be careful not to
417 # call this unless the cursor's at the beginning of a line.
420 @specs.each do |name, spec|
421 left[name] = "--#{spec[:long]}" +
422 (spec[:short] && spec[:short] != :none ? ", -#{spec[:short]}" : "") +
428 when :strings; " <s+>"
430 when :floats; " <f+>"
431 when :io; " <filename/uri>"
432 when :ios; " <filename/uri+>"
433 when :date; " <date>"
434 when :dates; " <date+>"
438 leftcol_width = left.values.map { |s| s.length }.max || 0
439 rightcol_start = leftcol_width + 6 # spaces
441 unless @order.size > 0 && @order.first.first == :text
442 stream.puts "#@version\n" if @version
443 stream.puts "Options:"
446 @order.each do |what, opt|
448 stream.puts wrap(opt)
453 stream.printf " %#{leftcol_width}s: ", left[opt]
454 desc = spec[:desc] + begin
455 default_s = case spec[:default]
456 when $stdout; "<stdout>"
457 when $stdin; "<stdin>"
458 when $stderr; "<stderr>"
460 spec[:default].join(", ")
466 if spec[:desc] =~ /\.$/
467 " (Default: #{default_s})"
469 " (default: #{default_s})"
475 stream.puts wrap(desc, :width => width - rightcol_start - 1, :prefix => rightcol_start)
480 @width ||= if $stdout.tty?
495 def wrap str, opts={} # :nodoc:
499 str.split("\n").map { |s| wrap_line s, opts }.flatten
505 ## yield successive arg, parameter pairs
510 until i >= args.length
511 if @stop_words.member? args[i]
512 remains += args[i .. -1]
516 when /^--$/ # arg terminator
517 remains += args[(i + 1) .. -1]
519 when /^--(\S+?)=(.*)$/ # long argument with equals
520 yield "--#{$1}", [$2]
522 when /^--(\S+)$/ # long argument
523 params = collect_argument_parameters(args, i + 1)
525 num_params_taken = yield args[i], params
526 unless num_params_taken
528 remains += args[i + 1 .. -1]
534 i += 1 + num_params_taken
535 else # long argument no parameter
539 when /^-(\S+)$/ # one or more short arguments
540 shortargs = $1.split(//)
541 shortargs.each_with_index do |a, j|
542 if j == (shortargs.length - 1)
543 params = collect_argument_parameters(args, i + 1)
545 num_params_taken = yield "-#{a}", params
546 unless num_params_taken
548 remains += args[i + 1 .. -1]
554 i += 1 + num_params_taken
555 else # argument no parameter
565 remains += args[i .. -1]
577 def parse_integer_parameter param, arg
578 raise CommandlineError, "option '#{arg}' needs an integer" unless param =~ /^\d+$/
582 def parse_float_parameter param, arg
583 raise CommandlineError, "option '#{arg}' needs a floating-point number" unless param =~ FLOAT_RE
587 def parse_io_parameter param, arg
589 when /^(stdin|-)$/i; $stdin
594 rescue SystemCallError => e
595 raise CommandlineError, "file or url for option '#{arg}' cannot be opened: #{e.message}"
600 def collect_argument_parameters args, start_at
603 while args[pos] && args[pos] !~ PARAM_RE && !@stop_words.member?(args[pos]) do
610 def resolve_default_short_options
611 @order.each do |type, name|
612 next unless type == :opt
616 c = opts[:long].split(//).find { |d| d !~ INVALID_SHORT_ARG_REGEX && !@short.member?(d) }
617 if c # found a character to use
624 def wrap_line str, opts={}
625 prefix = opts[:prefix] || 0
626 width = opts[:width] || (self.width - 1)
629 until start > str.length
631 if start + width >= str.length
634 x = str.rindex(/\s/, start + width)
635 x = str.index(/\s/, start) if x && x < start
638 ret << (ret.empty? ? "" : " " * prefix) + str[start ... nextt]
644 ## instance_eval but with ability to handle block arguments
645 ## thanks to why: http://redhanded.hobix.com/inspect/aBlockCostume.html
647 (class << self; self; end).class_eval do
648 define_method :cloaker_, &b
649 meth = instance_method :cloaker_
650 remove_method :cloaker_
656 ## The top-level entry method into Trollop. Creates a Parser object,
657 ## passes the block to it, then parses +args+ with it, handling any
658 ## errors or requests for help or version information appropriately (and
659 ## then exiting). Modifies +args+ in place. Returns a hash of option
662 ## The block passed in should contain zero or more calls to +opt+
663 ## (Parser#opt), zero or more calls to +text+ (Parser#text), and
664 ## probably a call to +version+ (Parser#version).
666 ## The returned block contains a value for every option specified with
667 ## +opt+. The value will be the value given on the commandline, or the
668 ## default value if the option was not specified on the commandline. For
669 ## every option specified on the commandline, a key "<option
670 ## name>_given" will also be set in the hash.
675 ## opts = Trollop::options do
676 ## opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
677 ## opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
678 ## opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
679 ## opt :num_thumbs, "Number of thumbs", :type => :int # an integer --num-thumbs <i>, defaulting to nil
682 ## ## if called with no arguments
683 ## p opts # => { :monkey => false, :goat => true, :num_limbs => 4, :num_thumbs => nil }
685 ## ## if called with --monkey
686 ## p opts # => {:monkey_given=>true, :monkey=>true, :goat=>true, :num_limbs=>4, :help=>false, :num_thumbs=>nil}
688 ## See more examples at http://trollop.rubyforge.org.
689 def options args = ARGV, *a, &b
690 @p = Parser.new(*a, &b)
694 @p.leftovers.each { |l| args << l }
696 rescue CommandlineError => e
697 $stderr.puts "Error: #{e.message}."
698 $stderr.puts "Try --help for help."
709 ## Informs the user that their usage of 'arg' was wrong, as detailed by
710 ## 'msg', and dies. Example:
713 ## opt :volume, :default => 0.0
716 ## die :volume, "too loud" if opts[:volume] > 10.0
717 ## die :volume, "too soft" if opts[:volume] < 0.1
719 ## In the one-argument case, simply print that message, a notice
720 ## about -h, and die. Example:
723 ## opt :whatever # ...
726 ## Trollop::die "need at least one filename" if ARGV.empty?
729 $stderr.puts "Error: argument --#{@p.specs[arg][:long]} #{msg}."
731 $stderr.puts "Error: #{arg}."
733 $stderr.puts "Try --help for help."
737 module_function :options, :die