Class: Trollop::Option
- Inherits:
-
Object
- Object
- Trollop::Option
- Defined in:
- lib/trollop.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.
-
#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
Trollop::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
-
#description_with_default ⇒ Object
Format the educate-line description including the default-value(s).
- #educate ⇒ Object
-
#flag? ⇒ Boolean
Indicates a flag option, which is an option without an argument.
-
#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
#initialize ⇒ Option
Returns a new instance of Option.
585 586 587 588 589 590 591 592 593 |
# File 'lib/trollop.rb', line 585 def initialize @long = nil @short = nil @name = nil @multi_given = false @hidden = false @default = nil @optshash = Hash.new() end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
582 583 584 |
# File 'lib/trollop.rb', line 582 def default @default end |
#long ⇒ Object
Returns the value of attribute long.
582 583 584 |
# File 'lib/trollop.rb', line 582 def long @long end |
#multi_given=(value) ⇒ Object (writeonly)
Sets the attribute multi_given
583 584 585 |
# File 'lib/trollop.rb', line 583 def multi_given=(value) @multi_given = value end |
#name ⇒ Object
Returns the value of attribute name.
582 583 584 |
# File 'lib/trollop.rb', line 582 def name @name end |
#short ⇒ Object
Returns the value of attribute short.
582 583 584 |
# File 'lib/trollop.rb', line 582 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 Trollop::opt
. This is trickier in Trollop, than other cmdline parsers (e.g. Slop) because we allow the default:
to be able to set the option’s type.
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/trollop.rb', line 666 def self.create(name, desc="", opts={}, settings={}) opttype = Trollop::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 || Trollop::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 |
Instance Method Details
#array_default? ⇒ Boolean
note: Option-Types with both multi_arg? and flag? false are single-parameter (normal) options.
616 |
# File 'lib/trollop.rb', line 616 def array_default? ; self.default.kind_of?(Array) ; end |
#callback ⇒ Object
620 |
# File 'lib/trollop.rb', line 620 def callback ; opts(:callback) ; end |
#desc ⇒ Object
621 |
# File 'lib/trollop.rb', line 621 def desc ; opts(:desc) ; end |
#description_with_default ⇒ Object
Format the educate-line description including the default-value(s)
637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'lib/trollop.rb', line 637 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 ⇒ Object
632 633 634 |
# File 'lib/trollop.rb', line 632 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
604 |
# File 'lib/trollop.rb', line 604 def flag? ; false ; end |
#multi ⇒ Object Also known as: multi?
609 |
# File 'lib/trollop.rb', line 609 def multi ; @multi_given ; end |
#multi_arg? ⇒ Boolean
Indicates that this is a multivalued (Array type) argument
613 |
# File 'lib/trollop.rb', line 613 def multi_arg? ; false ; end |
#opts(key) ⇒ Object
595 596 597 |
# File 'lib/trollop.rb', line 595 def opts (key) @optshash[key] end |
#opts=(o) ⇒ Object
599 600 601 |
# File 'lib/trollop.rb', line 599 def opts= (o) @optshash = o end |
#parse(_paramlist, _neg_given) ⇒ Object
625 626 627 |
# File 'lib/trollop.rb', line 625 def parse (_paramlist, _neg_given) raise NotImplementedError, "parse must be overridden for newly registered type" end |
#required? ⇒ Boolean
623 |
# File 'lib/trollop.rb', line 623 def required? ; opts(:required) ; end |
#short? ⇒ Boolean
618 |
# File 'lib/trollop.rb', line 618 def short? ; short && short != :none ; end |
#single_arg? ⇒ Boolean
605 606 607 |
# File 'lib/trollop.rb', line 605 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
630 |
# File 'lib/trollop.rb', line 630 def type_format ; "" ; end |