usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
On 1/21/2023 10:11 PM, Jach Feng wrote:Sorry to cause confusion here. I don't know there is a Pypi project with the same name infix2postfix.py:-(
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?It just doesn't work like that. If you download the package, there is
only one python file, __init__.py. This file contains one class. It
has a demo at the end, commented out. If you uncomment those lines and
run the file, you get a result printed.
These remarks are based on downloading the link for the source
distribution from Pypi
(https://pypi.org/project/infix2postfix/). When I installed it with
pip, nothing seems to have gotten installed although pip went through
the motions and claimed it was. So I just downloaded the source package.
The test expression is "-(a*b)+(c+d)-(a+b+c+d)". The test output for
this is "ab*-cd++ab+c+d+-".
If you substitute your expression, the result is
abs1-2-*2/3.5+2^4-
This may or may not be correct. I'm not sure but I think it's as
intended except for reversing "3.5". But maybe that's right, I'm not
too sure. Notice that this file is in its first release, version 0.0.1
- the metadata that says it's 'Development Status :: 5 -
Production/Stable' seems to be bogus. So it may very well be buggy.
At any rate, if you want to use it in a program that can accept
arguments, you will have to write that part yourself. And the
expression you feed it would need to be a single string, meaning it has
to be quoted on the command line as you have done (although on Windows
you should be using double quotes instead of single quotes).
As for argparse, it isn't doing what you want because you haven't told
it what to do with the arguments.
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
parser.add_argument("expression")
parser.parse_args(["4^2+5.3*abs(-2-1)/2"]) Namespace(expression='4^2+5.3*abs(-2-1)/2')
usage: [-h] expressionparser.parse_args(["-4^2+5.3*abs(-2-1)/2"])
parser.parse_args(["0-4^2+5.3*abs(-2-1)/2"]) Namespace(expression='0-4^2+5.3*abs(-2-1)/2')
parser.parse_args(["--", "-4^2+5.3*abs(-2-1)/2"]) Namespace(expression='-4^2+5.3*abs(-2-1)/2')
./convert_infix.py -- '-4^2+5.3*abs(-2-1)/2'(it's probably a good idea to quote the expression, in case it includes
On 1/21/2023 10:11 PM, Jach Feng wrote:[...]
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
[...]usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
It just doesn't work like that.
If you substitute your expression, the result is
abs1-2-*2/3.5+2^4-
This may or may not be correct. I'm not sure but I think it's as intended except for reversing "3.5". But maybe that's right, I'm not too sure.
Notice that this file is in its first release, version 0.0.1 - the metadata that says it's 'Development Status :: 5 - Production/Stable' seems to be bogus. So it may very well be buggy.
Notice that this file is in its first release, version 0.0.1 - the metadata >> that says it's 'Development Status :: 5 - Production/Stable' seems to beIt is at least too incomplete to be useful. It handles only single
bogus. So it may very well be buggy.
letters as operands and treats everything else (except parentheses) as an operator, handling
only +, -, *, / and ^ correctly (actually, ^ is typically right
associative, so that's arguably wrong, too).
e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2"
On 1/21/2023 10:11 PM, Jach Feng wrote:Sorry to cause confusion here. I don't know there is a Pypi project with the same name infix2postfix.py:-(
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?It just doesn't work like that. If you download the package, there is
only one python file, __init__.py. This file contains one class. It
has a demo at the end, commented out. If you uncomment those lines and
run the file, you get a result printed.
These remarks are based on downloading the link for the source
distribution from Pypi (https://urldefense.com/v3/__https://pypi.org/project/infix2postfix/__;!!Cn_UX_p3!gqKmYLlyUndAzxmJsqCB429izQ-2-KMbpGP2eVzp_iDKtbgQXfrCu21UBvepq-F9EXb4SJwP516MHeUFMBtW$ ). When I installed it with
pip, nothing seems to have gotten installed although pip went through
the motions and claimed it was. So I just downloaded the source package.
The test expression is "-(a*b)+(c+d)-(a+b+c+d)". The test output for
this is "ab*-cd++ab+c+d+-".
If you substitute your expression, the result is
abs1-2-*2/3.5+2^4-
This may or may not be correct. I'm not sure but I think it's as
intended except for reversing "3.5". But maybe that's right, I'm not
too sure. Notice that this file is in its first release, version 0.0.1
- the metadata that says it's 'Development Status :: 5 -
Production/Stable' seems to be bogus. So it may very well be buggy.
At any rate, if you want to use it in a program that can accept
arguments, you will have to write that part yourself. And the
expression you feed it would need to be a single string, meaning it has
to be quoted on the command line as you have done (although on Windows
you should be using double quotes instead of single quotes).
As for argparse, it isn't doing what you want because you haven't told
it what to do with the arguments.
-- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gqKmYLlyUndAzxmJsqCB429izQ-2-KMbpGP2eVzp_iDKtbgQXfrCu21UBvepq-F9EXb4SJwP516MHezHygV-$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-parser.parse_args("-4^2+5.3*abs(-2-1)/2")
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Jach Feng <jfong@ms4.hinet.net> writes:
e:\Works\Python>py infix2postfix.py "-4^2+5.3*abs(-2-1)/2"
Well, it's a nice exercise! But I only made it work for the
specific example given. I have not tested whether it always
works.
Fail on command line,Thank you for all your suggestions. From it, I get two solutions for my problem.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
Argparse is for parsing command line arguments and options.
If you just want to evaluate an Python expression, use eval( )
1) Modify the sys.argv by inserting an item '--' before parsing it, ie. sys.argv.insert(1, '--')
args = parser.parse_args()
2) By adding an extra space character before the leading '-' sign, ie. e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2"
-4 2 ^ 5.3 -2 1 - abs * 2 / +
But no idea how it works? and if it can survive in a newer argparse version?:-)
But for Jach Feng: the "--" is really expected as something the user
does when they invoke your programme, _explicitly_ saying that what
follows from here is not an argument. So the user is expected to type:
your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
where there are -x and -y options, then end of options, then an
argument, which would look like an option if there wasn't the "--"
argument.
On 2023-01-22 at 18:19:13 -0800,
Jach Feng <jfong@ms4.hinet.net> wrote:
1) Modify the sys.argv by inserting an item '--' before parsing it,
ie.
sys.argv.insert(1, '--')
args = parser.parse_args()
Please don't do that. :-)
In my mind, sys.argv belongs to Python, not the application. Instead,
pass a newly created argument list to parse_args:
args = parser.parse_args(['--'] + sys.argv)
On 2023-01-22 at 18:19:13 -0800,
Jach Feng <jfong@ms4.hinet.net> wrote:
1) Modify the sys.argv by inserting an item '--' before parsing it, ie.
sys.argv.insert(1, '--')
args = parser.parse_args()
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> wrote:The '--' requirement makes its usage less instinctive, and handling argv directly makes me loss the benefit of using '-h':-)
But for Jach Feng: the "--" is really expected as something the user
does when they invoke your programme, _explicitly_ saying that what follows from here is not an argument. So the user is expected to type:
your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
where there are -x and -y options, then end of options, then anAnd if you DON'T expect the user to enter the "--", then why use
argument, which would look like an option if there wasn't the "--" argument.
argparse? You can just check argv directly to get your arguments.
This entire thread is a massive "how can I use X to do Y?" problem.
ChrisA
Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中寫道:
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> wrote:
But for Jach Feng: the "--" is really expected as something the user
does when they invoke your programme, _explicitly_ saying that what follows from here is not an argument. So the user is expected to type:
your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
where there are -x and -y options, then end of options, then anAnd if you DON'T expect the user to enter the "--", then why use
argument, which would look like an option if there wasn't the "--" argument.
argparse? You can just check argv directly to get your arguments.
This entire thread is a massive "how can I use X to do Y?" problem.
ChrisAThe '--' requirement makes its usage less instinctive, and handling argv directly makes me loss the benefit of using '-h':-)
Any easy way to "pull the argument out of argv directly" before parse_args()?:-)If you do that, you'll never be able to have any actual options, soOn 2023-01-22 at 18:19:13 -0800,
Jach Feng <jf...@ms4.hinet.net> wrote:
1) Modify the sys.argv by inserting an item '--' before parsing it, ie. >>> sys.argv.insert(1, '--')
args = parser.parse_args()
using argparse seems like overkill. Just pull the argument out of
argv directly.
--
Greg
On 2023-01-22 at 18:19:13 -0800,More pathonic, but don't work. The '--' must be at index 1:-)
Jach Feng <jf...@ms4.hinet.net> wrote:
1) Modify the sys.argv by inserting an item '--' before parsing it, ie. sys.argv.insert(1, '--')Please don't do that. :-)
args = parser.parse_args()
In my mind, sys.argv belongs to Python, not the application. Instead,
pass a newly created argument list to parse_args:
args = parser.parse_args(['--'] + sys.argv)
This approach (adjusting the actual arguments) will work until your
program actually has options.
2) By adding an extra space character before the leading '-' sign, ie. e:\Works\Python>py infix2postfix.py " -4^2+5.3*abs(-2-1)/2"
-4 2 ^ 5.3 -2 1 - abs * 2 / +
But no idea how it works? and if it can survive in a newer argparse version?:-)It works because argparse checks the first character of each argument,
and *doesn't* strip/trim whitespace. So "-x" looks like an option, and
" -x" looks an argument.
usage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
usage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
: error: unrecognized arguments: -4.3+5
On 23Jan2023 17:58, Jach Feng <jf...@ms4.hinet.net> wrote:Hmm, good to you. During experiments in these days, I had found argparse shows some strange/unstable behaviors in dealing with the leading '-' situation.
This error doesn't look like "-4.3+5 looks like an option" but insteadusage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
: error: unrecognized arguments: -4.3+5
"we don't expect any arguments after "infix".
Not personally a fan of argparse myself, but then I have my own
elaborate command line framework which generally uses getopt for the
option stuff.
Cheers,
Cameron Simpson <c...@cskk.id.au>
On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net> wrote:
Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中寫道:
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> wrote:The '--' requirement makes its usage less instinctive, and handling argv directly makes me loss the benefit of using '-h':-)
And if you DON'T expect the user to enter the "--", then why use
But for Jach Feng: the "--" is really expected as something the user
does when they invoke your programme, _explicitly_ saying that what
follows from here is not an argument. So the user is expected to type: >>>>
your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
where there are -x and -y options, then end of options, then an
argument, which would look like an option if there wasn't the "--"
argument.
argparse? You can just check argv directly to get your arguments.
This entire thread is a massive "how can I use X to do Y?" problem.
ChrisA
if "-h" in sys.argv: usage()
else: do_stuff_with(sys.argv[1:])
What is argparse really doing for you?
Can you stop please
Sent from Yahoo Mail for iPhone <https://overview.mail.yahoo.com/?.src=iOS>
On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin
<list1@tompassin.net> wrote:
On 1/23/2023 9:12 PM, Chris Angelico wrote:
> On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net
<mailto:jfong@ms4.hinet.net>> wrote:
>>
>> Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中
寫道:
>>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au
<mailto:c...@cskk.id.au>> wrote:
>>>>
>>>> But for Jach Feng: the "--" is really expected as something
the user
>>>> does when they invoke your programme, _explicitly_ saying that
what
>>>> follows from here is not an argument. So the user is expected
to type:
>>>>
>>>> your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
>>>>
>>>> where there are -x and -y options, then end of options, then an
>>>> argument, which would look like an option if there wasn't the "--"
>>>> argument.
>>> And if you DON'T expect the user to enter the "--", then why use
>>> argparse? You can just check argv directly to get your arguments.
>>>
>>> This entire thread is a massive "how can I use X to do Y?" problem.
>>>
>>> ChrisA
>> The '--' requirement makes its usage less instinctive, and
handling argv directly makes me loss the benefit of using '-h':-)
>
> if "-h" in sys.argv: usage()
> else: do_stuff_with(sys.argv[1:])
>
> What is argparse really doing for you?
I second this. "if '-h' in sys.argv:" is usually what I do.
Alternatively, you could use "--arg=" syntax and place your string
"-4^2+5.3*abs(-2-1)/2" its right-hand side":
infix2postfix [options] "--infix=-4^2+5.3*abs(-2-1)/2"
This shouldn't be too hard for a user to work with. You could scan the
argument list for the presence of "--infix=" and display the help
message if it isn't there.
--
https://mail.python.org/mailman/listinfo/python-list
<https://mail.python.org/mailman/listinfo/python-list>
Can you stop please
Sent from Yahoo Mail for iPhone <https://urldefense.com/v3/__https://overview.mail.yahoo.com/?.src=iOS__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIyHt-2PcA$ >
On Tuesday, January 24, 2023, 10:12 AM, Thomas Passin
<list1@tompassin.net> wrote:
On 1/23/2023 9:12 PM, Chris Angelico wrote:
> On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net
<mailto:jfong@ms4.hinet.net>> wrote:
>>
>> Chris Angelico $B:_(B 2023$BG/(B1$B7n(B24$BF|(B $B@14|Fs@6Zo(B5:00:27 [UTC+8] $BE*?.Cf(B
$BUmF;!'(B
>>> On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au
<mailto:c...@cskk.id.au>> wrote:
>>>>
>>>> But for Jach Feng: the "--" is really expected as something
the user
>>>> does when they invoke your programme, _explicitly_ saying that
what
>>>> follows from here is not an argument. So the user is expected
to type:
>>>>
>>>> your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
>>>>
>>>> where there are -x and -y options, then end of options, then an
>>>> argument, which would look like an option if there wasn't the "--"
>>>> argument.
>>> And if you DON'T expect the user to enter the "--", then why use
>>> argparse? You can just check argv directly to get your arguments.
>>>
>>> This entire thread is a massive "how can I use X to do Y?" problem.
>>>
>>> ChrisA
>> The '--' requirement makes its usage less instinctive, and
handling argv directly makes me loss the benefit of using '-h':-)
>
> if "-h" in sys.argv: usage()
> else: do_stuff_with(sys.argv[1:])
>
> What is argparse really doing for you?
I second this. "if '-h' in sys.argv:" is usually what I do.
Alternatively, you could use "--arg=" syntax and place your string
"-4^2+5.3*abs(-2-1)/2" its right-hand side":
infix2postfix [options] "--infix=-4^2+5.3*abs(-2-1)/2"
This shouldn't be too hard for a user to work with. You could scan the
argument list for the presence of "--infix=" and display the help
message if it isn't there.
--python-list__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIykR5ILj4$>
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIykR5ILj4$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/
<https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!g3YRQIW4wminyTVpjV9kATW-QRZ1YhvDi82oJvatyhNe_OABSPTU9c8KIOlztBNofs690OPwUdIykR5ILj4$ >
More pathonic, but don't work. The '--' must be at index 1:-)
usage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
: error: unrecognized arguments: -4.3+5
On Tue, 24 Jan 2023 at 13:09, Jach Feng <jfong@ms4.hinet.net> wrote:
Chris Angelico 在 2023年1月24日 星期二清晨5:00:27 [UTC+8] 的信中寫道:
On Tue, 24 Jan 2023 at 07:47, Cameron Simpson <c...@cskk.id.au> wrote:The '--' requirement makes its usage less instinctive, and handling argv directly makes me loss the benefit of using '-h':-)
And if you DON'T expect the user to enter the "--", then why use
But for Jach Feng: the "--" is really expected as something the user
does when they invoke your programme, _explicitly_ saying that what
follows from here is not an argument. So the user is expected to type: >>>>
your_script -x -y -- "-4^2+5.3*abs(-2-1)/2"
where there are -x and -y options, then end of options, then an
argument, which would look like an option if there wasn't the "--"
argument.
argparse? You can just check argv directly to get your arguments.
This entire thread is a massive "how can I use X to do Y?" problem.
ChrisA
if "-h" in sys.argv: usage()
else: do_stuff_with(sys.argv[1:])
What is argparse really doing for you?
Can you stop please
On 23Jan2023 17:58, Jach Feng <jf...@ms4.hinet.net> wrote:Hmm, good to you. During experiments in these days, I had found argparse shows some strange/unstable behaviors in dealing with the leading '-' situation.
This error doesn't look like "-4.3+5 looks like an option" but insteadusage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
: error: unrecognized arguments: -4.3+5
"we don't expect any arguments after "infix".
Not personally a fan of argparse myself, but then I have my own
elaborate command line framework which generally uses getopt for the
option stuff.
Cheers,
Cameron Simpson <c...@cskk.id.au>
If I understood the issue, the problem is the unary minus at the start of the expression.looking "-abs(...)..." that could look like -a followed by something.
So if you know the expression is in that argument, would it make sense to pad it in one of many ways that are otherwise harmless?
Could you put parens on both sides of "-4^2+5.3*abs(-2-1)/2":
"(-4^2+5.3*abs(-2-1)/2)"
Or perhaps place a zero in front as in the awkward case where it begins with a minus sign:
"0 -4^2+5.3*abs(-2-1)/2"
Some people suggest you ask the user to modify what they type in and such changes may work. Your program could also potentially diddle with argv and recognize it albeit as you allow calling a function like abs() I can easily imagine a potentially valid
I see a deeper issue with interactions at the command shell level if parts of the arithmetic expression are evaluated or changed before python is even invoked.The user may not know any unix idiom, or even don't know Python. Ask them to add extra characters in their equations to avoid the argparse conflict is strange to me.
Then again, I may be misunderstanding the issue.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmai...@python.org> On Behalf Of Jach Feng
Sent: Tuesday, January 24, 2023 2:21 AM
To: pytho...@python.org
Subject: Re: How to make argparse accept "-4^2+5.3*abs(-2-1)/2" string argument?
cameron...@gmail.com 在 2023年1月24日 星期二下午2:05:33 [UTC+8] 的信中寫道:
On 23Jan2023 17:58, Jach Feng <jf...@ms4.hinet.net> wrote:
This error doesn't look like "-4.3+5 looks like an option" but insteadusage: [-h] infixparser.parse_args(['--', 'infix2postfix.py', '-4.3+5'])
: error: unrecognized arguments: -4.3+5
"we don't expect any arguments after "infix".
Not personally a fan of argparse myself, but then I have my own
elaborate command line framework which generally uses getopt for the option stuff.
Cheers,Hmm, good to you. During experiments in these days, I had found argparse shows some strange/unstable behaviors in dealing with the leading '-' situation.
Cameron Simpson <c...@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
On 1/23/23 18:58, Jach Feng wrote:Maybe a condition is required before the modification,
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.
Fail on command line,I was happy working with argparse during implement my script. To save the typing, I used a default equation for testing.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
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="....")
On Wed, 25 Jan 2023 at 14:42, Jach Feng <jfong@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.
On Wed, 25 Jan 2023 at 14:42, Jach Feng <jf...@ms4.hinet.net> wrote:I just don't get what you mean?
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
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."
On Wed, 25 Jan 2023 at 14:42, Jach Feng <jf...@ms4.hinet.net> wrote:I just don't get what you mean?
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
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."
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.
ChrisAI 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"?
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:
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.
I just don't get what you mean?
and then uses those different kinds of argumentmore than one kind of argument <<<
for different purposes <<<.
Fail on command line,I appreciate every comment/suggestion in this thread even sometimes I may not fully understand what they mean. To me, argparse has been just a tool which I can use in a CLI app. I use it as long as the situation allows, even it's as simple as only one
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
Now I understand some oppose this idea and saying that you shouldn't use a kitchen knife to cut a cake:-)
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 just don't get what you mean?
I was happy working with argparse during implement my script. To save the typing, I used a default equation for testing.You're still not really using argparse as an argument parser. Why not
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="....")
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
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"?
To me, argparse has been just a tool which I can use in a CLI app.
Fail on command line,I have to admit that I don't know the background upon which the argparse was built. The good side is that I don't carry its historical knowledge ( or burden?), that's why I can use it in a new way which may make someone feel uneasy.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道:
Fail on command line,I have to admit that I don't know the background upon which the argparse was built. The good side is that I don't carry its historical knowledge ( or burden?), that's why I can use it in a new way which may make someone feel uneasy.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
The reason I am still keep on using argparse on this "one positional argument only" CLI app is that I can't see what advantage I can get when writing code to handling sys.argv directly for the following two situations,
-----
e:\Works\Python>py infix2postfix.py
usage: infix2postfix.py [-h] infix
infix2postfix.py: error: the following arguments are required: infix
e:\Works\Python>py infix2postfix.py -h
usage: infix2postfix.py [-h] infix
Convert infix notation to postfix
positional arguments:
infix Put equations in quote if there is space in it and separate each one with a comma, ie.
"-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)))"
optional arguments:
-h, --help show this help message and exit
-----
comparing with code using the argparse below,
import argparse
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',
help='Put equations in quote if there is space in it and separate each one with a comma, ie. "{}"'.format(sample))
import sys
if len(sys.argv) > 1 and not '-h' in sys.argv:
....sys.argv.insert(1, '--')
args = parser.parse_args()
Jach Feng wrote:Thank you for detail explanation of the role the shell is involved in this problem. I'm very appreciated!
Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道:If you can use it in the way you want, that's great. I thought you were asking here because you *couldn't* use it the way you want.
Fail on command line,I have to admit that I don't know the background upon which the argparse was built. The good side is that I don't carry its historical knowledge ( or burden?), that's why I can use it in a new way which may make someone feel uneasy.
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
usage: [-h]parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2 >>
Just can't figure out where is wrong!?
--Jach
You're writing a command-line application. Your users are either
already familiar with the conventions of command-line applications, or they'll soon need to be.
If your application supports options beginning with a "-" (which is what argparse gives you, even if only the default "-h" and "--help" options
are actually valid), and you also need to be able to pass positional arguments which begin with a "-", you need some way for the user to
indicate whether any particular argument is an option or positional.
Using "--" to indicate that all subsequent arguments are positional, not options, is a common convention.
Some of your users might already be familiar with that convention from
other command-line tools. By using the same convention, you'd be making
it easier for them to use yours. Others might not already be familiar
with the convention, but by being consistent with other tools you'd
still be helping them when they eventually do come across it elsewhere.
You can always include an explanation of using "--" in your usage output
or other documentation.
Apart from dealing with how to pass an argument beginning with "-", your users will also likely have to deal with the features of whichever shell they're using, which you have no control over. For example, it's quite common to need to enclose an argument in quotes if it contains spaces.
It may also be necessary to use quotes if certain special characters are used, such as "*", "?" or "$" (perhaps "%" on Windows).
The reason I am still keep on using argparse on this "one positional argument only" CLI app is that I can't see what advantage I can get when writing code to handling sys.argv directly for the following two situations,
-----
e:\Works\Python>py infix2postfix.py
usage: infix2postfix.py [-h] infix
infix2postfix.py: error: the following arguments are required: infix
e:\Works\Python>py infix2postfix.py -h
usage: infix2postfix.py [-h] infix
Convert infix notation to postfix
positional arguments:
infix Put equations in quote if there is space in it and separate each one with a comma, ie.
"-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)))"
optional arguments:
-h, --help show this help message and exit
-----
comparing with code using the argparse below,
import argparse
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',
help='Put equations in quote if there is space in it and separate each one with a comma, ie. "{}"'.format(sample))
import sysPersonally, I do quite often use argparse even for simple cases, partly because it produces nice usage details, and deals with wrapping the
if len(sys.argv) > 1 and not '-h' in sys.argv:
....sys.argv.insert(1, '--')
args = parser.parse_args()
output to whatever width the console is. But I work with it rather than against it, and accept that a "--" is needed if a positional argument
starts with a "-".
I notice you explain the need to enclose the equation in quotes if it contains spaces. That's not even a feature of your application, but of
the shell used to call it. So why so much objection to explaining the
need for "--"?
Depending on the shell, there are other cases where quoting might be
needed, e.g. if the equation includes a "*" or "?" and happens to look
like a pattern matching files in the current directory (most shells I've used pass the pattern unchanged if it doesn't match any files). In
bash, if a "$" is used I'd need to enclose that in 'single quotes'
(can't even use "double quotes" for that one). You can't really expect
to document all that sort of thing, because it depends on which shell
the user happens to run your application from - you just have to trust
the user to know or learn how to use their shell.
--
Mark.
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:-)
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:-)
Fail on command line,I have to admit that I don't know the background upon which the argparse was built. The good side is that I don't carry its historical knowledge ( or burden?), that's why I can use it in a new way which may make someone feel uneasy.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix
notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) /
2
Just can't figure out where is wrong!?
--Jach
Mark Bourne 在 2023年1月28日 星期六晚上10:00:01 [UTC+8] 的信中寫道:
I notice you explain the need to enclose the equation in quotes if it
contains spaces. That's not even a feature of your application, but of
the shell used to call it. So why so much objection to explaining the
need for "--"?
Depending on the shell, there are other cases where quoting might be
needed, e.g. if the equation includes a "*" or "?" and happens to look
like a pattern matching files in the current directory (most shells I've
used pass the pattern unchanged if it doesn't match any files). In
bash, if a "$" is used I'd need to enclose that in 'single quotes'
(can't even use "double quotes" for that one). You can't really expect
to document all that sort of thing, because it depends on which shell
the user happens to run your application from - you just have to trust
the user to know or learn how to use their shell.
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.
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:-)
Fail on command line,OK, I take a quick try to use argv directly. I write the code in a way even get rid of the -h option.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
They're unrelated. As others have mentioned, "--" is _extremely_ common; almost _all_ UNIX command like programmes which handle -* style options honour the "--" convention. _argparse_ itself honours that convention, as does getopt etc.
They're unrelated. As others have mentioned, "--" is _extremely_
common; almost _all_ UNIX command like programmes which handle -*
style options honour the "--" convention. _argparse_ itself honours
that convention, as does getopt etc.
Mark Bourne 在 2023年1月28日 星期六晚上10:00:01 [UTC+8] 的信中寫道:
I notice you explain the need to enclose the equation in quotes if it
contains spaces. That's not even a feature of your application, but
of the shell used to call it. So why so much objection to explaining
the need for "--"?
Depending on the shell, there are other cases where quoting might be
needed, e.g. if the equation includes a "*" or "?" and happens to
look like a pattern matching files in the current directory (most
shells I've used pass the pattern unchanged if it doesn't match any
files). In bash, if a "$" is used I'd need to enclose that in 'single quotes'
(can't even use "double quotes" for that one). You can't really
expect to document all that sort of thing, because it depends on
which shell the user happens to run your application from - you just
have to trust the user to know or learn how to use their shell.
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.
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:-)
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.
Although today you could say POSIX is the reason for many things including the use of "--" I hesitate to mention I and many others used that convention long before as a standard part of many UNIX utilities.
Like many other such things, you build things first and by the time
you standardize, ...
I have to admit that I don't know the background upon which the
argparse was built. The good side is that I don't carry its historical knowledge ( or burden?), that's why I can use it in a new way
which may make someone feel uneasy.
The reason I am still keep on using argparse on this "one positional
argument only" CLI app is that I can't see what advantage I can get
when writing code to handling sys.argv directly for the following two situations,
Jach Feng 在 2023年1月22日 星期日上午11:11:22 [UTC+8] 的信中寫道:
Fail on command line,OK, I take a quick try to use argv directly. I write the code in a way even get rid of the -h option.
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.
usage: [-h]import argparse
parser = argparse.ArgumentParser(description='Convert infix notation to postfix')
parser.parse_args("-4^2+5.3*abs(-2-1)/2")
: error: unrecognized arguments: - 4 ^ 2 + 5 . 3 * a b s ( - 2 - 1 ) / 2
Just can't figure out where is wrong!?
--Jach
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...
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:-)
You are technically correct but perhaps off the mark.mentioned, whitespace disappears if not somehow protected as in quotes.
Yes, a python program only sees what is handed to it by some shell if invoked a certain way.
The issue here is what you tell people using your program about what they need to type to get it to work. That means if their shell is going to make changes in what they typed, they need to know how to avoid unintended changes. As one example not
On 2023-01-29 at 16:51:20 +1100,
Cameron Simpson <cs@cskk.id.au> wrote:
They're unrelated. As others have mentioned, "--" is _extremely_
common;
almost _all_ UNIX command like programmes which handle -* style options
honour the "--" convention. _argparse_ itself honours that convention, as
does getopt etc.
And why do UNIX programs behave this way?
Because POSIX says they should:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 67:38:38 |
Calls: | 6,915 |
Files: | 12,379 |
Messages: | 5,431,813 |