Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Issue #2 in cro-ki/xdice #3

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions roll.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
#! python3
"""
Usage:
roll [options] <expr>

Options:
-n Numeric score only
-v Verbose result

-n --num_only Numeric score only (Verbose result default)
-h --help Displays help message
--version Displays current xdice version
"""
import sys

import argparse
import xdice

def _print_and_exit(string, exit_code=0):
""" print and exit """
print(string)
sys.exit(exit_code)

# Parse arguments
args = sys.argv[1:]

if "-h" in args:
_print_and_exit(__doc__)

if "--version" in args:
_print_and_exit("xdice {}".format(xdice.__VERSION__))

score_only = False
if "-n" in args:
score_only = True
args.remove("-n")

verbose = False
if "-v" in args:
verbose = True
args.remove("-v")

if len(args) != 1:
_print_and_exit("xdice CLI: invalid arguments\n" + __doc__, 1)

pattern_string = args[0]

# Run xdice
ps = xdice.roll(pattern_string)

if score_only:
print(ps)
else:
print("{}\t({})".format(ps, ps.format(verbose)))
def main():
"""Main command line entry point."""
parser = argparse.ArgumentParser(
prog="roll", description="Command Line Interface to the xdice library."
)
parser.add_argument(
"expression",
nargs="+",
help="Mathematical expression containing dice format <n>d<s> objects.",
)
parser.add_argument(
"-v",
"--version",
action="store_true",
help="Prints the xdice library version string and exits.",
)
parser.add_argument(
"-n",
"--num_only",
action="store_true",
help="Prints numeric result only. Otherwise full verbose result is returned.",
)
args = parser.parse_args()

if args.version:
print("Version {}".format(xdice.__VERSION__))
else:
if args.expression:
expr = "".join(args.expression)
ps = xdice.roll(expr)
if args.num_only:
print(ps)
else:
print("{}\t({})".format(ps, ps.format(True)))


if __name__ == "__main__":
main()
7 changes: 6 additions & 1 deletion xdice.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import random
import re

__VERSION__ = 1.2
__VERSION__ = "1.2.1"

def compile(pattern_string): # @ReservedAssignment
"""
Expand Down Expand Up @@ -252,6 +252,11 @@ def __repr__(self):
self.dropped,
self.name)

def __str__(self):
"""Returns a usable string representation for use with evaluating
a mathematical result."""
return "{}".format(int(self))

def format(self, verbose=False):
"""
Return a formatted string detailing the score of the Dice roll.
Expand Down