@@ -74,7 +74,7 @@ ArgumentParser objects
74
74
prefix_chars='-', fromfile_prefix_chars=None, \
75
75
argument_default=None, conflict_handler='error', \
76
76
add_help=True, allow_abbrev=True, exit_on_error=True, \
77
- suggest_on_error=False)
77
+ suggest_on_error=False, color=False )
78
78
79
79
Create a new :class: `ArgumentParser ` object. All parameters should be passed
80
80
as keyword arguments. Each parameter has its own more detailed description
@@ -111,14 +111,15 @@ ArgumentParser objects
111
111
* add_help _ - Add a ``-h/--help `` option to the parser (default: ``True ``)
112
112
113
113
* allow_abbrev _ - Allows long options to be abbreviated if the
114
- abbreviation is unambiguous. (default: ``True ``)
114
+ abbreviation is unambiguous (default: ``True ``)
115
115
116
116
* exit_on_error _ - Determines whether or not :class: `!ArgumentParser ` exits with
117
117
error info when an error occurs. (default: ``True ``)
118
118
119
119
* suggest_on_error _ - Enables suggestions for mistyped argument choices
120
120
and subparser names (default: ``False ``)
121
121
122
+ * color _ - Allow color output (default: ``False ``)
122
123
123
124
.. versionchanged :: 3.5
124
125
*allow_abbrev * parameter was added.
@@ -130,6 +131,9 @@ ArgumentParser objects
130
131
.. versionchanged :: 3.9
131
132
*exit_on_error * parameter was added.
132
133
134
+ .. versionchanged :: 3.14
135
+ *suggest_on_error * and *color * parameters were added.
136
+
133
137
The following sections describe how each of these are used.
134
138
135
139
@@ -594,7 +598,8 @@ subparser names, the feature can be enabled by setting ``suggest_on_error`` to
594
598
``True ``. Note that this only applies for arguments when the choices specified
595
599
are strings::
596
600
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)
598
603
>>> parser.add_argument('--action', choices=['sum', 'max'])
599
604
>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
600
605
... help='an integer for the accumulator')
@@ -612,6 +617,33 @@ keyword argument::
612
617
.. versionadded :: 3.14
613
618
614
619
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
+
615
647
The add_argument() method
616
648
-------------------------
617
649
0 commit comments