argument.py 861 B

123456789101112131415161718192021222324252627282930313233
  1. class Argument(object):
  2. def __init__(self, *args, **kw):
  3. super(Argument, self).__init__()
  4. self.args = args # positional arugments
  5. self.kw = kw # keyword arguments
  6. @classmethod
  7. def int(cls, *args, **kw):
  8. arg_type = kw.pop("type", int)
  9. return cls(*args, type=arg_type, **kw)
  10. @classmethod
  11. def float(cls, *args, **kw):
  12. arg_type = kw.pop("type", float)
  13. return cls(*args, type=arg_type, **kw)
  14. @classmethod
  15. def flag(cls, *args, **kw):
  16. action = kw.pop("action", "store_true")
  17. return cls(*args, action=action, **kw)
  18. class FileArgument(Argument):
  19. def __init__(self, *args, **kw):
  20. super(FileArgument, self).__init__(*args, **kw)
  21. @classmethod
  22. def mode(cls, file_mode, encoding=None):
  23. def wrapper(*args, **kw):
  24. obj = cls(*args, **kw)
  25. obj.kw["type"] = argparse.FileType(file_mode, encoding=encoding)
  26. return obj
  27. return wrapper