Class: Optimist::Option
- Inherits:
-
Object
- Object
- Optimist::Option
- Defined in:
- lib/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.
-
#permitted_response ⇒ Object
Returns the value of attribute permitted_response.
-
#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
- #doesnt_need_autogen_short ⇒ Object
- #educate ⇒ Object
-
#flag? ⇒ Boolean
Indicates a flag option, which is an option without an argument.
-
#format_stdio(obj) ⇒ Object
Format stdio like objects to a string.
-
#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
- #permitted_type_valid? ⇒ Boolean
- #permitted_valid_string ⇒ Object
-
#permitted_value?(val) ⇒ Boolean
incoming values from the command-line should be strings, so we should stringify any permitted types as the basis of comparison.
- #required? ⇒ Boolean
- #single_arg? ⇒ Boolean
-
#type_format ⇒ Object
provide type-format string.
- #validate_permitted(arg, value) ⇒ Object
Constructor Details
#initialize ⇒ Option
Returns a new instance of Option.
770 771 772 773 774 775 776 777 778 779 780 |
# File 'lib/optimist.rb', line 770 def initialize @long = LongNames.new @short = ShortNames.new # can be an Array of one-char strings, a one-char String, nil or :none @name = nil @multi_given = false @hidden = false @default = nil @permitted = nil @permitted_response = "option '%{arg}' only accepts %{valid_string}" @optshash = Hash.new() end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
767 768 769 |
# File 'lib/optimist.rb', line 767 def default @default end |
#long ⇒ Object
Returns the value of attribute long.
767 768 769 |
# File 'lib/optimist.rb', line 767 def long @long end |
#multi_given=(value) ⇒ Object (writeonly)
Sets the attribute multi_given
768 769 770 |
# File 'lib/optimist.rb', line 768 def multi_given=(value) @multi_given = value end |
#name ⇒ Object
Returns the value of attribute name.
767 768 769 |
# File 'lib/optimist.rb', line 767 def name @name end |
#permitted ⇒ Object
Returns the value of attribute permitted.
767 768 769 |
# File 'lib/optimist.rb', line 767 def permitted @permitted end |
#permitted_response ⇒ Object
Returns the value of attribute permitted_response.
767 768 769 |
# File 'lib/optimist.rb', line 767 def permitted_response @permitted_response end |
#short ⇒ Object
Returns the value of attribute short.
767 768 769 |
# File 'lib/optimist.rb', line 767 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.
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'lib/optimist.rb', line 926 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.set(name, opts[:long], opts[:alt]) ## fill in :short opt_inst.short.add 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.permitted_response = opts[:permitted_response] if opts[:permitted_response] 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.
803 |
# File 'lib/optimist.rb', line 803 def array_default? ; self.default.kind_of?(Array) ; end |
#callback ⇒ Object
807 |
# File 'lib/optimist.rb', line 807 def callback ; opts(:callback) ; end |
#desc ⇒ Object
808 |
# File 'lib/optimist.rb', line 808 def desc ; opts(:desc) ; end |
#doesnt_need_autogen_short ⇒ Object
805 |
# File 'lib/optimist.rb', line 805 def doesnt_need_autogen_short ; !short.auto || short.chars.any? ; end |
#educate ⇒ Object
819 820 821 822 823 824 |
# File 'lib/optimist.rb', line 819 def educate optionlist = [] optionlist.concat(short.chars.map { |o| "-#{o}" }) optionlist.concat(long.names.map { |o| "--#{o}" }) optionlist.compact.join(', ') + type_format + (flag? && default ? ", --no-#{long}" : "") end |
#flag? ⇒ Boolean
Indicates a flag option, which is an option without an argument
791 |
# File 'lib/optimist.rb', line 791 def flag? ; false ; end |
#format_stdio(obj) ⇒ Object
Format stdio like objects to a string
835 836 837 838 839 840 841 842 |
# File 'lib/optimist.rb', line 835 def format_stdio(obj) case obj when $stdout then '<stdout>' when $stdin then '<stdin>' when $stderr then '<stderr>' else obj # pass-through-case end end |
#full_description ⇒ Object
Format the educate-line description including the default and permitted value(s)
827 828 829 830 831 832 |
# File 'lib/optimist.rb', line 827 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?
796 |
# File 'lib/optimist.rb', line 796 def multi ; @multi_given ; end |
#multi_arg? ⇒ Boolean
Indicates that this is a multivalued (Array type) argument
800 |
# File 'lib/optimist.rb', line 800 def multi_arg? ; false ; end |
#opts(key) ⇒ Object
782 783 784 |
# File 'lib/optimist.rb', line 782 def opts(key) @optshash[key] end |
#opts=(o) ⇒ Object
786 787 788 |
# File 'lib/optimist.rb', line 786 def opts=(o) @optshash = o end |
#parse(_paramlist, _neg_given) ⇒ Object
812 813 814 |
# File 'lib/optimist.rb', line 812 def parse(_paramlist, _neg_given) raise NotImplementedError, "parse must be overridden for newly registered type" end |
#permitted_type_valid? ⇒ Boolean
868 869 870 871 872 873 |
# File 'lib/optimist.rb', line 868 def permitted_type_valid? case permitted when NilClass, Array, Range, Regexp then true else false end end |
#permitted_valid_string ⇒ Object
856 857 858 859 860 861 862 863 864 865 866 |
# File 'lib/optimist.rb', line 856 def permitted_valid_string case permitted when Array return "one of: " + permitted.to_a.map(&:to_s).join(', ') when Range return "value in range of: #{permitted}" when Regexp return "value matching: #{permitted.inspect}" end raise NotImplementedError, "invalid branch" end |
#permitted_value?(val) ⇒ Boolean
incoming values from the command-line should be strings, so we should stringify any permitted types as the basis of comparison.
886 887 888 889 890 891 892 893 894 |
# File 'lib/optimist.rb', line 886 def permitted_value?(val) case permitted when nil then true when Regexp then val.match? permitted when Range then permitted.include? as_type(val) when Array then permitted.map(&:to_s).include? val else false end end |
#required? ⇒ Boolean
810 |
# File 'lib/optimist.rb', line 810 def required? ; opts(:required) ; end |
#single_arg? ⇒ Boolean
792 793 794 |
# File 'lib/optimist.rb', line 792 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
817 |
# File 'lib/optimist.rb', line 817 def type_format ; "" ; end |
#validate_permitted(arg, value) ⇒ Object
875 876 877 878 879 880 881 882 |
# File 'lib/optimist.rb', line 875 def validate_permitted(arg, value) return true if permitted.nil? unless permitted_value?(value) format_hash = {arg: arg, given: value, value: value, valid_string: permitted_valid_string(), permitted: permitted } raise CommandlineError, permitted_response % format_hash end true end |