@@ -185,6 +185,8 @@ class ExtOption(CompleterOption):
185185 - set default to None if no option passed,
186186 - set to default if option without value passed,
187187 - set to value if option with value passed
188+ - store_or_False: Same as store_or_None, but set to False instead of None
189+ - Additionally supports --disable-
188190
189191 Types:
190192 - strlist, strtuple : convert comma-separated string in a list resp. tuple of strings
@@ -196,14 +198,15 @@ class ExtOption(CompleterOption):
196198
197199 ENABLE = 'enable' # do nothing
198200 DISABLE = 'disable' # inverse action
201+ STORE_OR_FALSE = 'store_or_False'
199202
200203 EXTOPTION_EXTRA_OPTIONS = ('date' , 'datetime' , 'regex' , 'add' , 'add_first' , 'add_flex' ,)
201- EXTOPTION_STORE_OR = ('store_or_None' , 'help' ) # callback type
204+ EXTOPTION_STORE_OR = ('store_or_None' , STORE_OR_FALSE , 'help' ) # callback type
202205 EXTOPTION_LOG = ('store_debuglog' , 'store_infolog' , 'store_warninglog' ,)
203206 EXTOPTION_HELP = ('shorthelp' , 'confighelp' , 'help' )
204207
205208 ACTIONS = Option .ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_STORE_OR + EXTOPTION_LOG + EXTOPTION_HELP
206- STORE_ACTIONS = Option .STORE_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_LOG + ('store_or_None' ,)
209+ STORE_ACTIONS = Option .STORE_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_LOG + ('store_or_None' , STORE_OR_FALSE )
207210 TYPED_ACTIONS = Option .TYPED_ACTIONS + EXTOPTION_EXTRA_OPTIONS + EXTOPTION_STORE_OR
208211 ALWAYS_TYPED_ACTIONS = Option .ALWAYS_TYPED_ACTIONS + EXTOPTION_EXTRA_OPTIONS
209212
@@ -232,7 +235,9 @@ def store_or(option, opt_str, value, parser, *args, **kwargs): # pylint: disabl
232235 """Callback for supporting options with optional values."""
233236 # see http://stackoverflow.com/questions/1229146/parsing-empty-options-in-python
234237 # ugly code, optparse is crap
235- if parser .rargs and not parser .rargs [0 ].startswith ('-' ):
238+ if option .store_or == self .STORE_OR_FALSE and opt_str .startswith ("--%s-" % self .DISABLE ):
239+ val = False
240+ elif parser .rargs and not parser .rargs [0 ].startswith ('-' ):
236241 val = option .check_value (opt_str , parser .rargs .pop (0 ))
237242 else :
238243 val = kwargs .get ('orig_default' , None )
@@ -250,11 +255,7 @@ def store_or(option, opt_str, value, parser, *args, **kwargs): # pylint: disabl
250255 'orig_default' : copy .deepcopy (self .default ),
251256 }
252257 self .action = 'callback' # act as callback
253-
254- if self .store_or in self .EXTOPTION_STORE_OR :
255- self .default = None
256- else :
257- self .log .raiseException ("_set_attrs: unknown store_or %s" % self .store_or , exception = ValueError )
258+ self .default = False if self .action == self .STORE_OR_FALSE else None
258259
259260 def process (self , opt , value , values , parser ):
260261 """Handle option-as-value issues before actually processing option."""
@@ -1219,6 +1220,8 @@ def add_group_parser(self, opt_dict, description, prefix=None, otherdefaults=Non
12191220 if action in self .parser .option_class .BOOLEAN_ACTIONS :
12201221 args .append ("--%s-%s" % (self .parser .option_class .ENABLE , opt_name ))
12211222 args .append ("--%s-%s" % (self .parser .option_class .DISABLE , opt_name ))
1223+ elif action == self .parser .option_class .STORE_OR_FALSE :
1224+ args .append ("--%s-%s" % (self .parser .option_class .DISABLE , opt_name ))
12221225
12231226 # force passed_kwargs as final nameds
12241227 nameds .update (passed_kwargs )
0 commit comments