Class: Trollop::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/util/trollop.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOption

Returns a new instance of Option.



591
592
593
594
595
596
597
598
599
600
# File 'lib/rbcli/util/trollop.rb', line 591

def initialize
	@long = nil
	@short = nil
	@name = nil
	@multi_given = false
	@hidden = false
	@default = nil
	@permitted = nil
	@optshash = Hash.new()
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



588
589
590
# File 'lib/rbcli/util/trollop.rb', line 588

def default
  @default
end

#longObject

Returns the value of attribute long.



588
589
590
# File 'lib/rbcli/util/trollop.rb', line 588

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.



589
590
591
# File 'lib/rbcli/util/trollop.rb', line 589

def multi_given=(value)
  @multi_given = value
end

#nameObject

Returns the value of attribute name.



588
589
590
# File 'lib/rbcli/util/trollop.rb', line 588

def name
  @name
end

#permittedObject

Returns the value of attribute permitted.



588
589
590
# File 'lib/rbcli/util/trollop.rb', line 588

def permitted
  @permitted
end

#shortObject

Returns the value of attribute short.



588
589
590
# File 'lib/rbcli/util/trollop.rb', line 588

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.

Raises:

  • (ArgumentError)


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
# File 'lib/rbcli/util/trollop.rb', line 696

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

	## 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.default = defvalue
	opt_inst.name = name
	opt_inst.opts = opts
	opt_inst
end

.get_klass_from_default(opts, opttype) ⇒ Object



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/rbcli/util/trollop.rb', line 745

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)
	return Trollop::Parser.registry_getopttype(type_from_default)
end

.get_type_from_disdef(optdef, opttype, disambiguated_default) ⇒ Object



732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/rbcli/util/trollop.rb', line 732

def self.get_type_from_disdef(optdef, opttype, disambiguated_default)
	if disambiguated_default.is_a? Array
		return(optdef.first.class.name.downcase + "s") if !optdef.empty?
		if opttype
			raise ArgumentError, "multiple argument type must be plural" unless opttype.multi_arg?
			return nil
		else
			raise ArgumentError, "multiple argument type cannot be deduced from an empty array"
		end
	end
	return disambiguated_default.class.name.downcase
end

.handle_long_opt(lopt, name) ⇒ Object



762
763
764
765
766
767
768
769
# File 'lib/rbcli/util/trollop.rb', line 762

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

.handle_short_opt(sopt) ⇒ Object



771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/rbcli/util/trollop.rb', line 771

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

	if sopt
		raise ArgumentError, "a short option name can't be a number or a dash" if sopt =~ ::Trollop::Parser::INVALID_SHORT_ARG_REGEX
	end
	return sopt
end

.register_alias(*alias_keys) ⇒ Object

Provide a way to register symbol aliases to the Parser



683
684
685
686
687
688
# File 'lib/rbcli/util/trollop.rb', line 683

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)


623
# File 'lib/rbcli/util/trollop.rb', line 623

def array_default? ; self.default.kind_of?(Array) ; end

#callbackObject



627
# File 'lib/rbcli/util/trollop.rb', line 627

def callback ; opts(:callback) ; end

#descObject



628
# File 'lib/rbcli/util/trollop.rb', line 628

def desc ; opts(:desc) ; end

#description_with_default(str) ⇒ Object

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



652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/rbcli/util/trollop.rb', line 652

def description_with_default str
	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 = str.end_with?('.') ? 'Default' : 'default'
	return "#{str} (#{defword}: #{default_s})"
end

#description_with_permitted(str) ⇒ Object

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



667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/rbcli/util/trollop.rb', line 667

def description_with_permitted str
	permitted_s = permitted.map do |p|
		case p
		when $stdout   then '<stdout>'
		when $stdin    then '<stdin>'
		when $stderr   then '<stderr>'
		else
			p.to_s
		end
	end.join(', ')

	permword = str.end_with?('.') ? 'Permitted' : 'permitted'
	return "#{str} (#{permword}: #{permitted_s})"
end

#educateObject



639
640
641
# File 'lib/rbcli/util/trollop.rb', line 639

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)


611
# File 'lib/rbcli/util/trollop.rb', line 611

def flag? ; false ; end

#full_descriptionObject

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



644
645
646
647
648
649
# File 'lib/rbcli/util/trollop.rb', line 644

def full_description
	desc_str = desc
	desc_str = description_with_default desc_str if default
	desc_str = description_with_permitted desc_str if permitted
	desc_str
end

#multiObject Also known as: multi?



616
# File 'lib/rbcli/util/trollop.rb', line 616

def multi ; @multi_given ; end

#multi_arg?Boolean

Indicates that this is a multivalued (Array type) argument

Returns:

  • (Boolean)


620
# File 'lib/rbcli/util/trollop.rb', line 620

def multi_arg? ; false ; end

#opts(key) ⇒ Object



602
603
604
# File 'lib/rbcli/util/trollop.rb', line 602

def opts (key)
	@optshash[key]
end

#opts=(o) ⇒ Object



606
607
608
# File 'lib/rbcli/util/trollop.rb', line 606

def opts= (o)
	@optshash = o
end

#parse(_paramlist, _neg_given) ⇒ Object

Raises:

  • (NotImplementedError)


632
633
634
# File 'lib/rbcli/util/trollop.rb', line 632

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

#required?Boolean

Returns:

  • (Boolean)


630
# File 'lib/rbcli/util/trollop.rb', line 630

def required? ; opts(:required) ; end

#short?Boolean

Returns:

  • (Boolean)


625
# File 'lib/rbcli/util/trollop.rb', line 625

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

#single_arg?Boolean

Returns:

  • (Boolean)


612
613
614
# File 'lib/rbcli/util/trollop.rb', line 612

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

#type_formatObject

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



637
# File 'lib/rbcli/util/trollop.rb', line 637

def type_format ; "" ; end