roll.py 824 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """
  2. Usage:
  3. roll [options] <expr>
  4. Options:
  5. -s Numeric score only
  6. -h --help Displays help message
  7. --version Displays current xdice version
  8. """
  9. import sys
  10. import xdice
  11. def print_ex(string, exit_code=0):
  12. """ print and exit """
  13. print(string)
  14. sys.exit(exit_code)
  15. # Parse arguments
  16. args = sys.argv[1:]
  17. if "-h" in args:
  18. print_ex(__doc__)
  19. if "-v" in args:
  20. print_ex("xdice {}".format(xdice.__VERSION__))
  21. score_only = False
  22. if "-s" in args:
  23. score_only = True
  24. args.remove("-s")
  25. if len(args) != 1:
  26. print_ex("xdice CLI: invalid arguments\n" + __doc__, 1)
  27. pattern_string = args[0]
  28. # Run xdice
  29. ps = xdice.roll(pattern_string)
  30. if score_only:
  31. print(ps)
  32. else:
  33. print("{}\t({})".format(ps, ps.format()))