@@ -74,7 +74,7 @@ ArgumentParser objects
7474 prefix_chars='-', fromfile_prefix_chars=None, \
7575 argument_default=None, conflict_handler='error', \
7676 add_help=True, allow_abbrev=True, exit_on_error=True, \
77- suggest_on_error=False)
77+ suggest_on_error=False, color=False )
7878
7979 Create a new :class: `ArgumentParser ` object. All parameters should be passed
8080 as keyword arguments. Each parameter has its own more detailed description
@@ -111,14 +111,15 @@ ArgumentParser objects
111111 * add_help _ - Add a ``-h/--help `` option to the parser (default: ``True ``)
112112
113113 * allow_abbrev _ - Allows long options to be abbreviated if the
114- abbreviation is unambiguous. (default: ``True ``)
114+ abbreviation is unambiguous (default: ``True ``)
115115
116116 * exit_on_error _ - Determines whether or not :class: `!ArgumentParser ` exits with
117117 error info when an error occurs. (default: ``True ``)
118118
119119 * suggest_on_error _ - Enables suggestions for mistyped argument choices
120120 and subparser names (default: ``False ``)
121121
122+ * color _ - Allow color output (default: ``False ``)
122123
123124 .. versionchanged :: 3.5
124125 *allow_abbrev * parameter was added.
@@ -130,6 +131,9 @@ ArgumentParser objects
130131 .. versionchanged :: 3.9
131132 *exit_on_error * parameter was added.
132133
134+ .. versionchanged :: 3.14
135+ *suggest_on_error * and *color * parameters were added.
136+
133137The following sections describe how each of these are used.
134138
135139
@@ -594,7 +598,8 @@ subparser names, the feature can be enabled by setting ``suggest_on_error`` to
594598``True ``. Note that this only applies for arguments when the choices specified
595599are strings::
596600
597- >>> parser = argparse.ArgumentParser(description='Process some integers.', suggest_on_error=True)
601+ >>> parser = argparse.ArgumentParser(description='Process some integers.',
602+ suggest_on_error=True)
598603 >>> parser.add_argument('--action', choices=['sum', 'max'])
599604 >>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
600605 ... help='an integer for the accumulator')
@@ -612,6 +617,33 @@ keyword argument::
612617.. versionadded :: 3.14
613618
614619
620+ color
621+ ^^^^^
622+
623+ By default, the help message is printed in plain text. If you want to allow
624+ color in help messages, you can enable it by setting ``color `` to ``True ``::
625+
626+ >>> parser = argparse.ArgumentParser(description='Process some integers.',
627+ ... color=True)
628+ >>> parser.add_argument('--action', choices=['sum', 'max'])
629+ >>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
630+ ... help='an integer for the accumulator')
631+ >>> parser.parse_args(['--help'])
632+
633+ Even if a CLI author has enabled color, it can be
634+ :ref: `controlled using environment variables <using-on-controlling-color >`.
635+
636+ If you're writing code that needs to be compatible with older Python versions
637+ and want to opportunistically use ``color `` when it's available, you
638+ can set it as an attribute after initializing the parser instead of using the
639+ keyword argument::
640+
641+ >>> parser = argparse.ArgumentParser(description='Process some integers.')
642+ >>> parser.color = True
643+
644+ .. versionadded :: next
645+
646+
615647The add_argument() method
616648-------------------------
617649
0 commit comments