Module: ConfigParser::Utils
- Included in:
- ConfigParser
- Defined in:
- lib/config_parser/utils.rb
Overview
A medly of methods used throughout the ConfigParser classes.
Constant Summary collapse
- OPTION_BREAK =
The option break argument
"--"
- LONG_OPTION =
Matches a nested long option, with or without a value (ex: ‘–opt’, ‘–nested:opt’, ‘–opt=value’). After the match:
$1:: the switch $2:: the value
/^(--[A-z].*?)(?:=(.*))?$/
- SHORT_OPTION =
Matches a nested short option, with or without a value (ex: ‘-o’, ‘-n:o’, ‘-o=value’). After the match:
$1:: the switch $2:: the value
/^(-[A-z](?::[A-z])*)(?:=(.*))?$/
- ALT_SHORT_OPTION =
Matches the alternate syntax for short options (ex: ‘-n:ovalue’, ‘-ovalue’). After the match:
$1:: the switch $2:: the value
/^(-[A-z](?::[A-z])*)(.+)$/
Class Method Summary collapse
-
.infer_arg_name(key, attributes) ⇒ Object
Infers the default argname from attributes and sets it in attributes.
-
.infer_long(key, attributes) ⇒ Object
Infers the default long using key and adds it to attributes.
-
.longify(str) ⇒ Object
Turns the input string into a long-format option.
-
.prefix_long(switch, prefix, split_char = ':') ⇒ Object
Adds a prefix onto the last nested segment of a long option.
-
.setup_flag(key, default = true, attributes = {}) ⇒ Object
Attributes:.
-
.setup_list(key, attributes = {}) ⇒ Object
Attributes:.
-
.setup_option(key, attributes = {}) ⇒ Object
Attributes:.
-
.setup_switch(key, default = true, attributes = {}) ⇒ Object
Attributes:.
-
.shortify(str) ⇒ Object
Turns the input string into a short-format option.
Class Method Details
.infer_arg_name(key, attributes) ⇒ Object
Infers the default argname from attributes and sets it in attributes. Returns attributes.
infer_arg_name(:key, {:long => '--opt'}) # => {:long => '--opt', :arg_name => 'OPT'}
infer_arg_name(:key, {}) # => {:arg_name => 'KEY'}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/config_parser/utils.rb', line 104 def infer_arg_name(key, attributes) if attributes.has_key?(:arg_name) return attributes end if long = attributes[:long] long.to_s =~ /^(?:--)?(.*)$/ attributes[:arg_name] = $1.upcase else attributes[:arg_name] = key.to_s.upcase end attributes end |
.infer_long(key, attributes) ⇒ Object
Infers the default long using key and adds it to attributes. Returns attributes.
infer_long(:key, {}) # => {:long => '--key'}
90 91 92 93 94 95 96 |
# File 'lib/config_parser/utils.rb', line 90 def infer_long(key, attributes) unless attributes.has_key?(:long) attributes[:long] = "--#{key}" end attributes end |
.longify(str) ⇒ Object
Turns the input string into a long-format option. Underscores are converted to hyphens. Raises an error if the option does not match LONG_OPTION. Nils are returned directly.
longify("--opt") # => '--opt'
longify(:opt) # => '--opt'
longify(:opt_ion) # => '--opt-ion'
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/config_parser/utils.rb', line 61 def longify(str) return nil if str == nil str = str.to_s str = "--#{str}" unless str =~ /^--/ str.gsub!(/_/, '-') unless str =~ LONG_OPTION && $2 == nil raise ArgumentError, "invalid long option: #{str}" end str end |
.prefix_long(switch, prefix, split_char = ':') ⇒ Object
Adds a prefix onto the last nested segment of a long option.
prefix_long("--opt", 'no-') # => '--no-opt'
prefix_long("--nested:opt", 'no-') # => '--nested:no-opt'
78 79 80 81 82 83 |
# File 'lib/config_parser/utils.rb', line 78 def prefix_long(switch, prefix, split_char=':') switch = switch[2,switch.length-2] if switch =~ /^--/ switch = switch.split(split_char) switch[-1] = "#{prefix}#{switch[-1]}" "--#{switch.join(':')}" end |
.setup_flag(key, default = true, attributes = {}) ⇒ Object
Attributes:
:long the long key ("--key")
135 136 137 138 139 |
# File 'lib/config_parser/utils.rb', line 135 def setup_flag(key, default=true, attributes={}) infer_long(key, attributes) lambda {config[key] = !default } end |
.setup_list(key, attributes = {}) ⇒ Object
Attributes:
:long the long key ("--key")
:arg_name the argument name ("KEY")
:split the split character
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/config_parser/utils.rb', line 161 def setup_list(key, attributes={}) infer_long(key, attributes) infer_arg_name(key, attributes) split = attributes[:split] n = attributes[:n] lambda do |value| array = (config[key] ||= []) array.concat(split ? value.split(split) : [value]) if n && array.length > n raise "too many assignments: #{key.inspect}" end end end |
.setup_option(key, attributes = {}) ⇒ Object
Attributes:
:long the long key ("--key")
:arg_name the argument name ("KEY")
124 125 126 127 128 129 |
# File 'lib/config_parser/utils.rb', line 124 def setup_option(key, attributes={}) infer_long(key, attributes) infer_arg_name(key, attributes) lambda {|value| config[key] = value } end |
.setup_switch(key, default = true, attributes = {}) ⇒ Object
Attributes:
:long the long key ("--[no-]key")
145 146 147 148 149 150 151 152 153 |
# File 'lib/config_parser/utils.rb', line 145 def setup_switch(key, default=true, attributes={}) infer_long(key, attributes) if attributes[:long].to_s =~ /^(?:--)?(\[no-\])?(.*)$/ attributes[:long] = "--[no-]#{$2}" unless $1 end lambda {|value| config[key] = value } end |
.shortify(str) ⇒ Object
Turns the input string into a short-format option. Raises an error if the option does not match SHORT_OPTION. Nils are returned directly.
shortify("-o") # => '-o'
shortify(:o) # => '-o'
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/config_parser/utils.rb', line 42 def shortify(str) return nil if str == nil str = str.to_s str = "-#{str}" unless str[0] == ?- unless str =~ SHORT_OPTION && $2 == nil raise ArgumentError, "invalid short option: #{str}" end str end |