Mailing List Archive

Q: argparse.add_argument()
I accidentally used 'argparse' like this in my Python 3.9 program:
parser.add_argument ("-c, --clean", dest="clean", action="store_true")
parser.add_argument ("-n, --dryrun", dest="dryrun", action="store_true")

instead of:
parser.add_argument ("-c", "--clean", dest="clean", action="store_true")
parser.add_argument ("-n", "--dryrun", dest="dryrun", action="store_true")

So any use of 'my-prog.py -cn' threw an error:
error: unrecognized arguments: -cn

So is there something that throws an error on this wrong use of
"-c, --clean" instead? Could be useful. Or is "-c, --clean" legit somehow?

--
--gv
--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
On 3/18/2023 2:02 PM, Gisle Vanem via Python-list wrote:
> I accidentally used 'argparse' like this in my Python 3.9 program:
>   parser.add_argument ("-c, --clean",  dest="clean", action="store_true")
>   parser.add_argument ("-n, --dryrun", dest="dryrun", action="store_true")
>
> instead of:
>   parser.add_argument ("-c", "--clean",  dest="clean",
> action="store_true")
>   parser.add_argument ("-n", "--dryrun", dest="dryrun",
> action="store_true")
>
> So any use of 'my-prog.py -cn' threw an error:
>   error: unrecognized arguments: -cn
>
> So is there something that throws an error on this wrong use of
> "-c, --clean" instead? Could be useful. Or is "-c, --clean" legit somehow?


Are you trying to troll here? You just showed how you got an error with
this construction, so why are you asking how to get an error with this
construction?

--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
On Sun, 19 Mar 2023 at 06:09, Thomas Passin <list1@tompassin.net> wrote:
>
> On 3/18/2023 2:02 PM, Gisle Vanem via Python-list wrote:
> > I accidentally used 'argparse' like this in my Python 3.9 program:
> > parser.add_argument ("-c, --clean", dest="clean", action="store_true")
> > parser.add_argument ("-n, --dryrun", dest="dryrun", action="store_true")
> >
> > instead of:
> > parser.add_argument ("-c", "--clean", dest="clean",
> > action="store_true")
> > parser.add_argument ("-n", "--dryrun", dest="dryrun",
> > action="store_true")
> >
> > So any use of 'my-prog.py -cn' threw an error:
> > error: unrecognized arguments: -cn
> >
> > So is there something that throws an error on this wrong use of
> > "-c, --clean" instead? Could be useful. Or is "-c, --clean" legit somehow?
>
>
> Are you trying to troll here? You just showed how you got an error with
> this construction, so why are you asking how to get an error with this
> construction?
>

There's a small difference in the way that the arguments are passed.
In the accidental example, "-c, --clean" is passed as a single
argument, which is a bug.

The truth is, though, that no program can ever tell you about all of
your bugs. The "-c, --clean" form is technically valid, and will match
an argument that looks exactly like it; I suppose argparse might be
coded to give a warning if there's a space inside an argument, but
that would be extremely annoying to anyone who actually wants it.

Ultimately, the only way to find bugs is to test, and to get good at
noticing expected patterns (for instance, if your editor highlights
strings or quote characters, your brain should expect to see them in
certain places). It's a nice courtesy when a library will help you to
find your own bugs, but there's a limit to what it can do.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
Thomas Passin wrote:

> Are you trying to troll here?
>
You just showed how you got an error with this construction, so why are you asking how to
> get an error with this construction?

I meant (obviously), another error-message besides:
error: unrecognized arguments: -cn

Perhaps from 'parser.add_argument ("-c, --clean", dest="clean", action="store_true")'
error: "-c, --clean", 2 options are unsupported.

BTW, accusing someone of 'trolling' is rather rude IMHO.
And thanks to ChrisA for a nice and normal answer.

--
--gv

--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
On Sun, 19 Mar 2023 at 06:35, Gisle Vanem via Python-list
<python-list@python.org> wrote:
>
> Thomas Passin wrote:
>
> > Are you trying to troll here?
> >
> You just showed how you got an error with this construction, so why are you asking how to
> > get an error with this construction?
>
> I meant (obviously), another error-message besides:
> error: unrecognized arguments: -cn
>
> Perhaps from 'parser.add_argument ("-c, --clean", dest="clean", action="store_true")'
> error: "-c, --clean", 2 options are unsupported.
>
> BTW, accusing someone of 'trolling' is rather rude IMHO.
> And thanks to ChrisA for a nice and normal answer.

I suspect the reason you were accused of trolling was that it was
quite unobvious what the difference was. When you ask for help with
something where the distinction is subtle (and you generally know it
was subtle by how long it took you to spot it!), it's often valuable
to pinpoint the exact issue, so people don't gloss over it and think
you posted the same code twice. For example:

