Use a different prefix character parser = argparse.ArgumentParser(prefix_chars='%') parser.add_argument('expression')
args = parser.parse_args() print(args.expression) argparser is for allowing multiple command line options to be passed, providing default, controlling the number of arguments and the like. From: Python-list <python-list-bounces+gweatherby=uchc....@python.org> on behalf of Jach Feng <jf...@ms4.hinet.net> Date: Wednesday, January 25, 2023 at 12:25 PM To: python-list@python.org <python-list@python.org> Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument? *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. *** Chris Angelico 在 2023年1月25日 星期三下午1:16:25 [UTC+8] 的信中寫道: > On Wed, 25 Jan 2023 at 14:42, Jach Feng <jf...@ms4.hinet.net> wrote: > > I was happy working with argparse during implement my script. To save the > > typing, I used a default equation for testing. > > > > sample = "-4^2+5.3*abs(-2-1)/2, abs(Abc)*(B+C)/D, (-3) * > > sqrt(1-(x1/7)*(y1/7)) * sqrt(abs((x0-4.5)/(y0-4)))" > > parser = argparse.ArgumentParser(description='Convert infix notation to > > postfix') > > parser.add_argument('infix', nargs='?', default=sample, help="....") > > > You're still not really using argparse as an argument parser. Why not > just do your own -h checking? Stop trying to use argparse for what > it's not designed for, and then wondering why it isn't doing what you > expect it to magically know. > > ChrisA I just don't get what you mean? > You're still not really using argparse as an argument parser. Why not just do > your own -h checking? Is a math equation not qualified as a command line "argument"? What criteria do you use when judging the quality of an "argument"? > Stop trying to use argparse for what it's not designed for, Even the author considers a positional argument begin with '-' is a legal argument. Below is a quote from its manual. "If you have positional arguments that must begin with - and don’t look like negative numbers, you can insert the pseudo-argument '--' which tells parse_args() that everything after that is a positional argument" > and then wondering why it isn't doing what you expect it to magically know." I don't expect magic, I expect the consistency of a parser. -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jfetVCL_U0v2RwXvkQWkim9VOYbZSJsWxRjWVlhpCDNHLoBWGyPJKGC_vh1Hwkrm3AzcX2KNEOUFKNNBx0tG$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!jfetVCL_U0v2RwXvkQWkim9VOYbZSJsWxRjWVlhpCDNHLoBWGyPJKGC_vh1Hwkrm3AzcX2KNEOUFKNNBx0tG$> -- https://mail.python.org/mailman/listinfo/python-list