class Optimist::Option
Attributes
Public Class Methods
Source
# 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
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.
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
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
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
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
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
Source
# 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
Provide a way to register symbol aliases to the Parser
Public Instance Methods
Source
# File lib/optimist.rb, line 614 def array_default? ; self.default.kind_of?(Array) ; end
note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.
Source
# 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
Format the educate-line description including the default-value(s)
Source
# File lib/optimist.rb, line 630 def educate (short? ? "-#{short}, " : "") + "--#{long}" + type_format + (flag? && default ? ", --no-#{long}" : "") end
Source
# File lib/optimist.rb, line 602 def flag? ; false ; end
Indicates a flag option, which is an option without an argument
Source
# File lib/optimist.rb, line 611 def multi_arg? ; false ; end
Indicates that this is a multivalued (Array type) argument
Source
# File lib/optimist.rb, line 623 def parse(_paramlist, _neg_given) raise NotImplementedError, "parse must be overridden for newly registered type" end
Source
# File lib/optimist.rb, line 603 def single_arg? !self.multi_arg? && !self.flag? end
Source
# File lib/optimist.rb, line 628 def type_format ; "" ; end
provide type-format string. default to empty, but user should probably override it