"""
I accidentally used 'argparse' like this in my Python 3.9 program:
parser.add_argument ("-c, --clean", dest="clean", action="store_true")
parser.add_argument ("-n, --dryrun", dest="dryrun", action="store_true")

instead of:
parser.add_argument ("-c", "--clean", dest="clean", action="store_true")
parser.add_argument ("-n", "--dryrun", dest="dryrun", action="store_true")

Instead of having the short and long forms as separate parameters to
add_argument, I mistakenly put them in a single string.
"""

Now everyone can see immediately what your bug was, how easy it is to
make (and how hard to spot if not pointed out), and how likely it is
for this to be actually desired behaviour (in this case: highly
unlikely).

As to the reason you didn't get any other error message - that's
because, technically, what you did was perfectly valid.

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument ("-c, --clean", dest="clean", action="store_true")
_StoreTrueAction(option_strings=['-c, --clean'], dest='clean',
nargs=0, const=True, default=False, type=None, choices=None,
required=False, help=None, metavar=None)
>>> parser.add_argument ("-n, --dryrun", dest="dryrun", action="store_true")
_StoreTrueAction(option_strings=['-n, --dryrun'], dest='dryrun',
nargs=0, const=True, default=False, type=None, choices=None,
required=False, help=None, metavar=None)
>>> parser.parse_args(["-c, --clean"])
Namespace(clean=True, dryrun=False)

It's just that the argument needed to make this happen is... a tad
unlikely in the real world. So I do see that there's a problem here,
but I'm not sure what the true solution is. Is it reasonable to
mandate that arguments do not contain spaces? (Dubious.) What if
they're not allowed to contain commas? (Also dubious.) Maybe something
really complicated like ", -" as an entire construct? (Could easily be
confusing.) It's definitely worth opening the conversation on this,
and I'd be curious to know how many other people have made this same
bug.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
On 3/18/2023 4:05 PM, Chris Angelico wrote:
> On Sun, 19 Mar 2023 at 06:35, Gisle Vanem via Python-list
> <python-list@python.org> wrote:
>>
>> Thomas Passin wrote:
>>
>>> Are you trying to troll here?
>>>
>> You just showed how you got an error with this construction, so why are you asking how to
>>> get an error with this construction?
>>
>> I meant (obviously), another error-message besides:
>> error: unrecognized arguments: -cn
>>
>> Perhaps from 'parser.add_argument ("-c, --clean", dest="clean", action="store_true")'
>> error: "-c, --clean", 2 options are unsupported.
>>
>> BTW, accusing someone of 'trolling' is rather rude IMHO.
>> And thanks to ChrisA for a nice and normal answer.

I apologize for putting it that way. I did have a reason for wondering
about the post, but another time I wouldn't express it like that.
Really, this is more general - about how to be more effective when
asking for help.

> I suspect the reason you were accused of trolling was that it was
> quite unobvious what the difference was. When you ask for help with
> something where the distinction is subtle (and you generally know it
> was subtle by how long it took you to spot it!), it's often valuable
> to pinpoint the exact issue, so people don't gloss over it and think
> you posted the same code twice.

I did notice the difference, although I had to read it carefully a few
times. What I picked up on was that the OP wrote

"So is there something that throws an error on this wrong use..."

immediately after writing

"So any use of 'my-prog.py -cn' threw an error..."

To me that reads "using 'cn' as a parameter gave an error. Is there
something that will give this error?". The question doesn't make sense
that way - the OP *already* has "something" that gives the error. So
either the OP meant something else - and why should I need to guess what
it was? - or ... [troll, maybe? :)].

So please, try to think out how your questions will seem to the reader,
and be clear about what you are asking. You may not know the
terminology that some other people use, but don't let that stop you from
being clear about what you really need to find out. Including more
context is likely to be helpful, too. Don't leave it to the readers to
infer what you were thinking of.

Perhaps some people who ask for help do not realize it, but to
understand clearly what a situation and question are, and to work out
and write (and edit) a helpful reply, perhaps with some code that needed
to be checked first - that takes time and care.


--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
Thomas Passin wrote:

> So please, try to think out how your questions will seem to the reader, and be clear about what you are asking.  You may
> not know the terminology that some other people use, but don't let that stop you from being clear about what you really
> need to find out.  Including more context is likely to be helpful, too.  Don't leave it to the readers to infer what you
> were thinking of.

I'll do that. My post was written in a bit of a hurry
and frustration after struggling with my 'typo' for
30 minutes.

--
--gv
--
https://mail.python.org/mailman/listinfo/python-list
Re: Q: argparse.add_argument() [ In reply to ]
On 3/18/2023 10:52 PM, Gisle Vanem via Python-list wrote:
> Thomas Passin wrote:
>
>> So please, try to think out how your questions will seem to the
>> reader, and be clear about what you are asking.  You may not know the
>> terminology that some other people use, but don't let that stop you
>> from being clear about what you really need to find out.  Including
>> more context is likely to be helpful, too.  Don't leave it to the
>> readers to infer what you were thinking of.
>
> I'll do that. My post was written in a bit of a hurry
> and frustration after struggling with my 'typo' for
> 30 minutes.


Been there too :)

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