-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-66436: Improved prog default value for argparse.ArgumentParser #124799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1977630
58cbd64
291b6cf
93c7a80
0b72da9
5f62bbd
99c7d98
92b9d07
f6810ee
1f0bfc1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1697,6 +1697,20 @@ def add_mutually_exclusive_group(self, *args, **kwargs): | |
| return super().add_mutually_exclusive_group(*args, **kwargs) | ||
|
|
||
|
|
||
| def _prog_name(prog=None): | ||
| if prog is not None: | ||
| return prog | ||
| arg0 = _sys.argv[0] | ||
| modspec = _sys.modules['__main__'].__spec__ | ||
| if modspec is None: | ||
| return _os.path.basename(arg0) | ||
| py = _os.path.basename(_sys.executable) | ||
| if (modspec.name == '__main__' and not modspec.parent and modspec.has_location | ||
| and _os.path.dirname(modspec.origin) == _os.path.join(_os.getcwd(), arg0)): | ||
|
||
| return f'{py} {arg0}' | ||
| return f'{py} -m {modspec.name.removesuffix(".__main__")}' | ||
|
|
||
|
|
||
| class ArgumentParser(_AttributeHolder, _ActionsContainer): | ||
| """Object for parsing command line strings into Python objects. | ||
|
|
||
|
|
@@ -1740,11 +1754,7 @@ def __init__(self, | |
| argument_default=argument_default, | ||
| conflict_handler=conflict_handler) | ||
|
|
||
| # default setting for prog | ||
| if prog is None: | ||
| prog = _os.path.basename(_sys.argv[0]) | ||
|
|
||
| self.prog = prog | ||
| self.prog = _prog_name(prog) | ||
| self.usage = usage | ||
| self.epilog = epilog | ||
| self.formatter_class = formatter_class | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Improved :ref:`prog` default value for :class:`argparse.ArgumentParser`. It | ||
| can now include the name of the Python executable along with the module or | ||
serhiy-storchaka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| package name, or the path to a directory, ZIP file, or directory within a | ||
| ZIP file if the code was run that way. | ||
Uh oh!
There was an error while loading. Please reload this page.