Skip to content

Commit

Permalink
Merge branch 'main' into uprev
Browse files Browse the repository at this point in the history
  • Loading branch information
ianfhunter authored Nov 25, 2023
2 parents a8d0989 + 370021d commit 7c6bd6b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish_PyPi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel
pip install wheel setuptools
make python
ls src/python
echo "-------"
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel twine
pip install wheel twine setuptools
make python
ls src/python
echo "-------"
Expand Down
117 changes: 59 additions & 58 deletions src/python/code/gnoll/__main__.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,93 @@
"""Roll some dice with GNOLL."""

import sys
import argparse
import sys

import gnoll


def parse_cmdline_args(args):
"""Extract values from the commandline
@param args - the arguments from the commandline (excluding the python3 call)
"""
p = argparse.ArgumentParser(
description=__doc__,
usage='python3 -m gnoll [options] EXPR',
add_help=False)
p = argparse.ArgumentParser(description=__doc__,
usage="python3 -m gnoll [options] EXPR",
add_help=False)

p.add_argument(
'EXPR',
nargs='+',
help='a dice expression to evaluate'
'(multiple arguments will be joined with spaces)'
)

g = p.add_argument_group('main options')
g.add_argument(
'-h',
'--help',
action='help',
help='show this help message and exit'
)
g.add_argument(
'-b',
'--breakdown',
action='store_true',
help='show a breakdown into individual dice'
)
g.add_argument(
'--no-builtins',
action='store_true',
help='disable built-in macros'
"EXPR",
nargs="+",
help="a dice expression to evaluate"
"(multiple arguments will be joined with spaces)",
)

g = p.add_argument_group('debugging options')
g.add_argument(
'-v',
'--verbose',
action='store_true',
help='enable verbosity'
)
g = p.add_argument_group("main options")
g.add_argument("-h",
"--help",
action="help",
help="show this help message and exit")
g.add_argument(
'--keep-temp-file',
action='store_true',
help="don't delete the created temporary file"
"-b",
"--breakdown",
action="store_true",
help="show a breakdown into individual dice",
)
g.add_argument(
'--mock',
metavar='TYPE',
"-n",
"--times",
metavar="N",
type=int,
help='mocking type'
default=1,
help="execute the entire expression N times",
)
g.add_argument("--no-builtins",
action="store_true",
help="disable built-in macros")

g = p.add_argument_group("debugging options")
g.add_argument("-v",
"--verbose",
action="store_true",
help="enable verbosity")
g.add_argument(
'--mock-const',
metavar='N',
type=int,
default=3,
help='mocking constant'
"--keep-temp-file",
action="store_true",
help="don't delete the created temporary file",
)
g.add_argument("--mock", metavar="TYPE", type=int, help="mocking type")
g.add_argument("--mock-const",
metavar="N",
type=int,
default=3,
help="mocking constant")

a = p.parse_args(args)
a.EXPR = ' '.join(a.EXPR)
a.EXPR = " ".join(a.EXPR)
return a


def main(EXPR, no_builtins, **kwargs):
def main(EXPR, times, no_builtins, **kwargs):
"""
The entry point for gnoll when called via `python -m gnoll`
@param EXPR - the expression
@param times - number of times to execute
@param no_builtins - a flag to disable builtins
@param **kwargs - other key word arguments to be passed to gnoll.roll
"""
_, [[result]], breakdown = gnoll.roll(
EXPR,
builtins=not no_builtins,
**kwargs)
if breakdown:
print(breakdown[0], '-->', result)
else:
print(result)
for _ in range(times):
_, [[result]], breakdown = gnoll.roll(EXPR,
builtins=not no_builtins,
**kwargs)
yield (breakdown[0], "-->", result) if breakdown else (result, )


def main_with_args(args):
"""Parse the commandline args and then run `main`
@param args - the arguments from the commandline (excluding the python3 call)
"""
yield from main(**vars(parse_cmdline_args(args)))


if __name__ == '__main__':
main(**vars(parse_cmdline_args(sys.argv[1:])))
if __name__ == "__main__":
for line in main_with_args(sys.argv[1:]):
print(*line)
19 changes: 19 additions & 0 deletions tests/python/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import gnoll.__main__

m = lambda *x: list(gnoll.__main__.main_with_args(x))


def test_cli():
[[r]] = m("1d4")
assert isinstance(r, int)

[[(die1, die2), a, r]] = m("2d4", "--breakdown")
assert all((isinstance(x, int) for x in [die1, die2, r]))
assert a == "-->"

executions = m("4d6kh3", "+", "1", "--breakdown", "--times", "6",
"--no-builtins")
assert len(executions) == 6
for (die1, die2, die3, die4), a, r in executions:
assert all((isinstance(x, int) for x in [die1, die2, die3, die4, r]))
assert a == "-->"

0 comments on commit 7c6bd6b

Please sign in to comment.