gh-148766: Add colour to Python help#148767
Conversation
| #if defined(MS_WINDOWS) && defined(HAVE_WINDOWS_CONSOLE_IO) | ||
| { | ||
| DWORD mode = 0; | ||
| HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); |
There was a problem hiding this comment.
Shouldn't this also consider stderr (or maybe it does? I don't know Windows quirks).
| PyMem_RawFree(buf); | ||
| } | ||
| else { | ||
| fprintf(f, usage_line, program); |
There was a problem hiding this comment.
If PyMem_RawMalloc fails we will get the raw tags, no?
There was a problem hiding this comment.
This now falls back to no tags.
| return; | ||
| } | ||
| } | ||
| fprint_tagged(stdout, usage_envvars, 0); |
There was a problem hiding this comment.
We still need formatting here?
There was a problem hiding this comment.
Yes, updated to use colour if available.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
What if the program path by accident contains {E}?
|
Updated to flip it: colour codes are now included in the help text (via macros to keep it tidier), and stripped out when not needed. So |
|
Thanks for the reviews, any further comments? |
|
Would something like this be more maintainable? #define RESET "\x1b[0m"
#define ENVVAR(STR) "\x1b[1;36m" STR RESET |
|
Nice idea, updated :) |
Documentation build overview
5 files changed ·
|
|
This version is barely readable. What if return to the previous version with substitutions and use some of substitution codes to substitute the program name, the default PYTHONHOME, the path separator instead of just color sequences? |
| SHORTOPT("-d") " : turn on parser debugging output (for experts only, only works on\n" | ||
| " debug builds); also " ENVVAR_REF("PYTHONDEBUG") SUMMARY_LABEL("=x") "\n" | ||
| SHORTOPT("-E") " : ignore " ENVVAR_REF("PYTHON*") " environment variables (such as " ENVVAR_REF("PYTHONPATH") ")\n" | ||
| SHORTOPT("-h") " : print this help message and exit (also " SHORTOPT_SUMMARY("-?") " or " ENVVAR_REF("--help") ")\n" |
There was a problem hiding this comment.
| SHORTOPT("-h") " : print this help message and exit (also " SHORTOPT_SUMMARY("-?") " or " ENVVAR_REF("--help") ")\n" | |
| SHORTOPT("-h") " : print this help message and exit (also " SHORTOPT_SUMMARY("-?") " or " LONGOPT("--help") ")\n" |
| " this option has no effect on stdin; also " ENVVAR_REF("PYTHONUNBUFFERED") SUMMARY_LABEL("=x") "\n" | ||
| SHORTOPT("-v") " : verbose (trace import statements); also " ENVVAR_REF("PYTHONVERBOSE") SUMMARY_LABEL("=x") "\n" | ||
| " can be supplied multiple times to increase verbosity\n" | ||
| SHORTOPT("-V") " : print the Python version number and exit (also " ENVVAR_REF("--version") ")\n" |
There was a problem hiding this comment.
| SHORTOPT("-V") " : print the Python version number and exit (also " ENVVAR_REF("--version") ")\n" | |
| SHORTOPT("-V") " : print the Python version number and exit (also " LONGOPT("--version") ")\n" |
| "PYTHON_FROZEN_MODULES: whether to use frozen modules; the default is \"on\"\n" | ||
| " for installed Python and \"off\" for a local build\n" | ||
| " (-X frozen_modules)\n" | ||
| HEADING("These variables have equivalent command-line options (see ") ENVVAR_REF("--help") " for details):" "\n" |
There was a problem hiding this comment.
| HEADING("These variables have equivalent command-line options (see ") ENVVAR_REF("--help") " for details):" "\n" | |
| HEADING("These variables have equivalent command-line options") "(see " ENVVAR_REF("--help") " for details):" "\n" |
| "PYTHONPERFSUPPORT: support the Linux \"perf\" profiler (-X perf)\n" | ||
| ENVVAR("PYTHONINSPECT") " : inspect interactively after running script (" SHORTOPT_SUMMARY("-i") ")\n" | ||
| ENVVAR("PYTHONINTMAXSTRDIGITS") ": limit the size of int<->str conversions;\n" | ||
| " 0 disables the limit (" SHORTOPT_SUMMARY("-X") " " ENVVAR_REF("int_max_str_digits") SUMMARY_LABEL("=N") ")\n" |
There was a problem hiding this comment.
| " 0 disables the limit (" SHORTOPT_SUMMARY("-X") " " ENVVAR_REF("int_max_str_digits") SUMMARY_LABEL("=N") ")\n" | |
| " " SUMMARY_LABEL("0") " disables the limit (" SHORTOPT_SUMMARY("-X") " " ENVVAR_REF("int_max_str_digits") SUMMARY_LABEL("=N") ")\n" |
| ENVVAR("PYTHONEXECUTABLE") ": set sys.argv[0] to this value (macOS only)\n" | ||
| #endif | ||
| "PYTHONHASHSEED : if this variable is set to 'random', a random value is used\n" | ||
| ENVVAR("PYTHONHASHSEED") " : if this variable is set to 'random', a random value is used\n" |
There was a problem hiding this comment.
| ENVVAR("PYTHONHASHSEED") " : if this variable is set to 'random', a random value is used\n" | |
| ENVVAR("PYTHONHASHSEED") " : if this variable is set to '" SUMMARY_LABEL("random") "', a random value is used\n" |
| "PYTHONMALLOCSTATS: print memory allocator statistics\n" | ||
| "PYTHONPATH : '%lc'-separated list of directories prefixed to the\n" | ||
| ENVVAR("PYTHONMALLOCSTATS") ": print memory allocator statistics\n" | ||
| ENVVAR("PYTHONPATH") " : '%lc'-separated list of directories prefixed to the\n" |
There was a problem hiding this comment.
| ENVVAR("PYTHONPATH") " : '%lc'-separated list of directories prefixed to the\n" | |
| ENVVAR("PYTHONPATH") " : '" SUMMARY_LABEL(%lc) "'-separated list of directories prefixed to the\n" |
|
My previous comment was related to the version prior to the latest change, but I still thing that substitution-based approach would be clearer than macros-based. |
|
@serhiy-storchaka Could you give a short example of what you're thinking of? |
|
Simple example: "{E}PYTHONHOME{r} : alternate <prefix> directory (or <prefix>{b}{D}{r}<exec_prefix>).\n"
" The default module search path uses {b}{H}{r}.\n"
To avoid repeated "#E{PYTHONHOME} : alternate <prefix> directory (or <prefix>#b{#D}<exec_prefix>).\n"
" The default module search path uses #b{#H}.\n"This is simpler than it looks. |
|
Thanks, updated! How does this look? |
|
Thanks all for the reviews! |

python --help(and especially--help-all) is a monochrome wall of text. Similar to argparse help, let's add some colour to add structure and improve readability.Here's real screenshots of
--helpand termshot capture of--help-all:📚 Documentation preview 📚: https://cpython-previews--148767.org.readthedocs.build/