lucy-goosey 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/bin/t +1 -0
- data/lib/lucy-goosey.rb +35 -0
- data/lib/lucy-goosey/version.rb +5 -0
- data/lucy-goosey.gemspec +21 -0
- data/test/options_parsing_test.rb +114 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chris Continanza
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Lucy::Goosey
|
2
|
+
|
3
|
+
Command line parsing sucks.
|
4
|
+
`Lucy::Goosey` is the New Jersey style approach to the problem.
|
5
|
+
|
6
|
+
It assumes some unix conventions, and doesn't mess with your environment or do anything magic.
|
7
|
+
|
8
|
+
I write the tests first and use whatever implementation makes them pass.
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'lucy-goosey'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install lucy-goosey
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/t
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
bundle exec turn test
|
data/lib/lucy-goosey.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "lucy-goosey/version"
|
2
|
+
|
3
|
+
module Lucy
|
4
|
+
module Goosey
|
5
|
+
UNIX_SINGLE_FLAG = /^-/
|
6
|
+
UNIX_DOUBLE_FLAG = /^--/
|
7
|
+
|
8
|
+
def self.parse_options(_args)
|
9
|
+
args = _args.dup
|
10
|
+
config = {}
|
11
|
+
|
12
|
+
args.size.times do
|
13
|
+
break if args.empty?
|
14
|
+
arg = args.shift
|
15
|
+
peek = args.first
|
16
|
+
key = arg
|
17
|
+
if key.match(/=/)
|
18
|
+
key, value = key.split('=', 2)
|
19
|
+
elsif peek && peek.match(/=/)
|
20
|
+
config[key.sub(UNIX_DOUBLE_FLAG, '')] = true
|
21
|
+
key, value = peek.split('=', 2)
|
22
|
+
elsif peek.nil? || peek.match(UNIX_DOUBLE_FLAG) || peek.match(UNIX_SINGLE_FLAG)
|
23
|
+
value = true
|
24
|
+
else
|
25
|
+
value = args.shift
|
26
|
+
end
|
27
|
+
value = true if value == 'true'
|
28
|
+
key = key.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'')
|
29
|
+
config[key] = value
|
30
|
+
end
|
31
|
+
|
32
|
+
config
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lucy-goosey.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lucy-goosey/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "lucy-goosey"
|
8
|
+
gem.version = Lucy::Goosey::VERSION
|
9
|
+
gem.authors = ["Chris Continanza"]
|
10
|
+
gem.email = ["christopher.continanza@gmail.com"]
|
11
|
+
gem.description = %q{Simple, fast, and looose command line option parser.}
|
12
|
+
gem.summary = %q{Takes an array, returns a hash. Expects the array to consist of unix style flags or values, much like the command line arguments in ARGV}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_development_dependency 'minitest'
|
20
|
+
gem.add_development_dependency 'turn'
|
21
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'lucy-goosey'
|
3
|
+
|
4
|
+
describe Lucy::Goosey do
|
5
|
+
describe "::parse_options" do
|
6
|
+
|
7
|
+
describe "simple cases" do
|
8
|
+
it "understands -n" do
|
9
|
+
result = Lucy::Goosey.parse_options(%w(-n))
|
10
|
+
result.must_equal({'n' => true})
|
11
|
+
end
|
12
|
+
|
13
|
+
it "understands -n 1" do
|
14
|
+
result = Lucy::Goosey.parse_options(%w(-n 1))
|
15
|
+
result.must_equal({'n' => '1'})
|
16
|
+
end
|
17
|
+
|
18
|
+
it "understands --n" do
|
19
|
+
result = Lucy::Goosey.parse_options(%w(--n))
|
20
|
+
result.must_equal({'n' => true})
|
21
|
+
end
|
22
|
+
|
23
|
+
it "understands --n 1" do
|
24
|
+
result = Lucy::Goosey.parse_options(%w(--n 1))
|
25
|
+
result.must_equal({'n' => '1'})
|
26
|
+
end
|
27
|
+
|
28
|
+
it "understands n=1" do
|
29
|
+
result = Lucy::Goosey.parse_options(%w(n=1))
|
30
|
+
result.must_equal({'n' => '1'})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "combinatorial tests" do
|
35
|
+
before do
|
36
|
+
def tester(raw_string, result)
|
37
|
+
case raw_string
|
38
|
+
when /foo/ then result['foo'].must_equal 'bar'
|
39
|
+
when /--n/ then result['n'].must_equal '1'
|
40
|
+
when /tru/ then result['truth'].must_equal true
|
41
|
+
when /a=b/ then result['a'].must_equal 'b'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
first_level = ['-foo bar', '--n 1', '--truth', 'a=b', '-f']
|
47
|
+
|
48
|
+
first_level.each do |first|
|
49
|
+
second_level = first_level - [first]
|
50
|
+
second_level.each do |second|
|
51
|
+
test_string = "#{first} #{second}"
|
52
|
+
# two levels deep
|
53
|
+
it "understands #{test_string}" do
|
54
|
+
result = Lucy::Goosey.parse_options(test_string.split(' '))
|
55
|
+
tester(first, result)
|
56
|
+
tester(second, result)
|
57
|
+
end
|
58
|
+
|
59
|
+
# third level - its getting a little crazy in here
|
60
|
+
third_level = second_level - [second]
|
61
|
+
third_level.each do |third|
|
62
|
+
test_string = "#{first} #{second} #{third}"
|
63
|
+
|
64
|
+
it "understands #{test_string}" do
|
65
|
+
result = Lucy::Goosey.parse_options(test_string.split(' '))
|
66
|
+
tester(first, result)
|
67
|
+
tester(second, result)
|
68
|
+
tester(third, result)
|
69
|
+
end
|
70
|
+
|
71
|
+
# fourth level - now we're having fun!
|
72
|
+
fourth_level = third_level - [third]
|
73
|
+
fourth_level.each do |fourth|
|
74
|
+
test_string = "#{first} #{second} #{third} #{fourth}"
|
75
|
+
|
76
|
+
it "understands #{test_string}" do
|
77
|
+
result = Lucy::Goosey.parse_options(test_string.split(' '))
|
78
|
+
tester(first, result)
|
79
|
+
tester(second, result)
|
80
|
+
tester(third, result)
|
81
|
+
tester(fourth, result)
|
82
|
+
end
|
83
|
+
|
84
|
+
# fifth level - cus we're not fucking around
|
85
|
+
fifth = (fourth_level - [fourth]).first
|
86
|
+
test_string = "#{first} #{second} #{third} #{fourth} #{fifth}"
|
87
|
+
|
88
|
+
it "understands #{test_string}" do
|
89
|
+
result = Lucy::Goosey.parse_options(test_string.split(' '))
|
90
|
+
tester(first, result)
|
91
|
+
tester(second, result)
|
92
|
+
tester(third, result)
|
93
|
+
tester(fourth, result)
|
94
|
+
tester(fifth, result)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "edge cases" do
|
103
|
+
it "works in any order" do
|
104
|
+
result = Lucy::Goosey.parse_options(%w(--bar foo=baz --baz=bar bob=true --bap))
|
105
|
+
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => true, 'bap' => true })
|
106
|
+
end
|
107
|
+
|
108
|
+
it "understands foo=bar and --baz=bar on the same line" do
|
109
|
+
result = Lucy::Goosey.parse_options(%w(foo=baz --baz=bar bob=true --bar))
|
110
|
+
result.must_equal({ 'foo' => 'baz', 'baz' => 'bar', 'bar' => true, 'bob' => true })
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lucy-goosey
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Continanza
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: turn
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Simple, fast, and looose command line option parser.
|
47
|
+
email:
|
48
|
+
- christopher.continanza@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- bin/t
|
59
|
+
- lib/lucy-goosey.rb
|
60
|
+
- lib/lucy-goosey/version.rb
|
61
|
+
- lucy-goosey.gemspec
|
62
|
+
- test/options_parsing_test.rb
|
63
|
+
homepage: ''
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
hash: 1570045652843307468
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
hash: 1570045652843307468
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.23
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Takes an array, returns a hash. Expects the array to consist of unix style
|
93
|
+
flags or values, much like the command line arguments in ARGV
|
94
|
+
test_files:
|
95
|
+
- test/options_parsing_test.rb
|