How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Mark Bourne
nntp.mbourne at spamgourmet.com
Sun Jan 29 09:39:00 EST 2023
Jach Feng wrote:
> Thank you for detail explanation of the role the shell is involved in this problem. I'm very appreciated!
>
> It seems that a CLI app may become very complex when dealing with different kind of shell, and may not be possible to solve its problem. But the good thing in my app is that I need only to handle math equation:-)
If you want to try to tell the user how to deal with their shell's
requirements for quoting arguments, regardless of which shell they might
be using, yes, that explanation would become very complicated. It
doesn't affect the rest of the implementation of the application though
- the user just needs to know how to use their shell to pass the
arguments they want into the application. That's really something they
should look up in their shell's documentation, rather than something
your application should attempt to document.
Since your application requires equations to be passed in, and they're
quite likely to include characters handled specially by the shell
(space, "*" and "^" have already come up in this thread, but there may
be others), it may be worth highlighting that, but directing the user to
consult the documentation for their shell rather than assuming a
particular shell and attempting to cover all its features and limitations.
>> So why so much objection to explaining the need for "--"?
> Because of using " to enclose a space separated string is a common convention, and adding a "--" is not:-)
If you don't consider use of "--" to be a common convention, that seems
even more reason to mention it in your application's documentation.
Telling the user to use quotes around an argument containing spaces is
nothing to do with your application, and might not even be applicable if
they use a different shell to call your application. In most shells
I've come across, there are also various other characters that need
special handling (either quoting or escaping) - but exactly which
characters again depends on the shell. Yet you seem quite happy to
document that one particular case in your usage information.
Using "--" is also a common convention (as I and others have mentioned),
although perhaps not as common in Windows (where it's more common to use
"/" rather than "-" for options anyway). But more to the point, it is a
feature that is implemented under your application's control (if you
don't want this feature, don't use argparse). Use of "--" is applicable
regardless of which shell your user calls it from, and other
applications might not use that convention even if called from the same
shell, so it seems *more* in scope for your application to document than
using quotes around spaces.
--
Mark.
More information about the Python-list
mailing list