python / cpython

The Python programming language
https://www.python.org
Other
63.63k stars 30.48k forks source link

Display the invoked executable name when displaying help #91818

Closed scrummyin closed 1 month ago

scrummyin commented 2 years ago

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

hugovk commented 2 years ago

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 commented 2 years ago

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.

serhiy-storchaka commented 1 month ago

The general issue is a duplicate of #66436.

Leaving this issue open as json.tools specific issue.