Class: Recheck::Optimist::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/recheck/vendor/optimist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



783
784
785
786
787
788
789
790
791
792
793
# File 'lib/recheck/vendor/optimist.rb', line 783

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 = {}
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

def default
  @default
end

#longObject

Returns the value of attribute long.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

def long
  @long
end

#multi_given=(value) ⇒ Object (writeonly)

Sets the attribute multi_given

Parameters:

  • value

    the value to set the attribute multi_given to.



781
782
783
# File 'lib/recheck/vendor/optimist.rb', line 781

def multi_given=(value)
  @multi_given = value
end

#nameObject

Returns the value of attribute name.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

def name
  @name
end

#permittedObject

Returns the value of attribute permitted.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

def permitted
  @permitted
end

#permitted_responseObject

Returns the value of attribute permitted_response.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

def permitted_response
  @permitted_response
end

#shortObject

Returns the value of attribute short.



780
781
782
# File 'lib/recheck/vendor/optimist.rb', line 780

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.

Raises:

  • (ArgumentError)


959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
# File 'lib/recheck/vendor/optimist.rb', line 959

def self.create(name, desc = "", opts = {}, settings = {})
  opttype = Recheck::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 || Recheck::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.is_a?(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

.register_alias(*alias_keys) ⇒ Object

Provide a way to register symbol aliases to the Parser



946
947
948
949
950
951
# File 'lib/recheck/vendor/optimist.rb', line 946

def self.register_alias(*alias_keys)
  alias_keys.each do |alias_key|
    # pass in the alias-key and the class
    Recheck::Optimist::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.

Returns:

  • (Boolean)


823
824
825
# File 'lib/recheck/vendor/optimist.rb', line 823

def array_default?
  default.is_a?(Array)
end

#callbackObject



831
832
833
# File 'lib/recheck/vendor/optimist.rb', line 831

def callback
  opts(:callback)
end

#descObject



835
836
837
# File 'lib/recheck/vendor/optimist.rb', line 835

def desc
  opts(:desc)
end

#doesnt_need_autogen_shortObject



827
828
829
# File 'lib/recheck/vendor/optimist.rb', line 827

def doesnt_need_autogen_short
  !short.auto || short.chars.any?
end

#educateObject



852
853
854
855
856
857
# File 'lib/recheck/vendor/optimist.rb', line 852

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

Returns:

  • (Boolean)


804
805
806
# File 'lib/recheck/vendor/optimist.rb', line 804

def flag?
  false
end

#format_stdio(obj) ⇒ Object

Format stdio like objects to a string



868
869
870
871
872
873
874
875
# File 'lib/recheck/vendor/optimist.rb', line 868

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_descriptionObject

Format the educate-line description including the default and permitted value(s)



860
861
862
863
864
865
# File 'lib/recheck/vendor/optimist.rb', line 860

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

#multiObject Also known as: multi?



812
813
814
# File 'lib/recheck/vendor/optimist.rb', line 812

def multi
  @multi_given
end

#multi_arg?Boolean

Indicates that this is a multivalued (Array type) argument

Returns:

  • (Boolean)


818
819
820
# File 'lib/recheck/vendor/optimist.rb', line 818

def multi_arg?
  false
end

#opts(key) ⇒ Object



795
796
797
# File 'lib/recheck/vendor/optimist.rb', line 795

def opts(key)
  @optshash[key]
end

#opts=(o) ⇒ Object



799
800
801
# File 'lib/recheck/vendor/optimist.rb', line 799

def opts=(o)
  @optshash = o
end

#parse(_paramlist, _neg_given) ⇒ Object

Raises:

  • (NotImplementedError)


843
844
845
# File 'lib/recheck/vendor/optimist.rb', line 843

def parse(_paramlist, _neg_given)
  raise NotImplementedError, "parse must be overridden for newly registered type"
end

#permitted_type_valid?Boolean

Returns:

  • (Boolean)


901
902
903
904
905
906
# File 'lib/recheck/vendor/optimist.rb', line 901

def permitted_type_valid?
  case permitted
  when NilClass, Array, Range, Regexp then true
  else false
  end
end

#permitted_valid_stringObject

Raises:

  • (NotImplementedError)


889
890
891
892
893
894
895
896
897
898
899
# File 'lib/recheck/vendor/optimist.rb', line 889

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.

Returns:

  • (Boolean)


919
920
921
922
923
924
925
926
927
# File 'lib/recheck/vendor/optimist.rb', line 919

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

Returns:

  • (Boolean)


839
840
841
# File 'lib/recheck/vendor/optimist.rb', line 839

def required?
  opts(:required)
end

#single_arg?Boolean

Returns:

  • (Boolean)


808
809
810
# File 'lib/recheck/vendor/optimist.rb', line 808

def single_arg?
  !multi_arg? && !flag?
end

#type_formatObject

provide type-format string. default to empty, but user should probably override it



848
849
850
# File 'lib/recheck/vendor/optimist.rb', line 848

def type_format
  ""
end

#validate_permitted(arg, value) ⇒ Object



908
909
910
911
912
913
914
915
# File 'lib/recheck/vendor/optimist.rb', line 908

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