class Optimist::Option
Attributes
default[RW]
long[RW]
multi_given[W]
name[RW]
short[RW]
Public Class Methods
create(name, desc="", opts={}, settings={})
click to toggle source
Determines which type of object to create based on arguments passed to Optimist::opt
. This is trickier in Optimist
, than other cmdline parsers (e.g. Slop) because we allow the default:
to be able to set the option’s type.
# File lib/optimist.rb, line 664 def self.create(name, desc="", opts={}, settings={}) opttype = Optimist::Parser.registry_getopttype(opts[:type]) opttype_from_default = get_klass_from_default(opts, opttype) raise ArgumentError, ":type specification and default type don't match (default type is #{opttype_from_default.class})" if opttype && opttype_from_default && (opttype.class != opttype_from_default.class) opt_inst = (opttype || opttype_from_default || Optimist::BooleanOption.new) ## fill in :long opt_inst.long = handle_long_opt(opts[:long], name) ## fill in :short opt_inst.short = handle_short_opt(opts[:short]) ## fill in :multi multi_given = opts[:multi] || false opt_inst.multi_given = multi_given ## fill in :default for flags defvalue = opts[:default] || opt_inst.default ## autobox :default for :multi (multi-occurrence) arguments defvalue = [defvalue] if defvalue && multi_given && !defvalue.kind_of?(Array) opt_inst.default = defvalue opt_inst.name = name opt_inst.opts = opts opt_inst end
new()
click to toggle source
# File lib/optimist.rb, line 583 def initialize @long = nil @short = nil @name = nil @multi_given = false @hidden = false @default = nil @optshash = Hash.new() end
register_alias(*alias_keys)
click to toggle source
Provide a way to register symbol aliases to the Parser
# File lib/optimist.rb, line 651 def self.register_alias(*alias_keys) alias_keys.each do |alias_key| # pass in the alias-key and the class Parser.register(alias_key, self) end end
Private Class Methods
get_klass_from_default(opts, opttype)
click to toggle source
# File lib/optimist.rb, line 709 def self.get_klass_from_default(opts, opttype) ## for options with :multi => true, an array default doesn't imply ## a multi-valued argument. for that you have to specify a :type ## as well. (this is how we disambiguate an ambiguous situation; ## see the docs for Parser#opt for details.) disambiguated_default = if opts[:multi] && opts[:default].is_a?(Array) && opttype.nil? opts[:default].first else opts[:default] end return nil if disambiguated_default.nil? type_from_default = get_type_from_disdef(opts[:default], opttype, disambiguated_default) return Optimist::Parser.registry_getopttype(type_from_default) end
get_type_from_disdef(optdef, opttype, disambiguated_default)
click to toggle source
# File lib/optimist.rb, line 696 def self.get_type_from_disdef(optdef, opttype, disambiguated_default) if disambiguated_default.is_a? Array return(optdef.first.class.name.downcase + "s") if !optdef.empty? if opttype raise ArgumentError, "multiple argument type must be plural" unless opttype.multi_arg? return nil else raise ArgumentError, "multiple argument type cannot be deduced from an empty array" end end return disambiguated_default.class.name.downcase end
handle_long_opt(lopt, name)
click to toggle source
# File lib/optimist.rb, line 726 def self.handle_long_opt(lopt, name) lopt = lopt ? lopt.to_s : name.to_s.gsub("_", "-") lopt = case lopt when /^--([^-].*)$/ then $1 when /^[^-]/ then lopt else raise ArgumentError, "invalid long option name #{lopt.inspect}" end end
handle_short_opt(sopt)
click to toggle source
# File lib/optimist.rb, line 735 def self.handle_short_opt(sopt) sopt = sopt.to_s if sopt && sopt != :none sopt = case sopt when /^-(.)$/ then $1 when nil, :none, /^.$/ then sopt else raise ArgumentError, "invalid short option name '#{sopt.inspect}'" end if sopt raise ArgumentError, "a short option name can't be a number or a dash" if sopt =~ ::Optimist::Parser::INVALID_SHORT_ARG_REGEX end return sopt end
Public Instance Methods
array_default?()
click to toggle source
note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.
# File lib/optimist.rb, line 614 def array_default? ; self.default.kind_of?(Array) ; end
callback()
click to toggle source
# File lib/optimist.rb, line 618 def callback ; opts(:callback) ; end
desc()
click to toggle source
# File lib/optimist.rb, line 619 def desc ; opts(:desc) ; end
description_with_default()
click to toggle source
Format the educate-line description including the default-value(s)
# File lib/optimist.rb, line 635 def description_with_default return desc unless default default_s = case default when $stdout then '<stdout>' when $stdin then '<stdin>' when $stderr then '<stderr>' when Array default.join(', ') else default.to_s end defword = desc.end_with?('.') ? 'Default' : 'default' return "#{desc} (#{defword}: #{default_s})" end
educate()
click to toggle source
# File lib/optimist.rb, line 630 def educate (short? ? "-#{short}, " : "") + "--#{long}" + type_format + (flag? && default ? ", --no-#{long}" : "") end
flag?()
click to toggle source
Indicates a flag option, which is an option without an argument
# File lib/optimist.rb, line 602 def flag? ; false ; end
multi()
click to toggle source
# File lib/optimist.rb, line 607 def multi ; @multi_given ; end
Also aliased as: multi?
multi_arg?()
click to toggle source
Indicates that this is a multivalued (Array type) argument
# File lib/optimist.rb, line 611 def multi_arg? ; false ; end
opts(key)
click to toggle source
# File lib/optimist.rb, line 593 def opts(key) @optshash[key] end
opts=(o)
click to toggle source
# File lib/optimist.rb, line 597 def opts=(o) @optshash = o end
parse(_paramlist, _neg_given)
click to toggle source
# File lib/optimist.rb, line 623 def parse(_paramlist, _neg_given) raise NotImplementedError, "parse must be overridden for newly registered type" end
required?()
click to toggle source
# File lib/optimist.rb, line 621 def required? ; opts(:required) ; end
short?()
click to toggle source
# File lib/optimist.rb, line 616 def short? ; short && short != :none ; end
single_arg?()
click to toggle source
# File lib/optimist.rb, line 603 def single_arg? !self.multi_arg? && !self.flag? end
type_format()
click to toggle source
provide type-format string. default to empty, but user should probably override it
# File lib/optimist.rb, line 628 def type_format ; "" ; end