Class: Optimist::Option
- Inherits:
-
Object
- Object
- Optimist::Option
- Defined in:
- lib/rbcli/util/optimist.rb
Direct Known Subclasses
BooleanOption, DateOption, FloatOption, IOOption, IntegerOption, StringOption
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#long ⇒ Object
Returns the value of attribute long.
-
#multi_given ⇒ Object
writeonly
Sets the attribute multi_given.
-
#name ⇒ Object
Returns the value of attribute name.
-
#permitted ⇒ Object
Returns the value of attribute permitted.
-
#short ⇒ Object
Returns the value of attribute short.
Class Method Summary collapse
-
.create(name, desc = "", opts = {}, settings = {}) ⇒ Object
Determines which type of object to create based on arguments passed to
Optimist::opt. -
.register_alias(*alias_keys) ⇒ Object
Provide a way to register symbol aliases to the Parser.
Instance Method Summary collapse
-
#array_default? ⇒ Boolean
note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.
- #callback ⇒ Object
- #desc ⇒ Object
- #educate ⇒ Object
-
#flag? ⇒ Boolean
Indicates a flag option, which is an option without an argument.
-
#full_description ⇒ Object
Format the educate-line description including the default and permitted value(s).
-
#initialize ⇒ Option
constructor
A new instance of Option.
- #multi ⇒ Object (also: #multi?)
-
#multi_arg? ⇒ Boolean
Indicates that this is a multivalued (Array type) argument.
- #opts(key) ⇒ Object
- #opts=(o) ⇒ Object
- #parse(_paramlist, _neg_given) ⇒ Object
- #required? ⇒ Boolean
- #short? ⇒ Boolean
- #single_arg? ⇒ Boolean
-
#type_format ⇒ Object
provide type-format string.
Constructor Details
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
601 602 603 |
# File 'lib/rbcli/util/optimist.rb', line 601 def default @default end |
#long ⇒ Object
Returns the value of attribute long.
601 602 603 |
# File 'lib/rbcli/util/optimist.rb', line 601 def long @long end |
#multi_given=(value) ⇒ Object (writeonly)
Sets the attribute multi_given
602 603 604 |
# File 'lib/rbcli/util/optimist.rb', line 602 def multi_given=(value) @multi_given = value end |
#name ⇒ Object
Returns the value of attribute name.
601 602 603 |
# File 'lib/rbcli/util/optimist.rb', line 601 def name @name end |
#permitted ⇒ Object
Returns the value of attribute permitted.
601 602 603 |
# File 'lib/rbcli/util/optimist.rb', line 601 def permitted @permitted end |
#short ⇒ Object
Returns the value of attribute short.
601 602 603 |
# File 'lib/rbcli/util/optimist.rb', line 601 def short @short end |
Class Method Details
.create(name, desc = "", opts = {}, settings = {}) ⇒ Object
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.
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 |
# File 'lib/rbcli/util/optimist.rb', line 708 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 ## fill in permitted values permitted = opts[:permitted] || nil ## autobox :default for :multi (multi-occurrence) arguments defvalue = [defvalue] if defvalue && multi_given && !defvalue.kind_of?(Array) opt_inst.permitted = permitted opt_inst.default = defvalue opt_inst.name = name opt_inst.opts = opts opt_inst end |
.register_alias(*alias_keys) ⇒ Object
Provide a way to register symbol aliases to the Parser
695 696 697 698 699 700 |
# File 'lib/rbcli/util/optimist.rb', line 695 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 |
Instance Method Details
#array_default? ⇒ Boolean
note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.
636 |
# File 'lib/rbcli/util/optimist.rb', line 636 def array_default? ; self.default.kind_of?(Array) ; end |
#callback ⇒ Object
640 |
# File 'lib/rbcli/util/optimist.rb', line 640 def callback ; opts(:callback) ; end |
#desc ⇒ Object
641 |
# File 'lib/rbcli/util/optimist.rb', line 641 def desc ; opts(:desc) ; end |
#educate ⇒ Object
652 653 654 |
# File 'lib/rbcli/util/optimist.rb', line 652 def educate (short? ? "-#{short}, " : " ") + "--#{long}" + type_format + (flag? && default ? ", --no-#{long}" : "") end |
#flag? ⇒ Boolean
Indicates a flag option, which is an option without an argument
624 |
# File 'lib/rbcli/util/optimist.rb', line 624 def flag? ; false ; end |
#full_description ⇒ Object
Format the educate-line description including the default and permitted value(s)
657 658 659 660 661 662 |
# File 'lib/rbcli/util/optimist.rb', line 657 def full_description desc_str = desc desc_str += default_description_str(desc) if default desc_str += permitted_description_str(desc) if permitted desc_str end |
#multi ⇒ Object Also known as: multi?
629 |
# File 'lib/rbcli/util/optimist.rb', line 629 def multi ; @multi_given ; end |
#multi_arg? ⇒ Boolean
Indicates that this is a multivalued (Array type) argument
633 |
# File 'lib/rbcli/util/optimist.rb', line 633 def multi_arg? ; false ; end |
#opts(key) ⇒ Object
615 616 617 |
# File 'lib/rbcli/util/optimist.rb', line 615 def opts(key) @optshash[key] end |
#opts=(o) ⇒ Object
619 620 621 |
# File 'lib/rbcli/util/optimist.rb', line 619 def opts=(o) @optshash = o end |
#parse(_paramlist, _neg_given) ⇒ Object
645 646 647 |
# File 'lib/rbcli/util/optimist.rb', line 645 def parse(_paramlist, _neg_given) raise NotImplementedError, "parse must be overridden for newly registered type" end |
#required? ⇒ Boolean
643 |
# File 'lib/rbcli/util/optimist.rb', line 643 def required? ; opts(:required) ; end |
#short? ⇒ Boolean
638 |
# File 'lib/rbcli/util/optimist.rb', line 638 def short? ; short && short != :none ; end |
#single_arg? ⇒ Boolean
625 626 627 |
# File 'lib/rbcli/util/optimist.rb', line 625 def single_arg? !self.multi_arg? && !self.flag? end |
#type_format ⇒ Object
provide type-format string. default to empty, but user should probably override it
650 |
# File 'lib/rbcli/util/optimist.rb', line 650 def type_format ; "" ; end |