Jach Feng wrote:
Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道:
Fail on command line,

e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2"
usage: infix2postfix.py [-h] [infix]
infix2postfix.py: error: unrecognized arguments: -4^2+5.3*abs(-2-1)/2

Also fail in REPL,

e:\Works\Python>py
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:08:11) [MSC v.1928 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to 
postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
usage: [-h]
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2

Just can't figure out where is wrong!?

--Jach
OK, I take a quick try to use argv directly. I write the code in a way even get 
rid of the -h option.

import sys
if len(sys.argv) == 1:
....infix = " "
....print("Usage: a math equation must follow!")
else:
....infix = "".join(sys.argv[1:])

Simple enough, right? But unfortunately it didn't survive. The ^ symbol was 
lost!

e:\Works\Python>py infix2postfix.py
Usage: a math equation must follow!

e:\Works\Python>py infix2postfix.py -4^2 +5.3  *  abs(-2-1)/2
-42 5.3 -2 1 - abs * 2 / +

Hmm...

I'm not certain, but I think that's a shell feature again. In Windows' command prompt, "^" can be used at the end of a line to indicate that the command continues on the next line. I'm not sure what happens if it's in the middle of a line (and not on Windows to be able to check), but it's quite possible that it just gets ignored.

Enclosing your argument in quotes might help. Again, this is a feature of the shell, not your application, and other shells might behave differently. You probably don't want to go down the line of trying to document this kind of thing in your applications usage information, because it won't work like that for someone using e.g. the bash shell (which can be run on Windows).

--
Mark.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to