Closed SolitudeSF closed 5 years ago
I had to think about this for a minute, but this is the expected behavior. This is the expected behavior because when -h
or --help
is given, the parser assumes that rather than parsing and continuing with the program, the help text should be displayed and the program (or section of the program) should abort.
So I would do:
import argparse
let
p = newParser("test"): discard
try:
let opts = p.parse()
except ShortCircuit:
discard
However, there are some problems with this:
ShortCircuit
isn't great. Something with Help
in the name would be better.opts
, you can only use opts
inside the try:
statement, which might be annoying.I'll see what I can do about these. Thanks for the report!
maybe the parser should also store if help
is invoked in some special field, so user can manually check it and decide what to do?
I'm just re-reading https://nim-lang.org/docs/tut2.html#exceptions and thinking maybe using an exception here is not the Nim way to do things:
A convention is that exceptions should be raised in exceptional cases: For example, if a file cannot be opened, this should not raise an exception since this is quite common (the file may not exist).
Perhaps the result of parse
should contain what is now returned as opts
. So like this:
import argparse
let
p = newParser("test"):
flag("-a")
res = p.parse(@["-a"])
assert res.help_flag == false
assert res.opts.a == true
The API for run()
would not change.
What do you think of that?
opts already contains some special field help__special
, would there be room for one more? introducing wrapper just for one value seems like overkill.
or parse could return tuple
of current opts and help flag.
What if I renamed help__special
to help__shortcircuit
and then documented that. Would that work?
is help__special
that help
invocation flag? documenting it would work, just exception has to go. also whats the reason for double underscore? it just triggers style check.
Hmmm... and what should it return in the case of subcommands?
import argparse
let
p = newParser("test"):
command("something"):
discard
res = p.parse(@["something", "-h"])
The reason for the double underscore was to avoid conflicts with user-defined keys. And since users can't use double underscores, it works well :)
I think I'll just name the flag .help
and remove the exception.
underscores are ignored completely.
import argparse
let
p = newParser("test"):
flag("--helpspecial")
opts = p.parse
this triggers Error: attempt to redefine: 'help__special'
but thats offtopic.
I think I'll just name the flag .help and remove the exception.
that would be the most expected behavior. i didn't dig into subcommands, so i dont know how should they behave.
great, thank you. bumping a minor version would be nice.
Done! Thanks for reminding me :)
thanks for the great library. actually gonna switch now from parseopt
to this in my projects.
Same here. I've got a crappy diy solution I've never been happy with. This is much better than what I've done.
with this minimal program will output the help contents and throw
Error: unhandled exception: [ShortCircuit]
when program invoked with any help flag. Am i missing something?