roll.py 983 B

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