dominion 0.0.1
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/dominion.rb +134 -0
- data/spec/dominion_spec.rb +168 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- metadata +121 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Leal
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= dominion
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 David Leal. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "dominion"
|
8
|
+
gem.summary = %Q{Yet another command line option parser}
|
9
|
+
gem.email = "david@thedigitalants.com"
|
10
|
+
gem.homepage = "http://github.com/david/dominion"
|
11
|
+
gem.authors = ["David Leal"]
|
12
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
13
|
+
gem.add_development_dependency "yard", ">= 0"
|
14
|
+
gem.add_development_dependency "rr", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
44
|
+
end
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/dominion.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
module Dominion
|
4
|
+
class UnknownArguments < StandardError; end
|
5
|
+
|
6
|
+
def self.parse(args, &block)
|
7
|
+
Parser.new(args).parse(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
class Parser
|
11
|
+
COMMAND = /\w+(?::\w+)*/
|
12
|
+
|
13
|
+
def initialize(args)
|
14
|
+
@args = args.dup
|
15
|
+
@mappers = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def command(matcher, options = {}, &mappers)
|
19
|
+
if @invoker.for?(matcher)
|
20
|
+
yield if block_given?
|
21
|
+
@invoker.options = options
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def map(arg, opts)
|
26
|
+
if switch?(arg)
|
27
|
+
@mappers.unshift(lambda { extract_option(@args, arg, opts) })
|
28
|
+
else
|
29
|
+
opts[:to] ||= arg
|
30
|
+
|
31
|
+
@mappers.push(lambda { extract_option(@args, arg, opts) })
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse(&block)
|
36
|
+
@invoker = Invoker.new(@args.shift)
|
37
|
+
@result = {}
|
38
|
+
|
39
|
+
instance_eval(&block)
|
40
|
+
|
41
|
+
@mappers.each(&:call)
|
42
|
+
|
43
|
+
raise UnknownArguments, "Unknown arguments: #{@args.join(' ')}" unless @args.empty?
|
44
|
+
|
45
|
+
@invoker.invoke(@result)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def switch?(arg)
|
51
|
+
case arg
|
52
|
+
when Array
|
53
|
+
switch?(arg.first)
|
54
|
+
else
|
55
|
+
arg =~ /^--?/
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def extract_option(args, arg, opts)
|
60
|
+
key_or_val, index = args.each_with_index.find do |a, i|
|
61
|
+
if arg.is_a?(Array)
|
62
|
+
arg.include?(a)
|
63
|
+
elsif arg == Integer
|
64
|
+
a =~ /^\d+$/
|
65
|
+
elsif arg == Hash
|
66
|
+
a =~ /^\w+:\w+$/
|
67
|
+
else
|
68
|
+
arg == a
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
if index
|
73
|
+
if switch?(arg)
|
74
|
+
val = args[index + 1]
|
75
|
+
args[index, 2] = nil
|
76
|
+
|
77
|
+
@result[opts[:as]] = val
|
78
|
+
else
|
79
|
+
@result[opts[:as]] =
|
80
|
+
if opts[:to] == Integer
|
81
|
+
args.delete_at(index)
|
82
|
+
|
83
|
+
Integer(key_or_val)
|
84
|
+
elsif opts[:to] == Hash
|
85
|
+
i = index
|
86
|
+
h = {}
|
87
|
+
while args[i] =~ /^\w+:\w+$/
|
88
|
+
k, v = args[i].split(/:/)
|
89
|
+
h[k.to_sym] = v
|
90
|
+
i += 1
|
91
|
+
end
|
92
|
+
args[index, i - index] = nil
|
93
|
+
h
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
class Invoker
|
102
|
+
def initialize(path)
|
103
|
+
@path = path
|
104
|
+
@options = {}
|
105
|
+
end
|
106
|
+
|
107
|
+
def options=(options)
|
108
|
+
@options.merge!(options)
|
109
|
+
end
|
110
|
+
|
111
|
+
def for?(path)
|
112
|
+
@path == path || path.to_s == '*'
|
113
|
+
end
|
114
|
+
|
115
|
+
def invoke(args)
|
116
|
+
top = case @options[:top]
|
117
|
+
when Class, Module
|
118
|
+
@options[:top].name
|
119
|
+
else
|
120
|
+
@options[:top]
|
121
|
+
end
|
122
|
+
|
123
|
+
elements = @path.split(/:/)
|
124
|
+
method = elements.pop
|
125
|
+
klass = [top].concat(elements.map(&:classify)).compact.join('::').constantize
|
126
|
+
|
127
|
+
if klass.instance_method(method).arity == 1
|
128
|
+
klass.new.send(method, args)
|
129
|
+
else
|
130
|
+
klass.new.send(method)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Dominion do
|
4
|
+
before { class Recipe; end }
|
5
|
+
|
6
|
+
after { Object.send(:remove_const, :Recipe) }
|
7
|
+
|
8
|
+
describe "no arguments" do
|
9
|
+
before { Recipe.class_eval { def create; end } }
|
10
|
+
|
11
|
+
it "maps to a command in the top naming scope" do
|
12
|
+
mock(Recipe).new.mock!.create
|
13
|
+
|
14
|
+
Dominion::parse(%w(recipe:create)) do
|
15
|
+
command :*
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "search for commands inside a given path" do
|
20
|
+
before do
|
21
|
+
module Starships
|
22
|
+
class Enterprise
|
23
|
+
def explore; end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
after do
|
29
|
+
Starships.send(:remove_const, :Enterprise)
|
30
|
+
Object.send(:remove_const, :Starships)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "searches only inside a certain module" do
|
34
|
+
mock(Starships::Enterprise).new.mock!.explore
|
35
|
+
|
36
|
+
Dominion::parse(%w(enterprise:explore)) do
|
37
|
+
command :*, :top => Starships
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "with arguments" do
|
44
|
+
before do
|
45
|
+
Recipe.class_eval { def create(args); end }
|
46
|
+
end
|
47
|
+
|
48
|
+
it "calls a command with a single argument" do
|
49
|
+
mock(Recipe).new.mock!.create({})
|
50
|
+
|
51
|
+
Dominion::parse(%w(recipe:create)) do
|
52
|
+
command :*
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "passes a single letter option to a command" do
|
57
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual')
|
58
|
+
|
59
|
+
Dominion::parse(%w(recipe:create -n Bacalhau\ espiritual)) do
|
60
|
+
command :* do
|
61
|
+
map "-n", :as => :name
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
it "passes a long option to a command" do
|
67
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual')
|
68
|
+
|
69
|
+
Dominion::parse(%w(recipe:create --name Bacalhau\ espiritual)) do
|
70
|
+
command :* do
|
71
|
+
map "--name", :as => :name
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "short and long formats" do
|
77
|
+
[%w(long --name), %w(short -n)].each do |(format, switch)|
|
78
|
+
it "maps the #{format} format" do
|
79
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual')
|
80
|
+
|
81
|
+
Dominion::parse(%W(recipe:create #{switch} Bacalhau\ espiritual)) do
|
82
|
+
command :* do
|
83
|
+
map %w(--name -n), :as => :name
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "honors command specific options" do
|
91
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual', :author => 'unknown')
|
92
|
+
|
93
|
+
Dominion::parse(%W(recipe:create --name Bacalhau\ espiritual --author unknown)) do
|
94
|
+
command :* do
|
95
|
+
map "--name", :as => :name
|
96
|
+
end
|
97
|
+
|
98
|
+
command 'recipe:create' do
|
99
|
+
map "--author", :as => :author
|
100
|
+
end
|
101
|
+
|
102
|
+
command 'recipe:destroy' do
|
103
|
+
map "--reason", :as => :reason
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "number arguments" do
|
109
|
+
it "parses number arguments" do
|
110
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual', :recipe_number => 123456)
|
111
|
+
|
112
|
+
Dominion::parse(%W(recipe:create --name Bacalhau\ espiritual 123456)) do
|
113
|
+
command :* do
|
114
|
+
map "--name", :as => :name
|
115
|
+
end
|
116
|
+
|
117
|
+
command 'recipe:create' do
|
118
|
+
map Integer, :as => :recipe_number
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
it "omits number arguments if they're not present" do
|
124
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual')
|
125
|
+
|
126
|
+
Dominion::parse(%W(recipe:create --name Bacalhau\ espiritual)) do
|
127
|
+
command :* do
|
128
|
+
map "--name", :as => :name
|
129
|
+
end
|
130
|
+
|
131
|
+
command 'recipe:create' do
|
132
|
+
map Integer, :as => :recipe_number
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
it "parses hash arguments" do
|
139
|
+
mock(Recipe).new.mock!.create(:name => 'Bacalhau espiritual',
|
140
|
+
:meta => {:author => 'unknown', :recipe_number => '123456'})
|
141
|
+
|
142
|
+
Dominion::parse(%W(recipe:create --name Bacalhau\ espiritual author:unknown recipe_number:123456)) do
|
143
|
+
command :* do
|
144
|
+
map "--name", :as => :name
|
145
|
+
end
|
146
|
+
|
147
|
+
command 'recipe:create' do
|
148
|
+
map Integer, :as => :recipe_number
|
149
|
+
map Hash, :as => :meta
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
it "complains if there are unparsed arguments left" do
|
155
|
+
lambda do
|
156
|
+
Dominion::parse(%W(recipe:create --name Bacalhau\ espiritual 123456)) do
|
157
|
+
command :* do
|
158
|
+
map "--name", :as => :name
|
159
|
+
end
|
160
|
+
|
161
|
+
command 'recipe:create' do
|
162
|
+
map Integer, :as => :recipe_number
|
163
|
+
end
|
164
|
+
end.should raise_error(Dominion::UnknownArguments)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dominion
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Leal
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-04 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: yard
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rr
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
description:
|
66
|
+
email: david@thedigitalants.com
|
67
|
+
executables: []
|
68
|
+
|
69
|
+
extensions: []
|
70
|
+
|
71
|
+
extra_rdoc_files:
|
72
|
+
- LICENSE
|
73
|
+
- README.rdoc
|
74
|
+
files:
|
75
|
+
- .document
|
76
|
+
- .gitignore
|
77
|
+
- LICENSE
|
78
|
+
- README.rdoc
|
79
|
+
- Rakefile
|
80
|
+
- VERSION
|
81
|
+
- lib/dominion.rb
|
82
|
+
- spec/dominion_spec.rb
|
83
|
+
- spec/spec.opts
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
has_rdoc: true
|
86
|
+
homepage: http://github.com/david/dominion
|
87
|
+
licenses: []
|
88
|
+
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options:
|
91
|
+
- --charset=UTF-8
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
hash: 3
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
version: "0"
|
112
|
+
requirements: []
|
113
|
+
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.3.7
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: Yet another command line option parser
|
119
|
+
test_files:
|
120
|
+
- spec/dominion_spec.rb
|
121
|
+
- spec/spec_helper.rb
|