Module: Lucy::Goosey
- Defined in:
- lib/lucy-goosey.rb,
lib/lucy-goosey/version.rb
Defined Under Namespace
Classes: OptionsHash
Constant Summary collapse
- UNIX_SINGLE_FLAG =
/^-/
- UNIX_DOUBLE_FLAG =
/^--/
- EQUAL =
/=/
- VERSION =
"0.4.0"
Class Method Summary collapse
- .deflag(word) ⇒ Object
- .flag?(word) ⇒ Boolean
- .magic_word?(word) ⇒ Boolean
- .parse(_args) ⇒ Object
-
.parse_options(_args) ⇒ Object
Public: parses array of options, loosely assuming unix-style conventions.
Class Method Details
.deflag(word) ⇒ Object
29 30 31 |
# File 'lib/lucy-goosey.rb', line 29 def self.deflag(word) word.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'') end |
.flag?(word) ⇒ Boolean
24 25 26 27 |
# File 'lib/lucy-goosey.rb', line 24 def self.flag?(word) return unless word word.match(UNIX_DOUBLE_FLAG) || word.match(UNIX_SINGLE_FLAG) end |
.magic_word?(word) ⇒ Boolean
19 20 21 22 |
# File 'lib/lucy-goosey.rb', line 19 def self.magic_word?(word) return unless word (self.flag?(word) || word.match(EQUAL)) end |
.parse(_args) ⇒ Object
33 34 35 |
# File 'lib/lucy-goosey.rb', line 33 def self.parse(_args) self.(_args) end |
.parse_options(_args) ⇒ Object
Public: parses array of options, loosely assuming unix-style conventions.
Returns a Hash
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lucy-goosey.rb', line 40 def self.(_args) args = _args.dup = OptionsHash.new .argv = _args.dup raise ArgumentError, 'must be an array' unless args.is_a? Array return if args.empty? args.reverse! # get rid of leading words args.pop while args.last && !magic_word?(args.last) args.size.times do break if args.empty? head = args.pop peek = args.last key, value = nil, nil if head.match(/=/) key, value = head.split('=', 2) elsif peek.nil? || magic_word?(peek) key, value = head, true elsif peek key, value = head, [args.pop] value << args.pop while (args.last && !magic_word?(args.last)) value = value.join(' ') end next unless key and value key = deflag(key) [key] = value end end |