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
9 years ago
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. import re
  25. from docopt import docopt, Option, Argument
  26. class MyOption(Option):
  27. def __init__(self, *args, **kwargs):
  28. self.description = kwargs.pop('description', None)
  29. super(MyOption, self).__init__(*args, **kwargs)
  30. @classmethod
  31. def parse(class_, option_description):
  32. short, long, argcount, value = None, None, 0, False
  33. options, _, description = option_description.strip().partition(' ')
  34. options = options.replace(',', ' ').replace('=', ' ')
  35. for s in options.split():
  36. if s.startswith('--'):
  37. long = s
  38. elif s.startswith('-'):
  39. short = s
  40. else:
  41. argcount = 1
  42. if argcount:
  43. matched = re.findall(r'\[default: (.*)\]', description, flags=re.I)
  44. value = matched[0] if matched else None
  45. return class_(short, long, argcount, value, description=description.strip())
  46. def __repr__(self):
  47. return 'Option(%r, %r, %r, %r, %r)' % (self.short, self.long,
  48. self.argcount, self.value, self.description)
  49. if __name__ == '__main__':
  50. import sys
  51. a = docopt(__doc__)
  52. # import re
  53. # doc = __doc__
  54. # split = re.split('\n *(<\S+?>|-\S+?)', doc)[1:]
  55. # split = [s1 + s2 for s1, s2 in zip(split[::2], split[1::2])]
  56. # options = [MyOption.parse(s) for s in split if s.startswith('-')]
  57. # arguments = [Argument.parse(s) for s in split if s.startswith('<')]
  58. # #return options, arguments
  59. # print arguments
  60. # print options
  61. print a
  62. a = 10