ANSI coloring and styling
Using colors in the command line tool’s output does not just look good: contrasting important elements like argument names from the rest of the text reduces the cognitive load on the user. argparse uses ANSI escape sequences to add coloring and styling to the error messages and help text. In addition, argparse offers public API to apply colors and styles to any text printed to the console (see below).

Styles and colors
The following styles and colors are available in argparse.ansi submodule:
Font styles:
bolditalicunderline
Colors:
Foreground | Background |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
There is also a “virtual” style noStyle that means no styling is applied. It’s useful in ternary operations as a fallback for the case when styling is disabled. See below example for details.
All styles above can be combined using . and even be used in regular output:
The following example shows how styling can be used in custom help text (Usage, Description, ShortDescription, Epilog API):
Here is how help screen will look like:

Enable/disable the styling
By default argparse will try to detect whether ANSI styling is supported, and if so, it will apply styling to the help text and error messages. Note that detection works for stdout and stderr separately so, for example, if stdout is redirected to a file (so stdout styling is disabled) then stderr output (eg. error messages) will still have styling applied.
There is Config.stylingMode parameter that can be used to override default behavior:
If it’s set to
Config.StylingMode.on, then styling is always enabled.If it’s set to
Config.StylingMode.off, then styling is always disabled.If it’s set to
Config.StylingMode.autodetect, then heuristics are used to determine whether styling will be applied.
In some cases styling control should be exposed to a user as a command line argument (similar to --color argument in ls or grep command). argparse supports this use case – just add an argument to a command (it can be customized with @NamedArgument UDA):
This will add the following argument:

Heuristics for enabling styling
Below is the exact sequence of steps argparse uses to determine whether or not to emit ANSI escape codes (see detectSupport() function here for details):
If environment variable
NO_COLOR != "", then styling is disabled. See here for details.If environment variable
CLICOLOR_FORCE != "0", then styling is enabled. See here for details.If environment variable
CLICOLOR == "0", then styling is disabled. See here for details.If environment variable
ConEmuANSI == "OFF", then styling is disabled. See here for details.If environment variable
ConEmuANSI == "ON", then styling is enabled. See here for details.If environment variable
ANSICONis defined (regardless of its value), then styling is enabled. See here for details.Windows only (
version(Windows)):If environment variable
TERMcontains"cygwin"or starts with"xterm", then styling is enabled.If
GetConsoleModecall forSTD_OUTPUT_HANDLE/STD_ERROR_HANDLEreturns a mode that hasENABLE_VIRTUAL_TERMINAL_PROCESSINGset, then styling is enabled.If
SetConsoleModecall forSTD_OUTPUT_HANDLE/STD_ERROR_HANDLEwithENABLE_VIRTUAL_TERMINAL_PROCESSINGmode was successful, then styling is enabled.
Posix only (
version(Posix)):If
STDOUT/STDERRis not redirected (isattyreturns 1), then styling is enabled.
If none of the above applies, then styling is disabled.