@@ -182,6 +182,8 @@ class ExtOption(CompleterOption):
182
182
- set default to None if no option passed,
183
183
- set to default if option without value passed,
184
184
- set to value if option with value passed
185
+ - store_or_False: Same as store_or_None, but set to False instead of None
186
+ - Additionally supports --disable-
185
187
186
188
Types:
187
189
- strlist, strtuple : convert comma-separated string in a list resp. tuple of strings
@@ -193,14 +195,15 @@ class ExtOption(CompleterOption):
193
195
194
196
ENABLE = 'enable' # do nothing
195
197
DISABLE = 'disable' # inverse action
198
+ STORE_OR_FALSE = 'store_or_False'
196
199
197
200
EXTOPTION_EXTRA_OPTIONS = ('date' , 'datetime' , 'regex' , 'add' , 'add_first' , 'add_flex' ,)
198
- EXTOPTION_STORE_OR = ('store_or_None' , 'help' ) # callback type
201
+ EXTOPTION_STORE_OR = ('store_or_None' , STORE_OR_FALSE , 'help' ) # callback type
199
202
EXTOPTION_LOG = ('store_debuglog' , 'store_infolog' , 'store_warninglog' ,)
200
203
EXTOPTION_HELP = ('shorthelp' , 'confighelp' , 'help' )
201
204
202
205
ACTIONS = Option .ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_STORE_OR + EXTOPTION_LOG + EXTOPTION_HELP
203
- STORE_ACTIONS = Option .STORE_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_LOG + ('store_or_None' ,)
206
+ STORE_ACTIONS = Option .STORE_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_LOG + ('store_or_None' , STORE_OR_FALSE )
204
207
TYPED_ACTIONS = Option .TYPED_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_STORE_OR
205
208
ALWAYS_TYPED_ACTIONS = Option .ALWAYS_TYPED_ACTIONS + EXTOPTION_EXTRA_OPTIONS
206
209
@@ -229,7 +232,9 @@ def store_or(option, opt_str, value, parser, *args, **kwargs): # pylint: disabl
229
232
"""Callback for supporting options with optional values."""
230
233
# see http://stackoverflow.com/questions/1229146/parsing-empty-options-in-python
231
234
# ugly code, optparse is crap
232
- if parser .rargs and not parser .rargs [0 ].startswith ('-' ):
235
+ if option .store_or == self .STORE_OR_FALSE and opt_str .startswith ("--%s-" % self .DISABLE ):
236
+ val = False
237
+ elif parser .rargs and not parser .rargs [0 ].startswith ('-' ):
233
238
val = option .check_value (opt_str , parser .rargs .pop (0 ))
234
239
else :
235
240
val = kwargs .get ('orig_default' , None )
@@ -247,7 +252,7 @@ def store_or(option, opt_str, value, parser, *args, **kwargs): # pylint: disabl
247
252
'orig_default' : copy .deepcopy (self .default ),
248
253
}
249
254
self .action = 'callback' # act as callback
250
- self .default = None
255
+ self .default = False if self . action == self . STORE_OR_FALSE else None
251
256
252
257
def process (self , opt , value , values , parser ):
253
258
"""Handle option-as-value issues before actually processing option."""
@@ -1212,6 +1217,8 @@ def add_group_parser(self, opt_dict, description, prefix=None, otherdefaults=Non
1212
1217
if action in self .parser .option_class .BOOLEAN_ACTIONS :
1213
1218
args .append ("--%s-%s" % (self .parser .option_class .ENABLE , opt_name ))
1214
1219
args .append ("--%s-%s" % (self .parser .option_class .DISABLE , opt_name ))
1220
+ elif action == self .parser .option_class .STORE_OR_FALSE :
1221
+ args .append ("--%s-%s" % (self .parser .option_class .DISABLE , opt_name ))
1215
1222
1216
1223
# force passed_kwargs as final nameds
1217
1224
nameds .update (passed_kwargs )
0 commit comments