I am parsing commandline arguments in lua with a command parser library (penlight lapp
). This works high-quality, is straightforward to design and versatile sufficient for me.
Nevertheless, now I’ve a desk containing all of the parsed arguments, so I am nonetheless caught with the processing, that’s calling features or altering settings and so forth. inside my program. A few of these choices are checks or comparable operations that abort regular program behaviour, so I must exit the principle program earlier than getting into the conventional routines. To implement such behaviour, I am primarily utilizing a big if-elseif chain, which is complicated and arduous to take care of.
Right here is a few code, however my query is of a normal nature: Most not-trivial-utilities-with-more-than-two-arguments want to unravel this, there needs to be a means to do that considerably elegantly and non-complex. How?
native cmdargs = [[
Utility
--foo enables foo
--bar check for bar, then exit
]]
native args = parse_args(cmdargs)
if args.foo then
set_switch()
finish
if args.bar then
check_something()
exit(0)
finish
do_main_stuff()
My instance has solely two arguments, however with 20+ arguments this type turns into actually unreadable.