Class: Rzo::Trollop::Option
- Inherits:
-
Object
- Object
- Rzo::Trollop::Option
- Defined in:
- lib/rzo/trollop.rb
Overview
The option for each flag
Constant Summary collapse
- FLAG_TYPES =
The set of values that indicate a flag option when passed as the +:type+ parameter of #opt.
[:flag, :bool, :boolean]
- SINGLE_ARG_TYPES =
The set of values that indicate a single-parameter (normal) option when passed as the +:type+ parameter of #opt.
A value of +io+ corresponds to a readable IO resource, including a filename, URI, or the strings 'stdin' or '-'.
[:int, :integer, :string, :double, :float, :io, :date]
- MULTI_ARG_TYPES =
The set of values that indicate a multiple-parameter option (i.e., that takes multiple space-separated values on the commandline) when passed as the +:type+ parameter of #opt.
[:ints, :integers, :strings, :doubles, :floats, :ios, :dates]
- TYPES =
The complete set of legal values for the +:type+ parameter of #opt.
FLAG_TYPES + SINGLE_ARG_TYPES + MULTI_ARG_TYPES
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#opts ⇒ Object
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
-
#array_default? ⇒ Boolean
? def multi_default ; opts.default || opts.multi && [] ; end.
- #callback ⇒ Object
- #default ⇒ Object
- #desc ⇒ Object
- #flag? ⇒ Boolean
-
#initialize(name, desc = "", opts = {}, &b) ⇒ Option
constructor
A new instance of Option.
- #key?(name) ⇒ Boolean
- #long ⇒ Object
- #multi ⇒ Object (also: #multi?)
- #multi_arg? ⇒ Boolean
- #required? ⇒ Boolean
- #short ⇒ Object
-
#short=(val) ⇒ Object
not thrilled about this.
- #short? ⇒ Boolean
- #single_arg? ⇒ Boolean
- #type ⇒ Object
Constructor Details
#initialize(name, desc = "", opts = {}, &b) ⇒ Option
Returns a new instance of Option.
665 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 695 696 697 698 699 700 701 702 703 704 705 706 707 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 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
# File 'lib/rzo/trollop.rb', line 665 def initialize(name, desc="", opts={}, &b) ## fill in :type opts[:type] = # normalize case opts[:type] when :boolean, :bool then :flag when :integer then :int when :integers then :ints when :double then :float when :doubles then :floats when Class case opts[:type].name when 'TrueClass', 'FalseClass' then :flag when 'String' then :string when 'Integer' then :int when 'Float' then :float when 'IO' then :io when 'Date' then :date else raise ArgumentError, "unsupported argument type '#{opts[:type].class.name}'" end when nil then nil else raise ArgumentError, "unsupported argument type '#{opts[:type]}'" unless TYPES.include?(opts[:type]) opts[:type] end ## 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].kind_of?(Array) && !opts[:type] opts[:default].first else opts[:default] end type_from_default = case disambiguated_default when Integer then :int when Numeric then :float when TrueClass, FalseClass then :flag when String then :string when IO then :io when Date then :date when Array if opts[:default].empty? if opts[:type] raise ArgumentError, "multiple argument type must be plural" unless MULTI_ARG_TYPES.include?(opts[:type]) nil else raise ArgumentError, "multiple argument type cannot be deduced from an empty array for '#{opts[:default][0].class.name}'" end else case opts[:default][0] # the first element determines the types when Integer then :ints when Numeric then :floats when String then :strings when IO then :ios when Date then :dates else raise ArgumentError, "unsupported multiple argument type '#{opts[:default][0].class.name}'" end end when nil then nil else raise ArgumentError, "unsupported argument type '#{opts[:default].class.name}'" end raise ArgumentError, ":type specification and default type don't match (default type is #{type_from_default})" if opts[:type] && type_from_default && opts[:type] != type_from_default opts[:type] = opts[:type] || type_from_default || :flag ## fill in :long opts[:long] = opts[:long] ? opts[:long].to_s : name.to_s.gsub("_", "-") opts[:long] = case opts[:long] when /^--([^-].*)$/ then $1 when /^[^-]/ then opts[:long] else raise ArgumentError, "invalid long option name #{opts[:long].inspect}" end ## fill in :short opts[:short] = opts[:short].to_s if opts[:short] && opts[:short] != :none opts[:short] = case opts[:short] when /^-(.)$/ then $1 when nil, :none, /^.$/ then opts[:short] else raise ArgumentError, "invalid short option name '#{opts[:short].inspect}'" end if opts[:short] raise ArgumentError, "a short option name can't be a number or a dash" if opts[:short] =~ Trollop::Parser::INVALID_SHORT_ARG_REGEX end ## fill in :default for flags opts[:default] = false if opts[:type] == :flag && opts[:default].nil? ## autobox :default for :multi (multi-occurrence) arguments opts[:default] = [opts[:default]] if opts[:default] && opts[:multi] && !opts[:default].kind_of?(Array) ## fill in :multi opts[:multi] ||= false self.name = name self.opts = opts end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
663 664 665 |
# File 'lib/rzo/trollop.rb', line 663 def name @name end |
#opts ⇒ Object
Returns the value of attribute opts.
663 664 665 |
# File 'lib/rzo/trollop.rb', line 663 def opts @opts end |
Class Method Details
.create(name, desc = "", opts = {}) ⇒ Object
803 804 805 |
# File 'lib/rzo/trollop.rb', line 803 def self.create(name, desc="", opts={}) new(name, desc, opts) end |
Instance Method Details
#array_default? ⇒ Boolean
? def multi_default ; opts.default || opts.multi && [] ; end
791 |
# File 'lib/rzo/trollop.rb', line 791 def array_default? ; opts[:default].kind_of?(Array) ; end |
#callback ⇒ Object
798 |
# File 'lib/rzo/trollop.rb', line 798 def callback ; opts[:callback] ; end |
#default ⇒ Object
789 |
# File 'lib/rzo/trollop.rb', line 789 def default ; opts[:default] ; end |
#desc ⇒ Object
799 |
# File 'lib/rzo/trollop.rb', line 799 def desc ; opts[:desc] ; end |
#flag? ⇒ Boolean
777 |
# File 'lib/rzo/trollop.rb', line 777 def flag? ; type == :flag ; end |
#key?(name) ⇒ Boolean
772 773 774 |
# File 'lib/rzo/trollop.rb', line 772 def key?(name) opts.key?(name) end |
#long ⇒ Object
797 |
# File 'lib/rzo/trollop.rb', line 797 def long ; opts[:long] ; end |
#multi ⇒ Object Also known as: multi?
782 |
# File 'lib/rzo/trollop.rb', line 782 def multi ; opts[:multi] ; end |
#multi_arg? ⇒ Boolean
785 786 787 |
# File 'lib/rzo/trollop.rb', line 785 def multi_arg? MULTI_ARG_TYPES.include?(type) end |
#required? ⇒ Boolean
801 |
# File 'lib/rzo/trollop.rb', line 801 def required? ; opts[:required] ; end |
#short ⇒ Object
793 |
# File 'lib/rzo/trollop.rb', line 793 def short ; opts[:short] ; end |
#short=(val) ⇒ Object
not thrilled about this
796 |
# File 'lib/rzo/trollop.rb', line 796 def short=(val) ; opts[:short] = val ; end |
#short? ⇒ Boolean
794 |
# File 'lib/rzo/trollop.rb', line 794 def short? ; short && short != :none ; end |
#single_arg? ⇒ Boolean
778 779 780 |
# File 'lib/rzo/trollop.rb', line 778 def single_arg? SINGLE_ARG_TYPES.include?(type) end |
#type ⇒ Object
776 |
# File 'lib/rzo/trollop.rb', line 776 def type ; opts[:type] ; end |