class ArgParser
Constants
- ERR_MANIFEST_EXPECTED
- ERR_MULTIPLE_INPUTS
- ERR_OPTION_NULL
- ERR_REQUIRED
- ERR_UNIQUE_NAME
- OPTS_RESERVED
These options don’t display their synopsis and given for free unless explicitly specified in the manifest.
- OPT_ENOUGH
- OUT_ARGUMENTS
- OUT_BUGS
- OUT_COPYRIGHT
- OUT_HOMEPAGE
- OUT_LICENSE
- OUT_OPTIONS
- OUT_VERSION
Output templates used for the
terminate(0)
call- TRM_EXPECTED
- TRM_INVALID_OPTION
- TRM_OPTION_ARGUMENT_EXPECTED
- TRM_UNEXPECTED_ARGUMENT
Templates used for the terminate(2) call
- TRM_UNKNOWN
- VERSION
Attributes
arguments[R]
bugs[R]
copyright[R]
help[R]
homepage[R]
info[R]
license[R]
options[R]
package[R]
program[R]
synopsis[R]
version[R]
Public Class Methods
manifest()
click to toggle source
# File lib/argparser/arg_parser.rb, line 52 def manifest @@manifest ||= {} end
manifest=(manifest)
click to toggle source
# File lib/argparser/arg_parser.rb, line 48 def manifest=(manifest) @@manifest = manifest end
new(manifest)
click to toggle source
# File lib/argparser/arg_parser.rb, line 74 def initialize(manifest) hash2vars(ArgParser.manifest.merge(manifest)) @arguments ||= [] @options ||= [] @options = @options.map do |o| if o.kind_of?(Option) o else if o['input'] || o[:input] o[:name] ||= o['names'] || o[:names] @arguments << o nil else o[:param] ||= (o['argument'] || o[:argument]) Option.new(o) end end end.compact @arguments = @arguments.map {|o| o.kind_of?(Argument) ? o : Argument.new(o)} _check_manifest end
Public Instance Methods
[](name)
click to toggle source
Returns option by any of its names given
# File lib/argparser/arg_parser.rb, line 58 def [](name) get_argument(name) || get_option(name) end
all()
click to toggle source
# File lib/argparser/arg_parser.rb, line 62 def all options + arguments end
get_argument(name)
click to toggle source
# File lib/argparser/arg_parser.rb, line 66 def get_argument(name) arguments.find{|a| a.name == name} end
get_option(name)
click to toggle source
# File lib/argparser/arg_parser.rb, line 70 def get_option(name) options.find{|o| o.names.include?(name)} end
on_exit(code, message)
click to toggle source
# File lib/argparser/arg_parser.rb, line 146 def on_exit(code, message) (code == 0 ? $stdout : $stderr).print(message) exit(code) end
parse(argv = ARGV) { |name, value| ... }
click to toggle source
Uses ARGV by default, but you may supply your own arguments It exits if bad arguments given or they aren’t validated.
# File lib/argparser/arg_parser.rb, line 98 def parse(argv = ARGV) all.each(&:reset) _check_manifest OPTS_RESERVED.each do |res| next if !argv.include?("--#{res[:name]}") || self[res[:name]] terminate(0, send(res[:func])) end args = argv.dup enough = false while (a = args.shift) if a == OPT_ENOUGH enough = true elsif enough || (a =~ /^[^-]/) || (a == '-') _set_argument(a) elsif a =~ /^--(.+)/ _set_long_option($1, args) elsif a =~ /^-([^-].*)/ _set_short_options($1, args) else terminate(2, TRM_UNKNOWN % a) end end all.each { |o| o.set_default terminate(2, (TRM_EXPECTED % o.name)) if o.required && !o.value? } all.each { |o| terminate(2, TRM_INVALID_OPTION % o.name) unless o.valid?(self) } all.select(&:value?).each {|o| yield(o.name, o.value)} if block_given? self end
Also aliased as: parse!
printed_help()
click to toggle source
# File lib/argparser/arg_parser.rb, line 160 def printed_help str = StringIO.new str.puts(printed_synopsis) str.puts(info) if info if help str.puts(help) else unless options.empty? str.puts(OUT_OPTIONS) options.each {|o| str.puts(o.printed_help)} end OPTS_RESERVED.each do |res| r = Option.new(res) next if get_argument(r.name) str.puts(r.printed_help) end unless arguments.empty? str.puts(OUT_ARGUMENTS) arguments.each {|a| str.puts(a.printed_help)} end end str.puts(OUT_BUGS % bugs) if bugs str.puts(OUT_HOMEPAGE % [(package||program), homepage]) if homepage str.string end
printed_synopsis()
click to toggle source
# File lib/argparser/arg_parser.rb, line 186 def printed_synopsis "Usage: #{program} #{synopsis || all.map{|o| o.synopsis}.join(' ')}" end
printed_version()
click to toggle source
# File lib/argparser/arg_parser.rb, line 151 def printed_version str = StringIO.new pk = (pk = @package) ? " (#{pk})" : '' str.puts(OUT_VERSION % [program, pk, version]) str.puts(OUT_COPYRIGHT % copyright) if copyright str.puts(OUT_LICENSE % license) if license str.string end
terminate(code, str)
click to toggle source
# File lib/argparser/arg_parser.rb, line 139 def terminate(code, str) s = StringIO.new s.puts(printed_synopsis) if code != 0 s.puts(str[-1] == "\n" ? str.chop : str) on_exit(code, s.string) end
Private Instance Methods
_check_manifest()
click to toggle source
# File lib/argparser/arg_parser.rb, line 192 def _check_manifest {:program => program, :version => version}.each do |k, v| raise ManifestError, (ERR_MANIFEST_EXPECTED % k) if v.to_s.strip.empty? end arguments[0..-2].each do |i| raise ManifestError, (ERR_MULTIPLE_INPUTS % i.name) if i.multiple end opt = arguments.index{|i| !i.required} || arguments.size req = arguments.rindex{|i| i.required} || 0 raise ManifestError, (ERR_REQUIRED % arguments[req].name) if req > opt names = all.map(&:names).flatten raise ManifestError, ERR_UNIQUE_NAME if names.size != names.uniq.size end
_set_argument(a)
click to toggle source
# File lib/argparser/arg_parser.rb, line 208 def _set_argument(a) terminate(2, TRM_UNEXPECTED_ARGUMENT % a) unless (input = arguments.find{|i| !i.value || i.multiple}) input.add_value(a) end
_set_long_option(a, tail)
click to toggle source
# File lib/argparser/arg_parser.rb, line 214 def _set_long_option(a, tail) terminate(2, TRM_UNKNOWN % a) unless a.size > 1 && (o = get_option(a)) terminate(2, TRM_OPTION_ARGUMENT_EXPECTED % a) if o.param && tail.empty? o.add_value(o.param ? tail.shift : true) end
_set_short_options(a, tail)
click to toggle source
# File lib/argparser/arg_parser.rb, line 220 def _set_short_options(a, tail) a.chars.each_with_index do |char, index| terminate(2, TRM_UNKNOWN % char) unless (option = get_option(char)) if !option.param option.add_value(true) elsif a.size-1 == index terminate(2, TRM_OPTION_ARGUMENT_EXPECTED % char) if tail.empty? option.add_value(tail.shift) else option.add_value(a[index+1..-1]) break end end end