GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-109164: Replace `getopt` with `argparse` in pdb by gaogaotiantian · Pull Request #109165 · python/cpython Conversation
|
Thank you, it does make more sense in this way. I applied your changes with some modifications:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
A
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <[email protected]>
|
|
||
| parser.add_argument('-c', '--command', action='append', default=[], metavar='command') | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument('-m', metavar='module') |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I suggest using names longer than 1 letter, you should update the code below as well.
| group.add_argument('-m', metavar='module') | |
| group.add_argument('-m', metavar='module', dest='module') |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| allow_abbrev=False) | ||
|
|
||
| parser.add_argument('-c', '--command', action='append', default=[], metavar='command') |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's surprising that -c looks like python -c option, but different. Would you mind to add a help message to explain that there are pdb commands? And that they have the same format than .pdbrc configuration files?
I suggest to add dest='commands', since it's non-obvious from command name that's a list.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| parser.add_argument('-c', '--command', action='append', default=[], metavar='command') | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument('-m', metavar='module') | ||
| group.add_argument('pyfile', nargs='?') |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
IMO nargs='?' is wrong. You cannot pass no pyfile.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is a slight work-around for limitations in argparse -- we want the mutually exclusive group behaviour, so nargs='?' is needed.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Which limitation? Is there is a reason to use the wrong nargs on purpose, please add a comment to explain why.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Which limitation? Is there is a reason to use the wrong nargs on purpose, please add a comment to explain why.
I don't think this is wrong. Consider it as - you can either pass a module with -m OR a pyfile. So pyfile is optional, you can pass no pyfile.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
That's not how exclusive group works. Either you pass a module or a pyfile. pyfile is not optional.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
That's not how exclusive group works. Either you pass a module or a pyfile. pyfile is not optional.
I think either pass a module or a pyfile -> pyfile is optional, it's literally in a either/or sentence.
Mutually exclusive group requires all the options in the group to be "optional". What's your proposal for this? You can't use nargs=1 because that would make pyfile a required argument - and it's not. It's absolutely fine if you don't pass a pyfile when you pass a module (actually you can't pass a pyfile when you pass a module).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misunderstood how exclusive groups work. It's kind of surprising. Apparently, you must use nargs='?'. It's just counter intuitive to me.
import argparse parser = argparse.ArgumentParser(prog='PROG') group = parser.add_argument_group() exclusive_group = group.add_mutually_exclusive_group(required=True) exclusive_group.add_argument('-m', metavar='MODULE') exclusive_group.add_argument('pyscript', nargs='?') print(parser.parse_args(['-m', 'mymod'])) print(parser.parse_args(['myscript'])) print(parser.parse_args([]))
Output:
$ python3 x.py
Namespace(m='mymod', pyscript=None)
Namespace(m=None, pyscript='myscript')
usage: PROG [-h] (-m MODULE | pyscript)
PROG: error: one of the arguments -m pyscript is required
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It's just counter intuitive to me.
This bit of argparse's design is odd, I agree.
A
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| else: | ||
| file = opts.pyfile | ||
| target = _ScriptTarget(file) | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would prefer:
if opts.module:
args = [opts.module]
...
else:
args = [opts.pyfile]
...
args.extend(opts.ags)
...
sys.argv = args # Hide "pdb.py" and pdb options from argument list
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This would be a larger refactor, the current state reflects what's currently present.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Right, that's why I'm asking for :-)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The logic is pretty similar right? Just a slightly different implementation. I don't think this is too much if this is preferred.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm just disturbed by calling "module" a "file".
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Also, feel free to ignore my comment about this code.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Co-authored-by: Victor Stinner <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ah, sorry I did not have the chance to address some of the comments which I believe are valid. (comment -> comments for example). We can leave it here for now and there will be the next refactor. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.