I must note that it's not at all certain a negative status makes sense for sys.exit(); at least on Linux, it will be interpreted as an unsigned 8-bit value (range 0-255). As for an enumerated type, it's possible to do something like:
class ExitStatus: pass
for code, name in enumerate("Success Failure CriticalFailure".split()):
setattr(ExitStatus, name, code)
Resulting in something like:
>>> ExitStatus.__dict__
{'CriticalFailure': 2, 'Failure': 1, '__module__': '__main__',
'__doc__': None, 'Success': 0}
The predefined values in normal Unix systems are EXIT_FAILURE=1 and EXIT_SUCCESS=0.
Addendum: Considering the concern about IDE identification of identifiers, one could also do something like:
class EnumItem: pass
def adjustEnum(enum):
value=0
enumdict=enum.__dict__
for k,v in enumdict.items():
if isinstance(v,int):
if v>=value:
value=v+1
for k,v in enumdict.items():
if v is EnumItem:
enumdict[k]=value
value+=1
class ExitStatus:
Success=0
Failure=EnumItem
CriticalFailure=EnumItem
adjustEnum(ExitStatus)
Second edit: Couldn't keep away. Here's a variant that assigns the values in the order you've written the names.
class EnumItem:
serial=0
def __init__(self):
self.serial=self.__class__.serial
self.__class__.serial+=1
def adjustEnum(enum):
enumdict=enum.__dict__
value=0
unknowns={}
for k,v in enumdict.items():
if isinstance(v,int):
if v>=value:
value=v+1
elif isinstance(v,EnumItem):
unknowns[v.serial]=k
for i,k in sorted(unknowns.items()):
enumdict[k]=value
value+=1
return enum
@adjustEnum
class ExitStatus:
Success=0
Failure=EnumItem()
CriticalFailure=EnumItem()
Obviously the growing complexity is inelegant, but it does work.