lucy-goosey 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +20 -0
- data/lib/lucy-goosey.rb +33 -18
- data/lib/lucy-goosey/version.rb +1 -1
- data/test/combinatorial_test.rb +1 -0
- data/test/options_parsing_test.rb +15 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -7,6 +7,26 @@ It assumes some unix conventions, and doesn't mess with your environment or do a
|
|
7
7
|
|
8
8
|
Heavily tested.
|
9
9
|
|
10
|
+
## Examples
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
options = Lucy::Goosey.parse_options(%w{-n 1})
|
14
|
+
|
15
|
+
options['n']
|
16
|
+
# => '1'
|
17
|
+
|
18
|
+
options = Lucy::Goosey.parse_options(%w{--n 1 --foo bar --baz})
|
19
|
+
|
20
|
+
options['n']
|
21
|
+
# => '1'
|
22
|
+
|
23
|
+
options['foo']
|
24
|
+
# => 'bar'
|
25
|
+
|
26
|
+
options['baz']
|
27
|
+
# => true
|
28
|
+
```
|
29
|
+
|
10
30
|
|
11
31
|
## Installation
|
12
32
|
|
data/lib/lucy-goosey.rb
CHANGED
@@ -7,38 +7,53 @@ module Lucy
|
|
7
7
|
EQUAL = /=/
|
8
8
|
|
9
9
|
|
10
|
-
def self.
|
10
|
+
def self.magic_word?(word)
|
11
11
|
return unless word
|
12
|
-
|
12
|
+
(self.flag?(word) || word.match(EQUAL))
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.flag?(word)
|
16
|
+
return unless word
|
17
|
+
word.match(UNIX_DOUBLE_FLAG) || word.match(UNIX_SINGLE_FLAG)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.deflag(word)
|
21
|
+
word.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'')
|
13
22
|
end
|
14
23
|
|
15
24
|
|
25
|
+
# Public: parses array of options, loosely assuming unix-style conventions.
|
26
|
+
#
|
27
|
+
# Returns a Hash
|
16
28
|
def self.parse_options(_args)
|
17
29
|
args = _args.dup
|
18
30
|
config = {}
|
19
31
|
|
20
|
-
raise ArgumentError, 'must be an array' unless
|
32
|
+
raise ArgumentError, 'must be an array' unless args.is_a? Array
|
21
33
|
return config if args.empty?
|
22
34
|
|
23
|
-
args
|
35
|
+
args.reverse!
|
36
|
+
# get rid of leading words
|
37
|
+
args.pop while args.last && !magic_word?(args.last)
|
24
38
|
|
25
39
|
args.size.times do
|
26
40
|
break if args.empty?
|
27
|
-
|
28
|
-
peek = args.
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
key, value
|
35
|
-
elsif peek
|
36
|
-
value =
|
37
|
-
|
38
|
-
value =
|
41
|
+
head = args.pop
|
42
|
+
peek = args.last
|
43
|
+
|
44
|
+
key, value = nil, nil
|
45
|
+
if head.match(/=/)
|
46
|
+
key, value = head.split('=', 2)
|
47
|
+
elsif peek.nil? || magic_word?(peek)
|
48
|
+
key, value = head, true
|
49
|
+
elsif peek
|
50
|
+
key, value = head, [args.pop]
|
51
|
+
value << args.pop while (args.last && !magic_word?(args.last))
|
52
|
+
value = value.join(' ')
|
39
53
|
end
|
40
|
-
|
41
|
-
|
54
|
+
next unless key and value
|
55
|
+
|
56
|
+
key = deflag(key)
|
42
57
|
config[key] = value
|
43
58
|
end
|
44
59
|
|
data/lib/lucy-goosey/version.rb
CHANGED
data/test/combinatorial_test.rb
CHANGED
@@ -15,6 +15,11 @@ describe Lucy::Goosey do
|
|
15
15
|
result.must_equal({'n' => true})
|
16
16
|
end
|
17
17
|
|
18
|
+
it "understands -n is the best" do
|
19
|
+
result = Lucy::Goosey.parse_options(%w(-n is the best))
|
20
|
+
result.must_equal({'n' => 'is the best'})
|
21
|
+
end
|
22
|
+
|
18
23
|
it "understands -n 1" do
|
19
24
|
result = Lucy::Goosey.parse_options(%w(-n 1))
|
20
25
|
result.must_equal({'n' => '1'})
|
@@ -51,6 +56,14 @@ describe Lucy::Goosey do
|
|
51
56
|
end
|
52
57
|
|
53
58
|
describe "edge cases" do
|
59
|
+
it "lets you use lots of words" do
|
60
|
+
result = Lucy::Goosey.parse_options(%w(wtf bbq --foo bar none --foo-baz -n 1 -t))
|
61
|
+
result.must_equal({'foo' => 'bar none', 'foo-baz' => true, 't' => true, 'n' => '1'})
|
62
|
+
|
63
|
+
result = Lucy::Goosey.parse_options(%w(wtf bbq -foo bar none --foo-baz -n 1 -t))
|
64
|
+
result.must_equal({'foo' => 'bar none', 'foo-baz' => true, 't' => true, 'n' => '1'})
|
65
|
+
end
|
66
|
+
|
54
67
|
it "ignores leading words" do
|
55
68
|
result = Lucy::Goosey.parse_options(%w(wtf bbq --bar foo=baz --baz=bar -n 1 --bap))
|
56
69
|
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'n' => '1', 'bap' => true })
|
@@ -58,12 +71,12 @@ describe Lucy::Goosey do
|
|
58
71
|
|
59
72
|
it "works in any order" do
|
60
73
|
result = Lucy::Goosey.parse_options(%w(--bar foo=baz --baz=bar bob=true --bap))
|
61
|
-
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => true, 'bap' => true })
|
74
|
+
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => 'true', 'bap' => true })
|
62
75
|
end
|
63
76
|
|
64
77
|
it "understands foo=bar and --baz=bar on the same line" do
|
65
78
|
result = Lucy::Goosey.parse_options(%w(foo=baz --baz=bar bob=true --bar))
|
66
|
-
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => true })
|
79
|
+
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => 'true' })
|
67
80
|
end
|
68
81
|
end
|
69
82
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lucy-goosey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -75,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
75
|
version: '0'
|
76
76
|
segments:
|
77
77
|
- 0
|
78
|
-
hash: -
|
78
|
+
hash: -2478981354359286416
|
79
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
80
|
none: false
|
81
81
|
requirements:
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
version: '0'
|
85
85
|
segments:
|
86
86
|
- 0
|
87
|
-
hash: -
|
87
|
+
hash: -2478981354359286416
|
88
88
|
requirements: []
|
89
89
|
rubyforge_project:
|
90
90
|
rubygems_version: 1.8.23
|