How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?

Jach Feng jfong at ms4.hinet.net
Tue Jan 24 22:09:37 EST 2023


Michael Torrie 在 2023年1月25日 星期三凌晨3:05:44 [UTC+8] 的信中寫道:
> On 1/23/23 18:58, Jach Feng wrote: 
> > More pathonic, but don't work. The '--' must be at index 1:-)
> I'm very confused. Why are you even using argparse, since if you put -- 
> at index 1 then argparse wont't do any argument parsing at all. If all 
> you want is the expression on the command line, just access it directly. 
> If it's spread out with spaces, you can do something like this to put 
> it back together: 
> 
> expression = " ".join(sys.argv[1:] 
> 
> Otherwise the standard unix way of doing this is to require the user to 
> either provide the -- himself, or put the expression in quotes so it's 
> one unit.
Maybe a condition is required before the modification,
if len(sys.argv) > 1 and not '-h' in sys.argv:
    sys.argv.insert(1, '--')


More information about the Python-list mailing list