clive 0.7.1 → 0.7.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/README.md +36 -2
- data/lib/clive/command.rb +4 -4
- data/lib/clive/flag.rb +24 -15
- data/lib/clive/formatter.rb +1 -2
- data/lib/clive/version.rb +1 -1
- data/spec/clive/flag_spec.rb +12 -9
- metadata +2 -2
data/README.md
CHANGED
@@ -125,7 +125,7 @@ blocks of the options in it are only ran when they are found.
|
|
125
125
|
### Arguments
|
126
126
|
|
127
127
|
Anything that is not captured as a command, option or argument of a flag, is returned by
|
128
|
-
|
128
|
+
\#parse in an array.
|
129
129
|
|
130
130
|
class Args
|
131
131
|
include Clive::Parser
|
@@ -139,7 +139,41 @@ Anything that is not captured as a command, option or argument of a flag, is ret
|
|
139
139
|
#=> ['argument', 'A string']
|
140
140
|
|
141
141
|
|
142
|
-
###
|
142
|
+
### Variables
|
143
|
+
|
144
|
+
Usually you'll want to set up some kind of variable, such as a hash to store your
|
145
|
+
configuration in, or an array of items to do something with. This would be quite annoying
|
146
|
+
due to the fact it's a class, so I addded some handy methods.
|
147
|
+
|
148
|
+
class CLI
|
149
|
+
option_var :ok, false
|
150
|
+
end
|
151
|
+
CLI.ok #=> false
|
152
|
+
CLI.ok = true
|
153
|
+
CLI.ok #=> true
|
154
|
+
|
155
|
+
class CLI
|
156
|
+
option_hash :config
|
157
|
+
#=> is same as option_var :config, {}
|
158
|
+
|
159
|
+
flag :set, :args => "KEY VALUE" do |key, value|
|
160
|
+
config[key.to_sym] = value
|
161
|
+
end
|
162
|
+
|
163
|
+
option_list :items
|
164
|
+
# or option_array :items
|
165
|
+
#=> are the same as option_var :items, []
|
166
|
+
# option_var :a_list, []
|
167
|
+
|
168
|
+
flag :add, :args => "ITEM" do |item|
|
169
|
+
items << item
|
170
|
+
end
|
171
|
+
end
|
172
|
+
CLI.parse %w(--set works true --add apple --add orange)
|
173
|
+
CLI.config #=> {:works => true}
|
174
|
+
CLI.items #=> ['apple', 'orange']
|
175
|
+
|
176
|
+
### Option Missing Handling
|
143
177
|
|
144
178
|
You are able to intercept errors when an option does not exist in a similar way to
|
145
179
|
`method_missing`.
|
data/lib/clive/command.rb
CHANGED
@@ -106,8 +106,8 @@ module Clive
|
|
106
106
|
v.run
|
107
107
|
when :flag
|
108
108
|
args = i[2..-1]
|
109
|
-
opt_args = v.
|
110
|
-
nec_args = v.
|
109
|
+
opt_args = v.arg_size(:optional)
|
110
|
+
nec_args = v.arg_size(:mandatory)
|
111
111
|
# check for missing args
|
112
112
|
if args.size < nec_args
|
113
113
|
raise MissingArgument.new(v.sort_name)
|
@@ -151,7 +151,7 @@ module Clive
|
|
151
151
|
pre_command << i
|
152
152
|
end
|
153
153
|
end
|
154
|
-
|
154
|
+
|
155
155
|
post_command = Tokens.new(tokens.array - pre_command - [command])
|
156
156
|
pre_command_tokens = parse(pre_command)
|
157
157
|
r = pre_command_tokens
|
@@ -184,7 +184,7 @@ module Clive
|
|
184
184
|
else
|
185
185
|
if k == :word
|
186
186
|
# add to last flag?
|
187
|
-
if r.last && r.last[0] == :flag && r.last.size - 2 < r.last[1].arg_size
|
187
|
+
if r.last && r.last[0] == :flag && r.last.size - 2 < r.last[1].arg_size(:all)
|
188
188
|
r.last.push(v)
|
189
189
|
else
|
190
190
|
r << [:argument, v]
|
data/lib/clive/flag.rb
CHANGED
@@ -81,13 +81,30 @@ module Clive
|
|
81
81
|
@block.call(*args)
|
82
82
|
end
|
83
83
|
|
84
|
-
|
85
|
-
# @
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
84
|
+
|
85
|
+
# @param type [Symbol]
|
86
|
+
# Can be passed three things; :all, returns size of all arguments; :optional
|
87
|
+
# returns all optional arguments; :mandatory, returns size of mandatory arguments.
|
88
|
+
def arg_size(type=:all)
|
89
|
+
case type
|
90
|
+
when :all
|
91
|
+
if @args.is_a?(Array) && @args[0].is_a?(Hash)
|
92
|
+
@args.size
|
93
|
+
else
|
94
|
+
1
|
95
|
+
end
|
96
|
+
when :optional
|
97
|
+
if @args.is_a?(Array) && @args[0].is_a?(Hash)
|
98
|
+
@args.find_all {|i| i[:optional] == true }.size
|
99
|
+
else
|
100
|
+
0
|
101
|
+
end
|
102
|
+
when :mandatory
|
103
|
+
if @args.is_a?(Array) && @args[0].is_a?(Hash)
|
104
|
+
@args.find_all {|i| i[:optional] == false }.size
|
105
|
+
else
|
106
|
+
1
|
107
|
+
end
|
91
108
|
end
|
92
109
|
end
|
93
110
|
|
@@ -109,14 +126,6 @@ module Clive
|
|
109
126
|
end
|
110
127
|
end
|
111
128
|
|
112
|
-
def arg_size
|
113
|
-
if @args.is_a?(Range) || @args.is_a?(Array)
|
114
|
-
1
|
115
|
-
else
|
116
|
-
@args.size
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
129
|
def options_to_strings
|
121
130
|
if @args.is_a? Range
|
122
131
|
[@args.to_s]
|
data/lib/clive/formatter.rb
CHANGED
@@ -111,10 +111,9 @@ module Clive
|
|
111
111
|
|
112
112
|
def parse_format(format, args)
|
113
113
|
if format
|
114
|
-
tokens = Lexer.tokenise(format).to_a
|
115
114
|
obj = Obj.new(args)
|
116
115
|
r = ""
|
117
|
-
|
116
|
+
Lexer.tokenise(format).each do |t,v|
|
118
117
|
case t
|
119
118
|
when :block
|
120
119
|
r << obj.evaluate(v)
|
data/lib/clive/version.rb
CHANGED
data/spec/clive/flag_spec.rb
CHANGED
@@ -19,12 +19,6 @@ describe Clive::Flag do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
describe "#arg_num" do
|
23
|
-
it "returns the number of arguments" do
|
24
|
-
subject.arg_num(false).should == 1
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
22
|
describe "#args_to_strings" do
|
29
23
|
it "converts the arguments to strings" do
|
30
24
|
subject.args_to_strings.should == ["WORD(S)"]
|
@@ -40,9 +34,18 @@ describe Clive::Flag do
|
|
40
34
|
end
|
41
35
|
|
42
36
|
context "when arguments are required" do
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
subject { Clive::Flag.new([:n], "Description", ["REQ [OPT] REQ2 [OPT2] [OPT3]"]) }
|
38
|
+
|
39
|
+
it "returns the number of all arguments" do
|
40
|
+
subject.arg_size(:all).should == 5
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns the number of optional arguments" do
|
44
|
+
subject.arg_size(:optional).should == 3
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns the number of mandatory arguments" do
|
48
|
+
subject.arg_size(:mandatory).should == 2
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|