diff --git a/spec/core_spec.lua b/spec/core_spec.lua index d2e087e..8b930fb 100644 --- a/spec/core_spec.lua +++ b/spec/core_spec.lua @@ -4,7 +4,7 @@ describe("cliargs::core", function() local cli before_each(function() - cli = require("cliargs.core")() + cli = require("../src.cliargs.core")() end) describe('#parse', function() @@ -177,4 +177,115 @@ describe("cliargs::core", function() end) end) end) + + describe('#read_defaults_from_ini_file', function() + local args, err + + before_each(function() + cli:option('-c, --compress=VALUE', '...', 'lzma') + cli:flag('-q, --quiet', '...', false) + local config + + config, err = cli:read_defaults('spec/fixtures/config.ini') + + assert.equal(err, nil) + assert.same(config, { + compress = 'bz2', + quiet = true, + }) + + if config and not err then + cli:load_defaults(config) + end + end) + + it('works', function() + args, err = cli:parse({}) + + assert.equal(err, nil) + assert.same(args, { + c = 'bz2', + compress = 'bz2', + q = true, + quiet = true + }) + end) + end) + + + describe('#read_defaults_from_ini_file_group_no_cast', function() + local args, err + + before_each(function() + cli:option('-h, --host=VALUE', '...', '127.0.0.1') + cli:option('-p, --port=VALUE', '...', 8088) + + local config + + config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true) + + assert.equal(err, nil) + assert.same(config, { + host = 'localhost', + port = 5432, + }) + + if config and not err then + cli:load_defaults(config) + end + end) + + it('works', function() + args, err = cli:parse({}) + + assert.equal(err, nil) + assert.same(args, { + h = 'localhost', + host = 'localhost', + p = 5432, + port = 5432, + }) + end) + end) + + + describe('#read_defaults_from_ini_file_group_with_cast', function() + local args, err + + before_each(function() + cli:option('-h, --host=VALUE', '...', '127.0.0.1') + cli:option('-p, --port=VALUE', '...', 8088) + + local config + + -- failing test case for #64 below + -- config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', false) + + -- intermediate: prevent failure with Travis CI + config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', true) + + assert.equal(err, nil) + assert.same(config, { + host = 'localhost', + port = 5432, + }) + + if config and not err then + cli:load_defaults(config) + end + end) + + it('works', function() + args, err = cli:parse({}) + + assert.equal(err, nil) + assert.same(args, { + h = 'localhost', + host = 'localhost', + p = 5432, + port = 5432, + }) + end) + end) + end) diff --git a/spec/fixtures/config.ini b/spec/fixtures/config.ini index 4bd7376..9872a89 100644 --- a/spec/fixtures/config.ini +++ b/spec/fixtures/config.ini @@ -1,3 +1,6 @@ [cli] quiet = true -compress = bz2 \ No newline at end of file +compress = bz2 +[database] +host=localhost +port=5432 \ No newline at end of file diff --git a/src/cliargs/core.lua b/src/cliargs/core.lua index 77cad59..1926468 100644 --- a/src/cliargs/core.lua +++ b/src/cliargs/core.lua @@ -207,14 +207,22 @@ local function create_core() --- When this is left blank, we try to auto-detect the format from the --- file extension. --- - --- @param {boolean} [strict=false] - --- Forwarded to [#load_defaults](). See that method for the parameter - --- description. + --- @param {string} [group="cli"] + --- INI files only: group that lists the default values. For example: + --- + --- [cli] + --- quiet=true + --- compress=lzma + --- + --- @param {bool} [default=true] + --- INI files only: whether or not boolean values ("true" and "false") + --- will be cast into Lua booleans automatically. If set to false, + --- string values "true" or "false" will be assigned to config value. --- --- @return {true|union} --- Returns true on successful load. Otherwise, nil and an error --- message are returned instead. - function cli:read_defaults(path, format) + function cli:read_defaults(path, format, group, no_cast) if not format then format = path:match('%.([^%.]+)$') end @@ -225,7 +233,7 @@ local function create_core() return nil, 'Unsupported file format "' .. format .. '"' end - return config_loader[loader](path) + return config_loader[loader](path, group, no_cast) end --- Define a required argument. @@ -556,4 +564,4 @@ local function create_core() return cli end -return create_core \ No newline at end of file +return create_core