Skip to content

Commit 98c0aa5

Browse files
committed
Merge pull request #39 from o-lim/set-default-nil
Set default option value to nil if no default is specified
2 parents f1c4e80 + 4cc702c commit 98c0aa5

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

spec/cliargs_methods_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe("Testing cliargs library methods/functions", function()
182182
assert.are.equal(cli.optional[1].key, "i")
183183
assert.are.equal(cli.optional[1].desc, desc)
184184
assert.are.equal(cli.optional[1].flag, true)
185-
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
185+
assert.are.equal(cli.optional[1].default, nil) -- no value = flag type option, hence nil
186186
end)
187187

188188
it("should work with a short-key that is longer than 1 character", function()
@@ -193,7 +193,7 @@ describe("Testing cliargs library methods/functions", function()
193193
assert.are.equal(cli.optional[1].key, "Wno-unsigned")
194194
assert.are.equal(cli.optional[1].desc, desc)
195195
assert.are.equal(cli.optional[1].flag, true)
196-
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
196+
assert.are.equal(cli.optional[1].default, nil) -- no value = flag type option, hence nil
197197
end)
198198

199199
it("should work with only expanded-key", function()
@@ -203,7 +203,7 @@ describe("Testing cliargs library methods/functions", function()
203203
assert.are.equal(cli.optional[1].expanded_key, "insert")
204204
assert.are.equal(cli.optional[1].desc, desc)
205205
assert.are.equal(cli.optional[1].flag, true)
206-
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
206+
assert.are.equal(cli.optional[1].default, nil) -- no value = flag type option, hence nil
207207
end)
208208

209209
it("should work with combined short + expanded-key", function()
@@ -214,7 +214,7 @@ describe("Testing cliargs library methods/functions", function()
214214
assert.are.equal(cli.optional[1].expanded_key, "insert")
215215
assert.are.equal(cli.optional[1].desc, desc)
216216
assert.are.equal(cli.optional[1].flag, true)
217-
assert.are.equal(cli.optional[1].default, false) -- no value = flag type option, hence false
217+
assert.are.equal(cli.optional[1].default, nil) -- no value = flag type option, hence nil
218218
end)
219219
end)
220220
end)
@@ -224,7 +224,7 @@ describe("Testing cliargs library methods/functions", function()
224224
local key, desc = "-i, --insert", "thedescription"
225225
cli:add_flag(key, desc)
226226
assert.are.equal(cli.optional[1].flag, true)
227-
assert.are.equal(cli.optional[1].default, false) -- boolean because its a flag
227+
assert.are.equal(cli.optional[1].default, nil)
228228
end)
229229

230230
it("tests add_flag() to error-out when providing a value", function()

spec/cliargs_parsing_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ local function populate_flags()
4242
cli:add_flag("-d", "script will run in DEBUG mode")
4343
cli:add_flag("--verbose", "the script output will be very verbose")
4444

45-
return { d = false, v = false, version = false, verbose = false }
45+
return { d = nil, v = nil, version = nil, verbose = nil }
4646
end
4747

4848
-- start tests

src/cliargs.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ end
163163
--- ### Parameters
164164
--- 1. **key**: the argument's "name" that will be displayed to the user
165165
--- 2. **desc**: a description of the argument
166-
--- 3. **default**: *optional*; specify a default value (the default is "")
166+
--- 3. **default**: *optional*; specify a default value (the default is nil)
167167
--- 4. **maxcount**: *optional*; specify the maximum number of occurences allowed (default is 1)
168168
---
169169
--- ### Usage example
@@ -172,8 +172,7 @@ end
172172
--- The value returned will be a table with at least 1 entry and a maximum of 2 entries
173173
function cli:optarg(key, desc, default, maxcount)
174174
assert(type(key) == "string" and type(desc) == "string", "Key and description are mandatory arguments (Strings)")
175-
default = default or ""
176-
assert(type(default) == "string", "Default value must either be omitted or be a string")
175+
assert(type(default) == "string" or default == nil, "Default value must either be omitted or be a string")
177176
maxcount = maxcount or 1
178177
maxcount = tonumber(maxcount)
179178
assert(maxcount and maxcount>0 and maxcount<1000,"Maxcount must be a number from 1 to 999")
@@ -193,7 +192,7 @@ end
193192
--- As a final option it is possible to only use the expanded key (eg. `'--expanded-key'`) both with and
194193
--- without a value specified.
195194
--- 2. **desc**: a description for the argument to be shown in --help
196-
--- 3. **default**: *optional*; specify a default value (the default is "")
195+
--- 3. **default**: *optional*; specify a default value (the default is nil)
197196
--- 4. **callback**: *optional*; specify a function to call when this option is parsed (the default is nil)
198197
---
199198
--- ### Usage example
@@ -241,8 +240,7 @@ function cli:add_opt(key, desc, default, callback)
241240
end
242241

243242
-- set defaults
244-
if v == nil then default = false end -- no value, so its a flag
245-
if default == nil then default = "" end
243+
if v == nil then default = nil end -- no value, set it's a flag, so set default to nil
246244

247245
-- below description of full entry record, nils included for reference
248246
local entry = {
@@ -251,7 +249,7 @@ function cli:add_opt(key, desc, default, callback)
251249
desc = desc,
252250
default = default,
253251
label = key,
254-
flag = (default == false),
252+
flag = (v == nil), -- no value, so it's a flag
255253
value = default,
256254
callback = callback,
257255
}

0 commit comments

Comments
 (0)