Skip to content

Display the invoked executable name when displaying help #91818

Closed

Description

@scrummyin

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

Activity

  1. hugovk commented on Apr 22, 2022

    @hugovk
    Member

    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

  2. scrummyin commented on Apr 22, 2022

    @scrummyin
    ContributorAuthor

    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.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 27, 2023
  4. serhiy-storchaka commented on Sep 28, 2024

    @serhiy-storchaka
    Member

    The general issue is a duplicate of #66436.

    Leaving this issue open as json.tools specific issue.

  5. added a commit that references this issue on Oct 1, 2024
  6. added a commit that references this issue on Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions