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 $3:: 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 $4:: 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 $3:: the value /^(-[A-z](:[A-z])*)(.+)$/
Class Method Summary collapse
-
.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
.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 && $3 == 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")
102 103 104 105 106 |
# File 'lib/config_parser/utils.rb', line 102 def setup_flag(key, default=true, attributes={}) attributes[:long] ||= "--#{key}" 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/config_parser/utils.rb', line 126 def setup_list(key, attributes={}) attributes[:long] ||= "--#{key}" attributes[:long].to_s =~ /^(--)?(.*)$/ attributes[:arg_name] ||= $2.upcase 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")
90 91 92 93 94 95 96 |
# File 'lib/config_parser/utils.rb', line 90 def setup_option(key, attributes={}) attributes[:long] ||= "--#{key}" attributes[:long].to_s =~ /^(--)?(.*)$/ attributes[:arg_name] ||= $2.upcase lambda {|value| config[key] = value } end |
.setup_switch(key, default = true, attributes = {}) ⇒ Object
Attributes:
:long the long key ("--[no-]key")
112 113 114 115 116 117 118 |
# File 'lib/config_parser/utils.rb', line 112 def setup_switch(key, default=true, attributes={}) attributes[:long] ||= "--#{key}" attributes[:long].to_s =~ /^(--)?(\[no-\])?(.*)$/ attributes[:long] = "--[no-]#{$3}" unless $2 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 && $3 == nil raise ArgumentError, "invalid short option: #{str}" end str end |