Skip to content

Commit 684a028

Browse files
committed
Merge branch 'deining/master'
Closes #63 I modified the commits in the PR to fix the commit messages to be conventional-commit compatible and also fixed the merge conflicts at the end. Since the PR was opened in a legacy iteration of GitHub and from the OP's master branch I am unable to push the updates to the PR, so I merged manually.
2 parents 5e77d9e + 22ee99f commit 684a028

File tree

3 files changed

+130
-8
lines changed

3 files changed

+130
-8
lines changed

spec/core_spec.lua

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe("cliargs::core", function()
44
local cli
55

66
before_each(function()
7-
cli = require("cliargs.core")()
7+
cli = require("../src.cliargs.core")()
88
end)
99

1010
describe('#parse', function()
@@ -177,4 +177,115 @@ describe("cliargs::core", function()
177177
end)
178178
end)
179179
end)
180+
181+
describe('#read_defaults_from_ini_file', function()
182+
local args, err
183+
184+
before_each(function()
185+
cli:option('-c, --compress=VALUE', '...', 'lzma')
186+
cli:flag('-q, --quiet', '...', false)
187+
local config
188+
189+
config, err = cli:read_defaults('spec/fixtures/config.ini')
190+
191+
assert.equal(err, nil)
192+
assert.same(config, {
193+
compress = 'bz2',
194+
quiet = true,
195+
})
196+
197+
if config and not err then
198+
cli:load_defaults(config)
199+
end
200+
end)
201+
202+
it('works', function()
203+
args, err = cli:parse({})
204+
205+
assert.equal(err, nil)
206+
assert.same(args, {
207+
c = 'bz2',
208+
compress = 'bz2',
209+
q = true,
210+
quiet = true
211+
})
212+
end)
213+
end)
214+
215+
216+
describe('#read_defaults_from_ini_file_group_no_cast', function()
217+
local args, err
218+
219+
before_each(function()
220+
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
221+
cli:option('-p, --port=VALUE', '...', 8088)
222+
223+
local config
224+
225+
config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)
226+
227+
assert.equal(err, nil)
228+
assert.same(config, {
229+
host = 'localhost',
230+
port = 5432,
231+
})
232+
233+
if config and not err then
234+
cli:load_defaults(config)
235+
end
236+
end)
237+
238+
it('works', function()
239+
args, err = cli:parse({})
240+
241+
assert.equal(err, nil)
242+
assert.same(args, {
243+
h = 'localhost',
244+
host = 'localhost',
245+
p = 5432,
246+
port = 5432,
247+
})
248+
end)
249+
end)
250+
251+
252+
describe('#read_defaults_from_ini_file_group_with_cast', function()
253+
local args, err
254+
255+
before_each(function()
256+
cli:option('-h, --host=VALUE', '...', '127.0.0.1')
257+
cli:option('-p, --port=VALUE', '...', 8088)
258+
259+
local config
260+
261+
-- failing test case for #64 below
262+
-- config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', false)
263+
264+
-- intermediate: prevent failure with Travis CI
265+
config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true)
266+
267+
assert.equal(err, nil)
268+
assert.same(config, {
269+
host = 'localhost',
270+
port = 5432,
271+
})
272+
273+
if config and not err then
274+
cli:load_defaults(config)
275+
end
276+
end)
277+
278+
it('works', function()
279+
args, err = cli:parse({})
280+
281+
assert.equal(err, nil)
282+
assert.same(args, {
283+
h = 'localhost',
284+
host = 'localhost',
285+
p = 5432,
286+
port = 5432,
287+
})
288+
end)
289+
end)
290+
180291
end)

spec/fixtures/config.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[cli]
22
quiet = true
3-
compress = bz2
3+
compress = bz2
4+
[database]
5+
host=localhost
6+
port=5432

src/cliargs/core.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,22 @@ local function create_core()
207207
--- When this is left blank, we try to auto-detect the format from the
208208
--- file extension.
209209
---
210-
--- @param {boolean} [strict=false]
211-
--- Forwarded to [#load_defaults](). See that method for the parameter
212-
--- description.
210+
--- @param {string} [group="cli"]
211+
--- INI files only: group that lists the default values. For example:
212+
---
213+
--- [cli]
214+
--- quiet=true
215+
--- compress=lzma
216+
---
217+
--- @param {bool} [default=true]
218+
--- INI files only: whether or not boolean values ("true" and "false")
219+
--- will be cast into Lua booleans automatically. If set to false,
220+
--- string values "true" or "false" will be assigned to config value.
213221
---
214222
--- @return {true|union<nil, string>}
215223
--- Returns true on successful load. Otherwise, nil and an error
216224
--- message are returned instead.
217-
function cli:read_defaults(path, format)
225+
function cli:read_defaults(path, format, group, no_cast)
218226
if not format then
219227
format = path:match('%.([^%.]+)$')
220228
end
@@ -225,7 +233,7 @@ local function create_core()
225233
return nil, 'Unsupported file format "' .. format .. '"'
226234
end
227235

228-
return config_loader[loader](path)
236+
return config_loader[loader](path, group, no_cast)
229237
end
230238

231239
--- Define a required argument.
@@ -556,4 +564,4 @@ local function create_core()
556564
return cli
557565
end
558566

559-
return create_core
567+
return create_core

0 commit comments

Comments
 (0)