Show More
@@ -208,11 +208,18 b' class customopt(object):' | |||
|
208 | 208 | __metaclass__ = abc.ABCMeta |
|
209 | 209 | |
|
210 | 210 | def __init__(self, defaultvalue): |
|
211 | self.defaultvalue = defaultvalue | |
|
211 | self._defaultvalue = defaultvalue | |
|
212 | 212 | |
|
213 | 213 | def _isboolopt(self): |
|
214 | 214 | return False |
|
215 | 215 | |
|
216 | def getdefaultvalue(self): | |
|
217 | """Returns the default value for this opt. | |
|
218 | ||
|
219 | Subclasses should override this to return a new value if the value type | |
|
220 | is mutable.""" | |
|
221 | return self._defaultvalue | |
|
222 | ||
|
216 | 223 | @abc.abstractmethod |
|
217 | 224 | def newstate(self, oldstate, newparam, abort): |
|
218 | 225 | """Adds newparam to oldstate and returns the new state. |
@@ -221,7 +228,7 b' class customopt(object):' | |||
|
221 | 228 | |
|
222 | 229 | class _simpleopt(customopt): |
|
223 | 230 | def _isboolopt(self): |
|
224 | return isinstance(self.defaultvalue, (bool, type(None))) | |
|
231 | return isinstance(self._defaultvalue, (bool, type(None))) | |
|
225 | 232 | |
|
226 | 233 | def newstate(self, oldstate, newparam, abort): |
|
227 | 234 | return newparam |
@@ -235,6 +242,9 b' class _callableopt(customopt):' | |||
|
235 | 242 | return self.callablefn(newparam) |
|
236 | 243 | |
|
237 | 244 | class _listopt(customopt): |
|
245 | def getdefaultvalue(self): | |
|
246 | return self._defaultvalue[:] | |
|
247 | ||
|
238 | 248 | def newstate(self, oldstate, newparam, abort): |
|
239 | 249 | oldstate.append(newparam) |
|
240 | 250 | return oldstate |
@@ -313,7 +323,7 b' def fancyopts(args, options, state, gnu=' | |||
|
313 | 323 | defmap[name] = _defaultopt(default) |
|
314 | 324 | |
|
315 | 325 | # copy defaults to state |
|
316 | state[name] = defmap[name].defaultvalue | |
|
326 | state[name] = defmap[name].getdefaultvalue() | |
|
317 | 327 | |
|
318 | 328 | # does it take a parameter? |
|
319 | 329 | if not defmap[name]._isboolopt(): |
@@ -87,7 +87,7 b' def optrst(header, options, verbose):' | |||
|
87 | 87 | lo = '--' + longopt |
|
88 | 88 | |
|
89 | 89 | if isinstance(default, fancyopts.customopt): |
|
90 | default = default.defaultvalue | |
|
90 | default = default.getdefaultvalue() | |
|
91 | 91 | if default and not callable(default): |
|
92 | 92 | # default is of unknown type, and in Python 2 we abused |
|
93 | 93 | # the %s-shows-repr property to handle integers etc. To |
General Comments 0
You need to be logged in to leave comments.
Login now