@@ -30,7 +30,7 @@ def test_store_split_init(self):
30
30
def test_store_split (self ):
31
31
"""Check that argparse calls store_split correctly"""
32
32
namespace = argparse .Namespace ()
33
- namespace .passes = {}
33
+ namespace ._passes = {}
34
34
35
35
parser = argparse .ArgumentParser ()
36
36
parser .add_argument ("--foo" , action = _StoreSplitAction , sep = "," )
@@ -58,7 +58,7 @@ def test_store_split(self):
58
58
args , _ = parser .parse_known_args (["--baz=1" ], namespace )
59
59
60
60
args , _ = parser .parse_known_args (["--qux=one,two" ], namespace )
61
- self .assertEqual (args .passes , {"--qux" : ["one" , "two" ]})
61
+ self .assertEqual (args ._passes , {"--qux" : ["one" , "two" ]})
62
62
63
63
def test_extend_match_init (self ):
64
64
"""Check that extend_match recognizes custom arguments"""
@@ -78,7 +78,6 @@ def test_extend_match_init(self):
78
78
def test_extend_match (self ):
79
79
"""Check that argparse calls store_split correctly"""
80
80
namespace = argparse .Namespace ()
81
- namespace .passes = {}
82
81
83
82
parser = argparse .ArgumentParser ()
84
83
parser .add_argument (
@@ -100,6 +99,23 @@ def test_extend_match(self):
100
99
action = _ExtendMatchAction ,
101
100
pattern = r"option_(\d+)" ,
102
101
dest = "passes" ,
102
+ default = ["0" ],
103
+ override = True ,
104
+ )
105
+ parser .add_argument (
106
+ "--one" ,
107
+ "--two" ,
108
+ action = _ExtendMatchAction ,
109
+ pattern = r"option_(\d+)" ,
110
+ dest = "passes" ,
111
+ )
112
+ parser .add_argument (
113
+ "--default-override" ,
114
+ action = _ExtendMatchAction ,
115
+ pattern = r"option_(\d+)" ,
116
+ default = ["0" ],
117
+ dest = "override" ,
118
+ override = True ,
103
119
)
104
120
105
121
args , _ = parser .parse_known_args (
@@ -117,11 +133,34 @@ def test_extend_match(self):
117
133
with self .assertRaises (TypeError ):
118
134
args , _ = parser .parse_known_args (["--baz=1" ], namespace )
119
135
136
+ # Check that the default values defined by flags always exists.
137
+ # Note that the caller must initialize the default.
138
+ namespace .override = ["0" ]
139
+ namespace ._passes = {"--qux" : ["0" ]}
140
+ args , _ = parser .parse_known_args (
141
+ [],
142
+ namespace ,
143
+ )
144
+ self .assertEqual (args .override , ["0" ])
145
+ self .assertEqual (args ._passes , {"--qux" : ["0" ]})
146
+
147
+ # Check that the default pass is overridden by use of --qux.
148
+ # Note that the caller must initialize the default.
149
+ namespace .override = ["0" ]
150
+ namespace ._passes = {"--qux" : ["0" ]}
151
+ args , _ = parser .parse_known_args (
152
+ ["--qux=option_1,option_2" , "--default-override=option_1" ],
153
+ namespace ,
154
+ )
155
+ self .assertEqual (args .override , ["1" ])
156
+ self .assertEqual (args ._passes , {"--qux" : ["1" , "2" ]})
157
+
158
+ namespace ._passes = {}
120
159
args , _ = parser .parse_known_args (
121
- ["--qux =option_1, option_2" ],
160
+ ["--one =option_1" , "--two= option_2" ],
122
161
namespace ,
123
162
)
124
- self .assertEqual (args .passes , {"--qux " : ["1" , "2" ]})
163
+ self .assertEqual (args ._passes , {"--one " : ["1" , "2" ]})
125
164
126
165
127
166
if __name__ == "__main__" :
0 commit comments