optitron 0.1.1 → 0.1.2
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/Gemfile.lock +1 -1
- data/README.rdoc +23 -0
- data/lib/optitron/class_dsl.rb +7 -2
- data/lib/optitron/option.rb +25 -15
- data/lib/optitron/version.rb +1 -1
- data/spec/arg_spec.rb +18 -0
- data/spec/cli_spec.rb +6 -0
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -80,6 +80,29 @@ Will generate
|
|
80
80
|
|
81
81
|
start <modes1 modes2 ...> # Starts the process
|
82
82
|
|
83
|
+
As well, you can provide type hints to the args:
|
84
|
+
|
85
|
+
desc "Does something awesome"
|
86
|
+
arg_types :numeric, :string, :boolean
|
87
|
+
def start(number, string, truth)
|
88
|
+
# ... does something
|
89
|
+
end
|
90
|
+
|
91
|
+
Will generate
|
92
|
+
|
93
|
+
start [number] [string] [truth] # Does something awesome
|
94
|
+
|
95
|
+
And those arguments have to conform to the types given. For instance, calling this with
|
96
|
+
|
97
|
+
start 123 something flase
|
98
|
+
|
99
|
+
Will produce the error <tt>Truth is invalid</tt>. Calling with
|
100
|
+
|
101
|
+
start 123 something true
|
102
|
+
|
103
|
+
however, will correctly invoke the method.
|
104
|
+
|
105
|
+
|
83
106
|
=== Options
|
84
107
|
|
85
108
|
Options are specified with the +opt+ method in your class, as follows:
|
data/lib/optitron/class_dsl.rb
CHANGED
@@ -124,20 +124,25 @@ class Optitron
|
|
124
124
|
@opts << [name, desc, opts]
|
125
125
|
end
|
126
126
|
|
127
|
+
def arg_types(*types)
|
128
|
+
@arg_types = types
|
129
|
+
end
|
130
|
+
|
127
131
|
def build
|
128
132
|
unless @built
|
129
133
|
optitron_dsl.root.help unless send(:class_variable_defined?, :@@suppress_help)
|
130
134
|
@cmds.each do |(cmd_name, cmd_desc, opts)|
|
131
135
|
args = method_args[cmd_name.to_sym]
|
132
136
|
arity = instance_method(cmd_name).arity
|
137
|
+
arg_types = @arg_types
|
133
138
|
optitron_dsl.root.cmd(cmd_name, cmd_desc) do
|
134
139
|
opts.each { |o| opt *o }
|
135
140
|
args.each do |(arg_name, arg_type, arg_default)|
|
136
141
|
case arg_type
|
137
142
|
when :required
|
138
|
-
arg arg_name.to_s, :default => arg_default && eval(arg_default)
|
143
|
+
arg arg_name.to_s, :default => arg_default && eval(arg_default), :type => arg_types && arg_types.shift
|
139
144
|
when :optional
|
140
|
-
arg arg_name.to_s, :default => arg_default && eval(arg_default), :required => false
|
145
|
+
arg arg_name.to_s, :default => arg_default && eval(arg_default), :required => false, :type => arg_types && arg_types.shift
|
141
146
|
when :greedy
|
142
147
|
arg arg_name.to_s, :default => arg_default && eval(arg_default), :type => :greedy
|
143
148
|
end
|
data/lib/optitron/option.rb
CHANGED
@@ -194,27 +194,37 @@ class Optitron
|
|
194
194
|
self.required = opts && opts.key?(:required) ? opts[:required] : (@default.nil? and !greedy?)
|
195
195
|
end
|
196
196
|
|
197
|
-
def
|
197
|
+
def consume_array(response, tokens)
|
198
198
|
arg_tokens = tokens.select{ |tok| tok.respond_to?(:lit) }
|
199
|
-
|
200
|
-
|
201
|
-
|
199
|
+
response.args_with_tokens << [self, []]
|
200
|
+
while !arg_tokens.size.zero?
|
201
|
+
if val = yield(arg_tokens.first)
|
202
202
|
arg_tok = arg_tokens.shift
|
203
203
|
tokens.delete_at(tokens.index(arg_tok))
|
204
|
-
response.args_with_tokens.last.last <<
|
205
|
-
|
206
|
-
|
207
|
-
response.add_error("required", name)
|
204
|
+
response.args_with_tokens.last.last << val
|
205
|
+
else
|
206
|
+
break
|
208
207
|
end
|
208
|
+
end
|
209
|
+
if required? and response.args_with_tokens.last.last.size.zero?
|
210
|
+
response.add_error("required", name)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def consume(response, tokens)
|
215
|
+
case type
|
216
|
+
when :greedy, :array
|
217
|
+
consume_array(response, tokens) { |t| t.lit }
|
218
|
+
when :hash
|
219
|
+
consume_array(response, tokens) { |t| t.lit[':'] && t.lit.split(':', 2) }
|
220
|
+
response.args_with_tokens.last[-1] = Hash[response.args_with_tokens.last[-1]]
|
209
221
|
else
|
210
|
-
|
222
|
+
arg_token = tokens.find{ |tok| tok.respond_to?(:lit) }
|
223
|
+
if arg_token || has_default
|
224
|
+
tokens.delete_at(tokens.index(arg_token)) if arg_token
|
225
|
+
response.args_with_tokens << [self, arg_token ? arg_token.lit : default]
|
226
|
+
elsif !arg_token and required?
|
211
227
|
response.add_error("required", name)
|
212
|
-
elsif !arg_tokens.size.zero?
|
213
|
-
arg_tok = arg_tokens.shift
|
214
|
-
tokens.delete_at(tokens.index(arg_tok))
|
215
|
-
response.args_with_tokens << [self, arg_tok.lit]
|
216
|
-
elsif has_default
|
217
|
-
response.args_with_tokens << [self, default]
|
218
228
|
end
|
219
229
|
end
|
220
230
|
end
|
data/lib/optitron/version.rb
CHANGED
data/spec/arg_spec.rb
CHANGED
@@ -177,4 +177,22 @@ describe "Optitron::Parser arg spec" do
|
|
177
177
|
response.valid?.should be_true
|
178
178
|
end
|
179
179
|
end
|
180
|
+
|
181
|
+
context "various arg types" do
|
182
|
+
before(:each) {
|
183
|
+
@parser = Optitron.new {
|
184
|
+
cmd "install" do
|
185
|
+
arg "file", :type => :hash
|
186
|
+
end
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
it "should parse" do
|
191
|
+
response = @parser.parse(%w(install test:one test2:two))
|
192
|
+
response.command.should == 'install'
|
193
|
+
response.args.should == [{'test' => 'one', 'test2' => 'two'}]
|
194
|
+
response.valid?.should be_true
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
180
198
|
end
|
data/spec/cli_spec.rb
CHANGED
@@ -15,7 +15,9 @@ class CLIExample < Optitron::CLI
|
|
15
15
|
|
16
16
|
desc "Use this too"
|
17
17
|
opt 'another_opt'
|
18
|
+
arg_types :hash
|
18
19
|
def use_too(one, two = 'three')
|
20
|
+
puts "one: #{one.inspect} #{two.inspect}"
|
19
21
|
end
|
20
22
|
|
21
23
|
desc "Use this three"
|
@@ -67,6 +69,10 @@ describe "Optitron::Parser defaults" do
|
|
67
69
|
capture(:stdout) { CLIExample.dispatch(%w(use))}.should == "using this\n"
|
68
70
|
end
|
69
71
|
|
72
|
+
it "should dispatch with the type hinting" do
|
73
|
+
capture(:stdout) { CLIExample.dispatch(%w(use_too one:two three:four))}.should == "one: {\"three\"=>\"four\", \"one\"=>\"two\"} \"three\"\n"
|
74
|
+
end
|
75
|
+
|
70
76
|
it "should generate the correct help" do
|
71
77
|
AnotherCLIExample.build
|
72
78
|
AnotherCLIExample.optitron_parser.help.strip.should == "Commands\n\nuse_too [one] <two=\"three\"> # Use this too\n -a/--another_opt \n\nGlobal options\n\n-v/--verbose \n-?/--help # Print help message"
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: optitron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joshua Hull
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-22 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|