Skip to content

Commit

Permalink
Remove error condition for --help flag
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMagician23 committed Dec 13, 2023
1 parent 3e88e45 commit 538417a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 54 deletions.
34 changes: 0 additions & 34 deletions spec/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,12 @@ describe("cliargs::core", function()
assert.equal(arguments[1], "--quiet")
end)

it("generates the help listing but does not print it to STDOUT", function()
local res, err = cli:parse({'--help'})

assert.equal(type(res), "nil")
assert.equal(type(err), "string")
end)

it("returns error strings but does not print them to STDOUT", function()
local res, err = cli:parse({ "arg1" })

assert.equal(type(res), "nil")
assert.equal(type(err), "string")
end)

describe('displaying the help listing', function()
local res, err

before_each(function()
cli:argument('INPUT', '...')
cli:flag('--quiet', '...')
end)

after_each(function()
assert.equal(type(res), "nil")
assert.equal(type(err), "string")
assert.equal(err, cli.printer.generate_help_and_usage())
end)

it('works with --help in the beginning', function()
res, err = helpers.parse(cli, '--help something')
end)

it('works with --help in the end of options', function()
res, err = helpers.parse(cli, '--quiet --help something')
end)

it('works with --help after an argument', function()
res, err = helpers.parse(cli, '--quiet something --help')
end)
end)
end)

describe('#parse - the --__DUMP__ special option', function()
Expand Down
27 changes: 7 additions & 20 deletions src/cliargs/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ function p.invoke_command(args, options, done)
return done()
end

function p.print_help(args, printer, done)
-- has --help or -h ? display the help listing and abort!
for _, v in pairs(args) do
if v == "--help" or v == "-h" then
return nil, printer.generate_help_and_usage()
end
end

return done()
end

function p.track_dump_request(args, done)
-- starts with --__DUMP__; set dump to true to dump the parsed arguments
if args[1] == "--__DUMP__" then
Expand Down Expand Up @@ -300,15 +289,13 @@ return function(arguments, options, printer)
-- the spiral of DOOM:
return p.invoke_command(args, options, function()
return p.track_dump_request(args, function(dump, args_without_dump)
return p.print_help(args_without_dump, printer, function()
return p.process_arguments(args_without_dump, options, function(values, arg_count)
return p.validate(options, arg_count, function()
if dump then
return nil, printer.dump_internal_state(values)
else
return p.collect_results(values, options)
end
end)
return p.process_arguments(args_without_dump, options, function(values, arg_count)
return p.validate(options, arg_count, function()
if dump then
return nil, printer.dump_internal_state(values)
else
return p.collect_results(values, options)
end
end)
end)
end)
Expand Down

0 comments on commit 538417a

Please sign in to comment.