Class: Optimist::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/optimist_with_insert_blanks.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



690
691
692
693
694
695
696
697
698
# File 'lib/optimist_with_insert_blanks.rb', line 690

def initialize
  @long = nil
  @short = nil
  @name = nil
  @multi_given = false
  @hidden = false
  @default = nil
  @optshash = {}
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



687
688
689
# File 'lib/optimist_with_insert_blanks.rb', line 687

def default
  @default
end

#longObject

Returns the value of attribute long.



687
688
689
# File 'lib/optimist_with_insert_blanks.rb', line 687

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.



688
689
690
# File 'lib/optimist_with_insert_blanks.rb', line 688

def multi_given=(value)
  @multi_given = value
end

#nameObject

Returns the value of attribute name.



687
688
689
# File 'lib/optimist_with_insert_blanks.rb', line 687

def name
  @name
end

#shortObject

Returns the value of attribute short.



687
688
689
# File 'lib/optimist_with_insert_blanks.rb', line 687

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.



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/optimist_with_insert_blanks.rb', line 792

def self.create(name, _desc = '', opts = {}, _settings = {})
  opttype = Optimist::Parser.registry_getopttype(opts[:type])
  opttype_from_default = get_klass_from_default(opts, opttype)

  if opttype && opttype_from_default && (opttype.class != opttype_from_default.class)
    raise ArgumentError,
          ":type specification and default type don't match (default type is #{opttype_from_default.class})"
  end

  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

  ## autobox :default for :multi (multi-occurrence) arguments
  defvalue = [defvalue] if defvalue && multi_given && !defvalue.is_a?(Array)
  opt_inst.default = defvalue
  opt_inst.name = name
  opt_inst.opts = opts
  opt_inst
end

.get_klass_from_default(opts, opttype) ⇒ Object



837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/optimist_with_insert_blanks.rb', line 837

def self.get_klass_from_default(opts, opttype)
  ## 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].is_a?(Array) && opttype.nil?
                            opts[:default].first
                          else
                            opts[:default]
                          end

  return nil if disambiguated_default.nil?

  type_from_default = get_type_from_disdef(opts[:default], opttype, disambiguated_default)
  Optimist::Parser.registry_getopttype(type_from_default)
end

.get_type_from_disdef(optdef, opttype, disambiguated_default) ⇒ Object



824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/optimist_with_insert_blanks.rb', line 824

def self.get_type_from_disdef(optdef, opttype, disambiguated_default)
  if disambiguated_default.is_a? Array
    return(optdef.first.class.name.downcase + 's') unless optdef.empty?

    raise ArgumentError, 'multiple argument type cannot be deduced from an empty array' unless opttype
    raise ArgumentError, 'multiple argument type must be plural' unless opttype.multi_arg?

    return nil

  end
  disambiguated_default.class.name.downcase
end

.handle_long_opt(lopt, name) ⇒ Object



855
856
857
858
859
860
861
862
# File 'lib/optimist_with_insert_blanks.rb', line 855

def self.handle_long_opt(lopt, name)
  lopt = lopt ? lopt.to_s : name.to_s.gsub('_', '-')
  lopt = case lopt
         when /^--([^-].*)$/ then ::Regexp.last_match(1)
         when /^[^-]/        then lopt
         else                     raise ArgumentError, "invalid long option name #{lopt.inspect}"
         end
end

.handle_short_opt(sopt) ⇒ Object



864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/optimist_with_insert_blanks.rb', line 864

def self.handle_short_opt(sopt)
  sopt = sopt.to_s if sopt && sopt != :none
  sopt = case sopt
         when /^-(.)$/          then ::Regexp.last_match(1)
         when nil, :none, /^.$/ then sopt
         else                   raise ArgumentError, "invalid short option name '#{sopt.inspect}'"
         end

  if sopt && (sopt =~ ::Optimist::Parser::INVALID_SHORT_ARG_REGEX)
    raise ArgumentError,
          "a short option name can't be a number or a dash"
  end
  sopt
end

.register_alias(*alias_keys) ⇒ Object

Provide a way to register symbol aliases to the Parser



779
780
781
782
783
784
# File 'lib/optimist_with_insert_blanks.rb', line 779

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.

Returns:

  • (Boolean)


728
729
730
# File 'lib/optimist_with_insert_blanks.rb', line 728

def array_default?
  default.is_a?(Array)
end

#callbackObject



736
737
738
# File 'lib/optimist_with_insert_blanks.rb', line 736

def callback
  opts(:callback)
end

#descObject



740
741
742
# File 'lib/optimist_with_insert_blanks.rb', line 740

def desc
  opts(:desc)
end

#description_with_defaultObject

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



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/optimist_with_insert_blanks.rb', line 762

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'
  "#{desc} (#{defword}: #{default_s})"
end

#educateObject



757
758
759
# File 'lib/optimist_with_insert_blanks.rb', line 757

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

Returns:

  • (Boolean)


709
710
711
# File 'lib/optimist_with_insert_blanks.rb', line 709

def flag?
  false
end

#multiObject Also known as: multi?



717
718
719
# File 'lib/optimist_with_insert_blanks.rb', line 717

def multi
  @multi_given
end

#multi_arg?Boolean

Indicates that this is a multivalued (Array type) argument

Returns:

  • (Boolean)


723
724
725
# File 'lib/optimist_with_insert_blanks.rb', line 723

def multi_arg?
  false
end

#opts(key) ⇒ Object



700
701
702
# File 'lib/optimist_with_insert_blanks.rb', line 700

def opts(key)
  @optshash[key]
end

#opts=(o) ⇒ Object



704
705
706
# File 'lib/optimist_with_insert_blanks.rb', line 704

def opts=(o)
  @optshash = o
end

#parse(_paramlist, _neg_given) ⇒ Object

Raises:

  • (NotImplementedError)


748
749
750
# File 'lib/optimist_with_insert_blanks.rb', line 748

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

#required?Boolean

Returns:

  • (Boolean)


744
745
746
# File 'lib/optimist_with_insert_blanks.rb', line 744

def required?
  opts(:required)
end

#short?Boolean

Returns:

  • (Boolean)


732
733
734
# File 'lib/optimist_with_insert_blanks.rb', line 732

def short?
  short && short != :none
end

#single_arg?Boolean

Returns:

  • (Boolean)


713
714
715
# File 'lib/optimist_with_insert_blanks.rb', line 713

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

#type_formatObject

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



753
754
755
# File 'lib/optimist_with_insert_blanks.rb', line 753

def type_format
  ''
end