1
\$\begingroup\$

I'm parsing commandline arguments in lua with a command parser library (penlight lapp). This works fine, is easy to design and flexible enough for me. However, now I have a table containing all the parsed arguments, so I'm still stuck with the processing, that is calling functions or changing settings etc. within my program. Some of these options are checks or similar operations that abort normal program behaviour, so I need to exit the main program before entering the normal routines. To implement such behaviour, I'm essentially using a large if-elseif chain, which is complex and hard to maintain.

Here is some code, but my question is of a general nature: Most not-trivial-utilities-with-more-than-two-arguments need to solve this, there has to be a way to do this somewhat elegantly and non-complex. How?

local cmdargs = [[
    Utility

    --foo       enables foo
    --bar       check for bar, then exit
]]
local args = parse_args(cmdargs)

if args.foo then
    set_switch()
end
if args.bar then
    check_something()
    exit(0)
end
do_main_stuff()

My example has only two arguments, but with 20+ arguments this style becomes really unreadable.

\$\endgroup\$

0

Browse other questions tagged or ask your own question.