You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.1 KiB

9 years ago
  1. """
  2. Naval Fate.
  3. Usage:
  4. naval_fate.py ship new <name>...
  5. naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  6. naval_fate.py ship shoot <x> <y>
  7. naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  8. naval_fate.py -h | --help
  9. naval_fate.py --version
  10. Options:
  11. -h --help Show this screen.
  12. --version Show version.
  13. --speed=<kn> Speed in knots [default: 10].
  14. --moored Moored (anchored) mine.
  15. --drifting Drifting mine.
  16. """
  17. # Standard
  18. # choice
  19. # counter
  20. # flag
  21. # mutually_exclusive
  22. #
  23. # types?
  24. from docopt import docopt, Option, Argument
  25. class MyOption(Option):
  26. def __init__(self, *args, **kwargs):
  27. self.description = kwargs.pop('description', None)
  28. super(MyOption, self).__init__(*args, **kwargs)
  29. @classmethod
  30. def parse(class_, option_description):
  31. short, long, argcount, value = None, None, 0, False
  32. options, _, description = option_description.strip().partition(' ')
  33. options = options.replace(',', ' ').replace('=', ' ')
  34. for s in options.split():
  35. if s.startswith('--'):
  36. long = s
  37. elif s.startswith('-'):
  38. short = s
  39. else:
  40. argcount = 1
  41. if argcount:
  42. matched = re.findall('\[default: (.*)\]', description, flags=re.I)
  43. value = matched[0] if matched else None
  44. return class_(short, long, argcount, value, description=description.strip())
  45. def __repr__(self):
  46. return 'Option(%r, %r, %r, %r, %r)' % (self.short, self.long,
  47. self.argcount, self.value, self.description)
  48. if __name__ == '__main__':
  49. import sys
  50. a = docopt(__doc__)
  51. # import re
  52. # doc = __doc__
  53. # split = re.split('\n *(<\S+?>|-\S+?)', doc)[1:]
  54. # split = [s1 + s2 for s1, s2 in zip(split[::2], split[1::2])]
  55. # options = [MyOption.parse(s) for s in split if s.startswith('-')]
  56. # arguments = [Argument.parse(s) for s in split if s.startswith('<')]
  57. # #return options, arguments
  58. # print arguments
  59. # print options
  60. print a
  61. a = 10