|
7 | 7 | import dice.grammar |
8 | 8 | import dice.utilities |
9 | 9 |
|
10 | | -__all__ = ['parse', 'roll', 'main', 'ParseException'] |
| 10 | +__all__ = ['roll', 'main', 'ParseException'] |
11 | 11 | __author__ = "Sam Clements <[email protected]>" |
12 | | -__version__ = "0.2.0" |
| 12 | +__version__ = "0.2.1" |
13 | 13 |
|
14 | 14 | from pyparsing import ParseException |
15 | 15 |
|
16 | 16 | parser = argparse.ArgumentParser() |
17 | 17 | parser.add_argument( |
18 | | - '-v', '--version', action='version', |
| 18 | + '-V', '--version', action='version', |
19 | 19 | version='dice v{0} by {1}'.format(__version__, __author__)) |
| 20 | +parser.add_argument( |
| 21 | + '-v', '--verbose', action='store_true', |
| 22 | + help="show additional output") |
20 | 23 | parser.add_argument( |
21 | 24 | 'expression', |
22 | 25 | help="the expression to parse and roll") |
23 | 26 |
|
24 | | -def _(obj): |
25 | | - print(obj) |
26 | | - return obj |
27 | | - |
28 | 27 | def parse(string, grammar): |
29 | 28 | """Returns an AST parsed from an expression""" |
30 | | - return _(grammar.parseString(string, parseAll=True)) |
| 29 | + return grammar.parseString(string, parseAll=True) |
31 | 30 |
|
32 | | -def evaluate(string, grammar): |
| 31 | +def evaluate(string, grammar, **options): |
33 | 32 | """Parse and then evaluate a string with a grammar""" |
34 | | - return _([element.evaluate() for element in parse(string, grammar)]) |
| 33 | + return [e.evaluate(**options) for e in parse(string, grammar)] |
35 | 34 |
|
36 | | -def roll(string): |
| 35 | +def roll(string, verbose=False): |
37 | 36 | """Parses and evaluates an expression""" |
38 | | - return evaluate(string, dice.grammar.expression) |
| 37 | + return evaluate(string, grammar=dice.grammar.expression, verbose=verbose) |
39 | 38 |
|
40 | 39 | def main(argv=None): |
41 | 40 | args = parser.parse_args(argv) |
42 | | - result = roll(args.expression) |
43 | | - print("Result:", dice.utilities.single(result)) |
| 41 | + result = roll(args.expression, verbose=args.verbose) |
| 42 | + result = dice.utilities.single(result) |
| 43 | + if args.verbose: |
| 44 | + print("Result:", end=" ") |
44 | 45 | return result |
0 commit comments