206 """Manage defaults and mutations for any type of opt.""" |
206 """Manage defaults and mutations for any type of opt.""" |
207 |
207 |
208 __metaclass__ = abc.ABCMeta |
208 __metaclass__ = abc.ABCMeta |
209 |
209 |
210 def __init__(self, defaultvalue): |
210 def __init__(self, defaultvalue): |
211 self.defaultvalue = defaultvalue |
211 self._defaultvalue = defaultvalue |
212 |
212 |
213 def _isboolopt(self): |
213 def _isboolopt(self): |
214 return False |
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 @abc.abstractmethod |
223 @abc.abstractmethod |
217 def newstate(self, oldstate, newparam, abort): |
224 def newstate(self, oldstate, newparam, abort): |
218 """Adds newparam to oldstate and returns the new state. |
225 """Adds newparam to oldstate and returns the new state. |
219 |
226 |
220 On failure, abort can be called with a string error message.""" |
227 On failure, abort can be called with a string error message.""" |
221 |
228 |
222 class _simpleopt(customopt): |
229 class _simpleopt(customopt): |
223 def _isboolopt(self): |
230 def _isboolopt(self): |
224 return isinstance(self.defaultvalue, (bool, type(None))) |
231 return isinstance(self._defaultvalue, (bool, type(None))) |
225 |
232 |
226 def newstate(self, oldstate, newparam, abort): |
233 def newstate(self, oldstate, newparam, abort): |
227 return newparam |
234 return newparam |
228 |
235 |
229 class _callableopt(customopt): |
236 class _callableopt(customopt): |
233 |
240 |
234 def newstate(self, oldstate, newparam, abort): |
241 def newstate(self, oldstate, newparam, abort): |
235 return self.callablefn(newparam) |
242 return self.callablefn(newparam) |
236 |
243 |
237 class _listopt(customopt): |
244 class _listopt(customopt): |
|
245 def getdefaultvalue(self): |
|
246 return self._defaultvalue[:] |
|
247 |
238 def newstate(self, oldstate, newparam, abort): |
248 def newstate(self, oldstate, newparam, abort): |
239 oldstate.append(newparam) |
249 oldstate.append(newparam) |
240 return oldstate |
250 return oldstate |
241 |
251 |
242 class _intopt(customopt): |
252 class _intopt(customopt): |
311 for n in onames: |
321 for n in onames: |
312 argmap['--' + n] = name |
322 argmap['--' + n] = name |
313 defmap[name] = _defaultopt(default) |
323 defmap[name] = _defaultopt(default) |
314 |
324 |
315 # copy defaults to state |
325 # copy defaults to state |
316 state[name] = defmap[name].defaultvalue |
326 state[name] = defmap[name].getdefaultvalue() |
317 |
327 |
318 # does it take a parameter? |
328 # does it take a parameter? |
319 if not defmap[name]._isboolopt(): |
329 if not defmap[name]._isboolopt(): |
320 if short: |
330 if short: |
321 short += ':' |
331 short += ':' |