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 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
 
@@ -7,38 +7,53 @@ module Lucy
7
7
  EQUAL = /=/
8
8
 
9
9
 
10
- def self.leading_word?(word)
10
+ def self.magic_word?(word)
11
11
  return unless word
12
- ! (word.match(UNIX_DOUBLE_FLAG) || word.match(UNIX_SINGLE_FLAG) || word.match(EQUAL))
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 _args.is_a? Array
32
+ raise ArgumentError, 'must be an array' unless args.is_a? Array
21
33
  return config if args.empty?
22
34
 
23
- args = args[1..-1] while leading_word?(args.first)
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
- arg = args.shift
28
- peek = args.first
29
- key = arg
30
- if key.match(/=/)
31
- key, value = key.split('=', 2)
32
- elsif peek && peek.match(/=/)
33
- config[key.sub(UNIX_DOUBLE_FLAG, '')] = true
34
- key, value = peek.split('=', 2)
35
- elsif peek.nil? || peek.match(UNIX_DOUBLE_FLAG) || peek.match(UNIX_SINGLE_FLAG)
36
- value = true
37
- else
38
- value = args.shift
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
- value = true if value == 'true'
41
- key = key.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'')
54
+ next unless key and value
55
+
56
+ key = deflag(key)
42
57
  config[key] = value
43
58
  end
44
59
 
@@ -1,5 +1,5 @@
1
1
  module Lucy
2
2
  module Goosey
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -11,6 +11,7 @@ describe Lucy::Goosey do
11
11
  when /--n/ then result['n'].must_equal '1'
12
12
  when /tru/ then result['truth'].must_equal true
13
13
  when /a=b/ then result['a'].must_equal 'b'
14
+ when /fudge/ then result['fudge'].must_equal 'cakes are good'
14
15
  end
15
16
  end
16
17
  end
@@ -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.1.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: -1157995687993160958
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: -1157995687993160958
87
+ hash: -2478981354359286416
88
88
  requirements: []
89
89
  rubyforge_project:
90
90
  rubygems_version: 1.8.23