Class: CP::Option
Instance Attribute Summary collapse
-
#allowed ⇒ Object
Returns the value of attribute allowed.
-
#arg ⇒ Object
readonly
Returns the value of attribute arg.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#long ⇒ Object
Returns the value of attribute long.
-
#short ⇒ Object
Returns the value of attribute short.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #action(&block) ⇒ Object
- #action=(block) ⇒ Object
-
#initialize(*args) {|_self| ... } ⇒ Option
constructor
see: OptionsParser#make_switch.
- #merge(opt) ⇒ Object
- #name ⇒ Object
- #required=(val) ⇒ Object
- #required? ⇒ Boolean
- #switches ⇒ Object
- #to_option_parser_args ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(*args) {|_self| ... } ⇒ Option
see: OptionsParser#make_switch
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cp/option.rb', line 9 def initialize( *args ) @required = false parsed = parse_args( args ) yield self if block_given? # pick up any args we dropped the first time parse_args( self.to_option_parser_args + args ) unless parsed unless self.short || self.long raise ArgumentError.new( "no switch given" ) end end |
Instance Attribute Details
#allowed ⇒ Object
Returns the value of attribute allowed.
5 6 7 |
# File 'lib/cp/option.rb', line 5 def allowed @allowed end |
#arg ⇒ Object (readonly)
Returns the value of attribute arg.
6 7 8 |
# File 'lib/cp/option.rb', line 6 def arg @arg end |
#default ⇒ Object
Returns the value of attribute default.
6 7 8 |
# File 'lib/cp/option.rb', line 6 def default @default end |
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/cp/option.rb', line 5 def description @description end |
#long ⇒ Object
Returns the value of attribute long.
6 7 8 |
# File 'lib/cp/option.rb', line 6 def long @long end |
#short ⇒ Object
Returns the value of attribute short.
6 7 8 |
# File 'lib/cp/option.rb', line 6 def short @short end |
#type ⇒ Object
Returns the value of attribute type.
5 6 7 |
# File 'lib/cp/option.rb', line 5 def type @type end |
Instance Method Details
#action(&block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cp/option.rb', line 23 def action( &block ) save_value = lambda { |value| @value = value } if !block_given? @action || save_value else @action = lambda { |value| save_value.call( value ) block.call( *( block.arity === 0 ? [] : [value] ) ) if block } end end |
#action=(block) ⇒ Object
36 37 38 |
# File 'lib/cp/option.rb', line 36 def action=( block ) action( &block ) end |
#merge(opt) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cp/option.rb', line 58 def merge( opt ) new_opt = self.dup new_opt.action = @action [:allowed, :default, :description, :type, :short, :long].each { |attr| val = opt.send( :"#{attr}" ) new_opt.send( :"#{attr}=", val.dup ) unless val.nil? } new_opt end |
#name ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/cp/option.rb', line 49 def name if self.long name = self.long.sub( /^-+(\[no-\])?/, '' ) else name = self.short.sub( /^-/, '' ).sub( /\[(.-.)\]/, '\\1' ) end name.gsub( "-", "_" ).to_sym end |
#required=(val) ⇒ Object
69 70 71 |
# File 'lib/cp/option.rb', line 69 def required=( val ) @required = !!val end |
#required? ⇒ Boolean
73 74 75 |
# File 'lib/cp/option.rb', line 73 def required? @required end |
#switches ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/cp/option.rb', line 81 def switches [self.short, self.long].inject( [] ) { |acc, s| unless s.nil? s += self.arg unless self.arg.nil? acc << s end acc } end |
#to_option_parser_args ⇒ Object
91 92 93 94 |
# File 'lib/cp/option.rb', line 91 def to_option_parser_args args = self.switches + [self.type, self.allowed, self.description, self.action] args.find_all { |a| !a.nil? } end |
#value ⇒ Object
96 97 98 |
# File 'lib/cp/option.rb', line 96 def value @value.nil? ? self.default : @value end |