-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Python command-line interface (#441)
* In Python, delete the temporary file by default * Add a Python command-line interface * Apply style fixes Signed-off-by: Ian Hunter <ian.hunter@intel.com> * more style fixes * fix trailing whitespace --------- Signed-off-by: Ian Hunter <ian.hunter@intel.com> Co-authored-by: Ian Hunter <ian.hunter@intel.com>
- Loading branch information
1 parent
989487f
commit 52d65ac
Showing
3 changed files
with
140 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""Roll some dice with GNOLL.""" | ||
|
||
import sys | ||
import argparse | ||
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.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' | ||
) | ||
|
||
g = p.add_argument_group('debugging options') | ||
g.add_argument( | ||
'-v', | ||
'--verbose', | ||
action='store_true', | ||
help='enable verbosity' | ||
) | ||
g.add_argument( | ||
'--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) | ||
return a | ||
|
||
|
||
def main(EXPR, no_builtins, **kwargs): | ||
""" | ||
The entry point for gnoll when called via `python -m gnoll` | ||
@param EXPR - the expression | ||
@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) | ||
|
||
|
||
if __name__ == '__main__': | ||
main(**vars(parse_cmdline_args(sys.argv[1:]))) |