Skip to content

Commit 39f956e

Browse files
committed
Merge pull request #35 from o-lim/pass-arg-to-parse
Modify cli:parse to take arg as an input
2 parents 449b863 + 0cfff4e commit 39f956e

File tree

4 files changed

+89
-73
lines changed

4 files changed

+89
-73
lines changed

examples/00_general.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cli:set_name("cli_example.lua")
3232
cli:add_flag("--verbose", "the script output will be very verbose")
3333

3434
-- Parses from _G['arg']
35-
local args = cli:parse_args()
35+
local args = cli:parse(arg)
3636

3737
if not args then
3838
-- something wrong happened and an error was printed

spec/cliargs_methods_spec.lua

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
-- some helper stuff for debugging
33
local quoted = function(s)
4-
return "'" .. tostring(s) .. "'"
4+
return "'" .. tostring(s) .. "'"
55
end
66
local dump = function(t)
77
print(" ============= Dump " .. tostring(t) .. " =============")
@@ -42,7 +42,7 @@ describe("Testing cliargs library methods/functions", function()
4242
it("tests the private split() function", function()
4343
-- takes: str, split-char
4444
local expected, result
45-
45+
4646
result = cli.split("hello,world",",")
4747
expected = {"hello", "world"}
4848
assert.is.same(result, expected)
@@ -65,7 +65,7 @@ describe("Testing cliargs library methods/functions", function()
6565
-- takes: text, size, padding
6666
local text = "123456789 123456789 123456789!"
6767
local expected, result
68-
68+
6969
result = cli.wordwrap(text, 10)
7070
expected = "123456789\n123456789\n123456789!"
7171
assert.is.same(result, expected)
@@ -92,7 +92,7 @@ describe("Testing cliargs library methods/functions", function()
9292
end)
9393

9494
end) -- private functions
95-
95+
9696
describe("testing public functions", function()
9797

9898
setup(function()
@@ -104,7 +104,7 @@ describe("Testing cliargs library methods/functions", function()
104104
teardown(function()
105105
_G._TEST = nil
106106
end)
107-
107+
108108
before_each(function()
109109
cli.optional = {}
110110
cli.required = {}
@@ -120,7 +120,7 @@ describe("Testing cliargs library methods/functions", function()
120120
assert.are.equal(cli.required[1].key, key)
121121
assert.are.equal(cli.required[1].desc, desc)
122122
end)
123-
123+
124124
it("tests add_opt() with short-key", function()
125125
-- takes: key, descr, default
126126
local key, desc, default = "-i", "thedescription", "default"
@@ -131,7 +131,7 @@ describe("Testing cliargs library methods/functions", function()
131131
assert.are.equal(cli.optional[1].flag, true)
132132
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
133133
end)
134-
134+
135135
it("tests add_opt() with short-key & value", function()
136136
-- takes: key, descr, default
137137
local key, desc, default = "-i VALUE", "thedescription", "default"
@@ -142,7 +142,7 @@ describe("Testing cliargs library methods/functions", function()
142142
assert.are.equal(cli.optional[1].flag, false)
143143
assert.are.equal(cli.optional[1].default, default)
144144
end)
145-
145+
146146
it("tests add_opt() with short + expanded-key", function()
147147
-- takes: key, descr, default
148148
local key, desc, default = "-i, --insert", "thedescription", "default"
@@ -153,7 +153,7 @@ describe("Testing cliargs library methods/functions", function()
153153
assert.are.equal(cli.optional[1].flag, true)
154154
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
155155
end)
156-
156+
157157
it("tests add_opt() with short + expanded-key & value", function()
158158
-- takes: key, descr, default
159159
local key, desc, default = "-i, --insert=VALUE", "thedescription", "default"
@@ -164,7 +164,7 @@ describe("Testing cliargs library methods/functions", function()
164164
assert.are.equal(cli.optional[1].flag, false)
165165
assert.are.equal(cli.optional[1].default, default)
166166
end)
167-
167+
168168
it("tests add_opt() with only expanded-key", function()
169169
-- takes: key, descr, default
170170
local key, desc, default = "--insert", "thedescription", "default"
@@ -175,7 +175,7 @@ describe("Testing cliargs library methods/functions", function()
175175
assert.are.equal(cli.optional[1].flag, true)
176176
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
177177
end)
178-
178+
179179
it("tests add_opt() with only expanded-key & value", function()
180180
-- takes: key, descr, default
181181
local key, desc, default = "--insert=VALUE", "thedescription", "default"
@@ -186,15 +186,15 @@ describe("Testing cliargs library methods/functions", function()
186186
assert.are.equal(cli.optional[1].flag, false)
187187
assert.are.equal(cli.optional[1].default, default)
188188
end)
189-
189+
190190
it("tests add_opt() with short and expanded-key, no comma between them", function()
191191
-- takes: key, descr, default
192192
local key, desc, default = "-i --insert=VALUE", "thedescription", "default"
193193
cli:add_opt(key, desc, default)
194194
assert.are.equal(cli.optional[1].key, "i")
195195
assert.are.equal(cli.optional[1].expanded_key, "insert")
196196
end)
197-
197+
198198
it("tests add_flag() for setting default value", function()
199199
-- takes: key, descr
200200
local key, desc = "-i, --insert", "thedescription"
@@ -216,7 +216,7 @@ describe("Testing cliargs library methods/functions", function()
216216
assert.are.equal(cli.required[1].key, key) -- make sure it got added
217217
assert.is.error(function() cli:add_arg(key, desc) end) -- this should blow up
218218
end)
219-
219+
220220
it("tests add_opt() with a duplicate argument", function()
221221
-- takes: key, descr
222222
local key, desc, default = "-i", "thedescription", "default"
@@ -225,11 +225,11 @@ describe("Testing cliargs library methods/functions", function()
225225
--assert.are.equal(cli.optional[1].expanded_key, "")
226226
assert.is.error(function() cli:add_opt(key, desc, default) end) -- this should blow up
227227
end)
228-
228+
229229
describe("testing the 'noprint' options", function()
230230

231231
local old_print, touched
232-
232+
233233
setup(function()
234234
old_print = print
235235
local interceptor = function(...)
@@ -238,15 +238,15 @@ describe("Testing cliargs library methods/functions", function()
238238
end
239239
print = interceptor
240240
end)
241-
241+
242242
teardown(function()
243243
print = (old_print or print)
244244
end)
245245

246246
before_each(function()
247247
touched = nil
248248
end)
249-
249+
250250
after_each(function()
251251
end)
252252

@@ -258,21 +258,21 @@ describe("Testing cliargs library methods/functions", function()
258258
assert.is.equal(type(res), "string")
259259
assert.is.equal(nil, touched)
260260
end)
261-
261+
262262
it("tests whether a parsing error through cli_error() does not print anything, if noprint is set", function()
263263
-- generate a parse error
264264
local key, desc = "ARGUMENT", "thedescription"
265265
cli:add_opt(key, desc)
266266
local noprint = true
267-
_G.arg = {"arg1", "arg2", "arg3", "arg4"} -- should fail for too many arguments
268-
local res, err = cli:parse(noprint)
267+
local args = {"arg1", "arg2", "arg3", "arg4"} -- should fail for too many arguments
268+
local res, err = cli:parse(args, noprint)
269269
assert.is.equal(nil, res)
270270
assert.is.equal(type(err), "string")
271271
assert.is.equal(nil, touched)
272272
end)
273-
273+
274274
end)
275275

276276
end) -- public functions
277-
277+
278278
end)

0 commit comments

Comments
 (0)