How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
Chris Angelico
rosuav at gmail.com
Sat Jan 28 23:55:34 EST 2023
On Sun, 29 Jan 2023 at 14:29, Jach Feng <jfong at ms4.hinet.net> 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:-)
>
> > 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:-)
Double hyphen is incredibly common.
On most Unix shells, double quotes are just one way of marking that a
space should be included in an argument rather than separating (others
include single quotes, escaping the space with a backslash, and
changing the field separator). Of course, everything is conventions,
not requirements, but it's generally better to let the person's shell
define the argument splitting; that way, if your script is called from
another process, it's dead easy to control the splitting yourself
(just pass an array/list of arguments to the subprocess spawner).
Maybe you come from Windows, where there are fewer conventions and
less shell parsing, but argparse follows Unix conventions, so if you
don't want Unix conventions, don't use argparse. Just read sys.argv
directly.
ChrisA
More information about the Python-list
mailing list