Skip to content

Commit 82051f9

Browse files
committed
Drop docopt
1 parent b8be8e1 commit 82051f9

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The command line arguments are as follows:
2424
* `-V` `--version` Show the package version
2525

2626
If your expression begins with a dash (`-`), then put a double dash (`--`)
27-
before it to prevent docopt from trying to process it as a command option.
27+
before it to prevent the parser from trying to process it as a command option.
2828
Example: `roll -- -10d6`. Alternatively, use parenthesis: `roll (-10d6)`.
2929

3030
### Python API
@@ -49,8 +49,7 @@ command-line tool, I recommend [pipx].
4949

5050
A recent version of Python 3 (3.8 or above) is required. You can probably run
5151
it or easily adapt it for older versions of Python, but I don't support any
52-
end-of-life Python versions. Beyond that, the only dependencies are the
53-
`docopt` and `pyparsing` libraries.
52+
end-of-life Python versions. Beyond that, the only dependency is the `pyparsing` library.
5453

5554
[pipx]: https://pypa.github.io/pipx/
5655

dice/command.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,51 @@
1111
-V --version Show the package version
1212
"""
1313

14-
import docopt
14+
import argparse
1515

1616
import dice
17-
from dice.exceptions import DiceBaseException
17+
import dice.exceptions
1818

19-
__version__ = "dice v{0} by {1}".format(dice.__version__, dice.__author__)
19+
__version__ = "dice v{0} by {1}.".format(dice.__version__, dice.__author__)
2020

21+
parser = argparse.ArgumentParser(prog="dice", description="Parse and evaluate dice notation.", epilog=__version__)
22+
parser.add_argument('-m', '--min', action="store_true", help="Make all rolls the lowest possible result.")
23+
parser.add_argument('-M', '--max', action="store_true", help="Make all rolls the highest possible result.")
24+
parser.add_argument('-D', '--max-dice', action="store", type=int, metavar='N', help="Set the maximum number of dice per element.")
25+
parser.add_argument('-v', '--verbose', action="store_true", help="Show additional output.")
26+
parser.add_argument('-V', '--version', action="version", version=__version__, help="Show the package version.")
27+
parser.add_argument('expression', nargs='+')
2128

22-
def main(argv=None):
23-
"""Run roll() from a command line interface"""
24-
args = docopt.docopt(__doc__, argv=argv, version=__version__)
25-
verbose = bool(args["--verbose"])
2629

27-
f_roll = dice.roll
28-
kwargs = {}
30+
def main(args=None):
31+
"""Run roll() from a command line interface"""
32+
args = parser.parse_args(args=args)
33+
f_kwargs = {}
2934

30-
if args["--min"]:
35+
if args.min:
3136
f_roll = dice.roll_min
32-
elif args["--max"]:
37+
elif args.max:
3338
f_roll = dice.roll_max
39+
else:
40+
f_roll = dice.roll
3441

35-
if args["--max-dice"]:
36-
try:
37-
kwargs["max_dice"] = int(args["--max-dice"])
38-
except ValueError:
39-
print("Invalid value for --max-dice: '%s'" % args["--max-dice"])
40-
exit(1)
42+
if args.max_dice:
43+
f_kwargs['max_dice'] = args.max_dice
4144

42-
expr = " ".join(args["<expression>"])
45+
f_expr = " ".join(args.expression)
4346

4447
try:
45-
roll, kwargs = f_roll(expr, raw=True, return_kwargs=True, **kwargs)
48+
roll, kwargs = f_roll(f_expr, raw=True, return_kwargs=True, **f_kwargs)
4649

47-
if verbose:
50+
if args.verbose:
4851
print("Result: ", end="")
4952

5053
print(str(roll.evaluate_cached(**kwargs)))
5154

52-
if verbose:
55+
if args.verbose:
5356
print("Breakdown:")
5457
print(dice.utilities.verbose_print(roll, **kwargs))
55-
except DiceBaseException as e:
58+
except dice.exceptions.DiceBaseException as e:
5659
print("Whoops! Something went wrong:")
5760
print(e.pretty_print())
5861
exit(1)

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ classifiers = [
3939
]
4040
keywords = ["dice"]
4141
dependencies = [
42-
"docopt>=0.6.1",
4342
"pyparsing>=2.4.1",
4443
]
4544
urls = { homepage = "https://github.com/borntyping/python-dice" }

0 commit comments

Comments
 (0)