Skip to content
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

Display the invoked executable name when displaying help #91818

Closed
scrummyin opened this issue Apr 22, 2022 · 3 comments
Closed

Display the invoked executable name when displaying help #91818

scrummyin opened this issue Apr 22, 2022 · 3 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@scrummyin
Copy link
Contributor

scrummyin commented Apr 22, 2022

Feature or enhancement

Display the invoked executable name such as python3 or python.exe so users can copy and paste without having to translate, for tools such as ast and json.tool.

Pitch

The example below shows a before usage of json.tool and an after below.

$ python3 -m json.tool -h
usage: python -m json.tool [-h] [--sort-keys] [--json-lines] [infile] [outfile]

becomes

$ python3 -m json.tool -h
usage: python3 -m json.tool [-h] [--sort-keys] [--json-lines] [infile] [outfile]

This is more friendly to users on windows because of the .exe extension and to *nix users who's OS might install python 3 at python3 and have an old python2 version somewhere else. Or as we move into the future and some OS drop python2 but never rectify python point to python3.

Linked PRs

@scrummyin scrummyin added the type-feature A feature request or enhancement label Apr 22, 2022
@hugovk
Copy link
Member

hugovk commented Apr 22, 2022

This could be done something like this:

diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 0490b8c0be..e0e196c722 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -16,8 +16,11 @@
 from pathlib import Path
 
 
+_python_exe = sys.executable
+
+
 def main():
-    prog = 'python -m json.tool'
+    prog = f'{_python_exe} -m json.tool'
     description = ('A simple command line interface for json module '
                    'to validate and pretty-print JSON objects.')
     parser = argparse.ArgumentParser(prog=prog, description=description)
$ ./python.exe -m json.tool -h
usage: /Users/hugo/github/cpython/python.exe -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact]
                                                              [infile] [outfile]
...

Or (less helpfully?) without the full path:

diff --git a/Lib/json/tool.py b/Lib/json/tool.py
index 0490b8c0be..43754cdd59 100644
--- a/Lib/json/tool.py
+++ b/Lib/json/tool.py
@@ -12,12 +12,16 @@
 """
 import argparse
 import json
+import os
 import sys
 from pathlib import Path
 
 
+_python_exe = os.path.basename(sys.executable)
+
+
 def main():
-    prog = 'python -m json.tool'
+    prog = f'{_python_exe} -m json.tool'
     description = ('A simple command line interface for json module '
                    'to validate and pretty-print JSON objects.')
     parser = argparse.ArgumentParser(prog=prog, description=description)
$ ./python.exe -m json.tool -h
usage: python.exe -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact] [infile] [outfile]
...

pip does something along these lines:

https://github.com/pypa/pip/blob/c247ddce123513198858d4589c5ee198dcc6ba40/src/pip/_internal/cli/main_parser.py#L21

https://github.com/pypa/pip/blob/c247ddce123513198858d4589c5ee198dcc6ba40/src/pip/_internal/utils/misc.py#L109-L118

@scrummyin
Copy link
Contributor Author

I did a little more than that with #91819 , partially to guarantee that something comes out when sys.executable might fail, since it can return None.

@erlend-aasland erlend-aasland moved this to Features in Argparse issues Feb 21, 2023
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 27, 2023
@serhiy-storchaka
Copy link
Member

The general issue is a duplicate of #66436.

Leaving this issue open as json.tools specific issue.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 1, 2024
As argparse now detects by default when the code was run as a module.

This leads to using the actual executable name instead of simply "python"
to display in the usage message ("usage: python -m ...").
serhiy-storchaka added a commit that referenced this issue Oct 9, 2024
)

As argparse now detects by default when the code was run as a module.

This leads to using the actual executable name instead of simply "python"
to display in the usage message ("usage: python -m ...").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

4 participants