89
89
import re as _re
90
90
import sys as _sys
91
91
92
+ import warnings
93
+
92
94
from gettext import gettext as _ , ngettext
93
95
94
96
SUPPRESS = '==SUPPRESS=='
@@ -151,6 +153,7 @@ def _copy_items(items):
151
153
# Formatting Help
152
154
# ===============
153
155
156
+
154
157
class HelpFormatter (object ):
155
158
"""Formatter for generating usage messages and argument help strings.
156
159
@@ -693,15 +696,27 @@ class ArgumentDefaultsHelpFormatter(HelpFormatter):
693
696
"""
694
697
695
698
def _get_help_string (self , action ):
699
+ """
700
+ Add the default value to the option help message.
701
+
702
+ ArgumentDefaultsHelpFormatter and BooleanOptionalAction when it isn't
703
+ already present. This code will do that, detecting cornercases to
704
+ prevent duplicates or cases where it wouldn't make sense to the end
705
+ user.
706
+ """
696
707
help = action .help
697
- if '%(default)' not in action .help :
708
+ if help is None :
709
+ help = ''
710
+
711
+ if '%(default)' not in help :
698
712
if action .default is not SUPPRESS :
699
713
defaulting_nargs = [OPTIONAL , ZERO_OR_MORE ]
700
714
if action .option_strings or action .nargs in defaulting_nargs :
701
715
help += ' (default: %(default)s)'
702
716
return help
703
717
704
718
719
+
705
720
class MetavarTypeHelpFormatter (HelpFormatter ):
706
721
"""Help message formatter which uses the argument 'type' as the default
707
722
metavar value (instead of the argument 'dest')
@@ -717,7 +732,6 @@ def _get_default_metavar_for_positional(self, action):
717
732
return action .type .__name__
718
733
719
734
720
-
721
735
# =====================
722
736
# Options and Arguments
723
737
# =====================
@@ -752,7 +766,7 @@ def __str__(self):
752
766
if self .argument_name is None :
753
767
format = '%(message)s'
754
768
else :
755
- format = 'argument %(argument_name)s: %(message)s'
769
+ format = _ ( 'argument %(argument_name)s: %(message)s' )
756
770
return format % dict (message = self .message ,
757
771
argument_name = self .argument_name )
758
772
@@ -860,6 +874,7 @@ def format_usage(self):
860
874
def __call__ (self , parser , namespace , values , option_string = None ):
861
875
raise NotImplementedError (_ ('.__call__() not defined' ))
862
876
877
+
863
878
class BooleanOptionalAction (Action ):
864
879
def __init__ (self ,
865
880
option_strings ,
@@ -879,9 +894,6 @@ def __init__(self,
879
894
option_string = '--no-' + option_string [2 :]
880
895
_option_strings .append (option_string )
881
896
882
- if help is not None and default is not None and default is not SUPPRESS :
883
- help += " (default: %(default)s)"
884
-
885
897
super ().__init__ (
886
898
option_strings = _option_strings ,
887
899
dest = dest ,
@@ -893,6 +905,7 @@ def __init__(self,
893
905
help = help ,
894
906
metavar = metavar )
895
907
908
+
896
909
def __call__ (self , parser , namespace , values , option_string = None ):
897
910
if option_string in self .option_strings :
898
911
setattr (namespace , self .dest , not option_string .startswith ('--no-' ))
@@ -941,7 +954,7 @@ class _StoreConstAction(Action):
941
954
def __init__ (self ,
942
955
option_strings ,
943
956
dest ,
944
- const ,
957
+ const = None ,
945
958
default = None ,
946
959
required = False ,
947
960
help = None ,
@@ -1036,7 +1049,7 @@ class _AppendConstAction(Action):
1036
1049
def __init__ (self ,
1037
1050
option_strings ,
1038
1051
dest ,
1039
- const ,
1052
+ const = None ,
1040
1053
default = None ,
1041
1054
required = False ,
1042
1055
help = None ,
@@ -1168,6 +1181,13 @@ def add_parser(self, name, **kwargs):
1168
1181
1169
1182
aliases = kwargs .pop ('aliases' , ())
1170
1183
1184
+ if name in self ._name_parser_map :
1185
+ raise ArgumentError (self , _ ('conflicting subparser: %s' ) % name )
1186
+ for alias in aliases :
1187
+ if alias in self ._name_parser_map :
1188
+ raise ArgumentError (
1189
+ self , _ ('conflicting subparser alias: %s' ) % alias )
1190
+
1171
1191
# create a pseudo-action to hold the choice help
1172
1192
if 'help' in kwargs :
1173
1193
help = kwargs .pop ('help' )
@@ -1648,6 +1668,14 @@ def _remove_action(self, action):
1648
1668
super (_ArgumentGroup , self )._remove_action (action )
1649
1669
self ._group_actions .remove (action )
1650
1670
1671
+ def add_argument_group (self , * args , ** kwargs ):
1672
+ warnings .warn (
1673
+ "Nesting argument groups is deprecated." ,
1674
+ category = DeprecationWarning ,
1675
+ stacklevel = 2
1676
+ )
1677
+ return super ().add_argument_group (* args , ** kwargs )
1678
+
1651
1679
1652
1680
class _MutuallyExclusiveGroup (_ArgumentGroup ):
1653
1681
@@ -1668,6 +1696,14 @@ def _remove_action(self, action):
1668
1696
self ._container ._remove_action (action )
1669
1697
self ._group_actions .remove (action )
1670
1698
1699
+ def add_mutually_exclusive_group (self , * args , ** kwargs ):
1700
+ warnings .warn (
1701
+ "Nesting mutually exclusive groups is deprecated." ,
1702
+ category = DeprecationWarning ,
1703
+ stacklevel = 2
1704
+ )
1705
+ return super ().add_mutually_exclusive_group (* args , ** kwargs )
1706
+
1671
1707
1672
1708
class ArgumentParser (_AttributeHolder , _ActionsContainer ):
1673
1709
"""Object for parsing command line strings into Python objects.
@@ -1857,8 +1893,7 @@ def parse_known_args(self, args=None, namespace=None):
1857
1893
if self .exit_on_error :
1858
1894
try :
1859
1895
namespace , args = self ._parse_known_args (args , namespace )
1860
- except ArgumentError :
1861
- err = _sys .exc_info ()[1 ]
1896
+ except ArgumentError as err :
1862
1897
self .error (str (err ))
1863
1898
else :
1864
1899
namespace , args = self ._parse_known_args (args , namespace )
@@ -1962,7 +1997,11 @@ def consume_optional(start_index):
1962
1997
# arguments, try to parse more single-dash options out
1963
1998
# of the tail of the option string
1964
1999
chars = self .prefix_chars
1965
- if arg_count == 0 and option_string [1 ] not in chars :
2000
+ if (
2001
+ arg_count == 0
2002
+ and option_string [1 ] not in chars
2003
+ and explicit_arg != ''
2004
+ ):
1966
2005
action_tuples .append ((action , [], option_string ))
1967
2006
char = option_string [0 ]
1968
2007
option_string = char + explicit_arg [0 ]
@@ -2133,8 +2172,7 @@ def _read_args_from_files(self, arg_strings):
2133
2172
arg_strings .append (arg )
2134
2173
arg_strings = self ._read_args_from_files (arg_strings )
2135
2174
new_arg_strings .extend (arg_strings )
2136
- except OSError :
2137
- err = _sys .exc_info ()[1 ]
2175
+ except OSError as err :
2138
2176
self .error (str (err ))
2139
2177
2140
2178
# return the modified argument list
@@ -2484,9 +2522,9 @@ def _get_value(self, action, arg_string):
2484
2522
result = type_func (arg_string )
2485
2523
2486
2524
# ArgumentTypeErrors indicate errors
2487
- except ArgumentTypeError :
2525
+ except ArgumentTypeError as err :
2488
2526
name = getattr (action .type , '__name__' , repr (action .type ))
2489
- msg = str (_sys . exc_info ()[ 1 ] )
2527
+ msg = str (err )
2490
2528
raise ArgumentError (action , msg )
2491
2529
2492
2530
# TypeErrors or ValueErrors also indicate errors
0 commit comments