lucy-goosey 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +31 -4
- data/lib/lucy-goosey.rb +17 -4
- data/lib/lucy-goosey/version.rb +1 -1
- data/test/options_hash_test.rb +40 -0
- metadata +6 -4
data/README.md
CHANGED
@@ -15,8 +15,7 @@ Heavily tested.
|
|
15
15
|
options['n']
|
16
16
|
# => '1'
|
17
17
|
|
18
|
-
options = Lucy::Goosey.parse_options(%w{
|
19
|
-
|
18
|
+
options = Lucy::Goosey.parse_options(%w{-n 1 --foo bar --baz})
|
20
19
|
options['n']
|
21
20
|
# => '1'
|
22
21
|
|
@@ -25,6 +24,33 @@ Heavily tested.
|
|
25
24
|
|
26
25
|
options['baz']
|
27
26
|
# => true
|
27
|
+
|
28
|
+
|
29
|
+
options = Lucy::Goosey.parse_options(%w{foo=bar --db postgres:///local-dev-db})
|
30
|
+
|
31
|
+
options['foo']
|
32
|
+
# => 'bar'
|
33
|
+
|
34
|
+
options['db']
|
35
|
+
# => 'postgres:///local-dev-db'
|
36
|
+
|
37
|
+
options = Lucy::Goosey.parse_options(%w{-n 1 foo=bar -t})
|
38
|
+
options['n']
|
39
|
+
# => '1'
|
40
|
+
|
41
|
+
options['foo']
|
42
|
+
# => 'bar'
|
43
|
+
|
44
|
+
options['t']
|
45
|
+
# => true
|
46
|
+
|
47
|
+
options = Lucy::Goosey.parse_options(%w{--this is pretty cool -foo bar --baz})
|
48
|
+
options['this']
|
49
|
+
# => 'is pretty cool'
|
50
|
+
|
51
|
+
options = Lucy::Goosey.parse_options(%w{ignore leading words --for the win})
|
52
|
+
options
|
53
|
+
# => {'for' => 'the win'}
|
28
54
|
```
|
29
55
|
|
30
56
|
|
@@ -42,9 +68,10 @@ Or install it yourself as:
|
|
42
68
|
|
43
69
|
$ gem install lucy-goosey
|
44
70
|
|
45
|
-
##
|
71
|
+
## Quotes!
|
46
72
|
|
47
|
-
|
73
|
+
"so simple, I love it!" - Pedro Belo
|
74
|
+
"oh thats gross" - Dane Harrigan
|
48
75
|
|
49
76
|
## Contributing
|
50
77
|
|
data/lib/lucy-goosey.rb
CHANGED
@@ -6,6 +6,15 @@ module Lucy
|
|
6
6
|
UNIX_DOUBLE_FLAG = /^--/
|
7
7
|
EQUAL = /=/
|
8
8
|
|
9
|
+
class OptionsHash < Hash
|
10
|
+
attr_accessor :argv
|
11
|
+
|
12
|
+
# Returns true if -h or --help are any of the arg
|
13
|
+
# or 'help' is the first word to argv
|
14
|
+
def help?
|
15
|
+
self['h'] || self['help'] || argv[0] == 'help'
|
16
|
+
end
|
17
|
+
end
|
9
18
|
|
10
19
|
def self.magic_word?(word)
|
11
20
|
return unless word
|
@@ -21,16 +30,20 @@ module Lucy
|
|
21
30
|
word.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'')
|
22
31
|
end
|
23
32
|
|
33
|
+
def self.parse(_args)
|
34
|
+
self.parse_options(_args)
|
35
|
+
end
|
24
36
|
|
25
37
|
# Public: parses array of options, loosely assuming unix-style conventions.
|
26
38
|
#
|
27
39
|
# Returns a Hash
|
28
40
|
def self.parse_options(_args)
|
29
41
|
args = _args.dup
|
30
|
-
|
42
|
+
options = OptionsHash.new
|
43
|
+
options.argv = _args.dup
|
31
44
|
|
32
45
|
raise ArgumentError, 'must be an array' unless args.is_a? Array
|
33
|
-
return
|
46
|
+
return options if args.empty?
|
34
47
|
|
35
48
|
args.reverse!
|
36
49
|
# get rid of leading words
|
@@ -54,10 +67,10 @@ module Lucy
|
|
54
67
|
next unless key and value
|
55
68
|
|
56
69
|
key = deflag(key)
|
57
|
-
|
70
|
+
options[key] = value
|
58
71
|
end
|
59
72
|
|
60
|
-
|
73
|
+
options
|
61
74
|
end
|
62
75
|
end
|
63
76
|
end
|
data/lib/lucy-goosey/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'lucy-goosey'
|
3
|
+
|
4
|
+
describe Lucy::Goosey do
|
5
|
+
describe "argv" do
|
6
|
+
it "should equal the original array" do
|
7
|
+
options = Lucy::Goosey.parse(['fe', 'fi', 'fo', 'fum'])
|
8
|
+
assert_equal 'fe', options.argv[0]
|
9
|
+
assert_equal 'fi', options.argv[1]
|
10
|
+
assert_equal 'fo', options.argv[2]
|
11
|
+
assert_equal 'fum', options.argv[3]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'help?' do
|
16
|
+
it "should be true if -h is passed in" do
|
17
|
+
options = Lucy::Goosey.parse(%w{fi -h fo fum})
|
18
|
+
assert options.help?
|
19
|
+
|
20
|
+
options = Lucy::Goosey.parse(%w{fi -hi fo fum})
|
21
|
+
refute options.help?
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be true if --help is passed in" do
|
25
|
+
options = Lucy::Goosey.parse(%w{fi --help fo fum})
|
26
|
+
assert options.help?
|
27
|
+
|
28
|
+
options = Lucy::Goosey.parse(%w{fi --halp fo fum})
|
29
|
+
refute options.help?
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be true if 'help' is the first arg" do
|
33
|
+
options = Lucy::Goosey.parse(%w{help me})
|
34
|
+
assert options.help?
|
35
|
+
|
36
|
+
options = Lucy::Goosey.parse(%w{me help you})
|
37
|
+
refute options.help?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
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.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-09-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- lib/lucy-goosey/version.rb
|
61
61
|
- lucy-goosey.gemspec
|
62
62
|
- test/combinatorial_test.rb
|
63
|
+
- test/options_hash_test.rb
|
63
64
|
- test/options_parsing_test.rb
|
64
65
|
homepage: ''
|
65
66
|
licenses: []
|
@@ -75,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
76
|
version: '0'
|
76
77
|
segments:
|
77
78
|
- 0
|
78
|
-
hash:
|
79
|
+
hash: 3971889562994054087
|
79
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
81
|
none: false
|
81
82
|
requirements:
|
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
version: '0'
|
85
86
|
segments:
|
86
87
|
- 0
|
87
|
-
hash:
|
88
|
+
hash: 3971889562994054087
|
88
89
|
requirements: []
|
89
90
|
rubyforge_project:
|
90
91
|
rubygems_version: 1.8.23
|
@@ -94,4 +95,5 @@ summary: Takes an array, returns a hash. Expects the array to consist of unix s
|
|
94
95
|
flags or values, much like the command line arguments in ARGV
|
95
96
|
test_files:
|
96
97
|
- test/combinatorial_test.rb
|
98
|
+
- test/options_hash_test.rb
|
97
99
|
- test/options_parsing_test.rb
|