Skip to content

Commit 36a8b5a

Browse files
committed
Added an improved command line interface
1 parent 35a7c94 commit 36a8b5a

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

dice/__init__.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
22

33
from __future__ import absolute_import, print_function, unicode_literals
44

5-
from sys import argv
5+
import sys
6+
import argparse
67

7-
from pyparsing import ParseException
8+
import dice.grammar
9+
import dice.utilities
10+
11+
__all__ = ['roll', 'main', 'Dice', 'Roll', 'Bag', 'ParseException']
12+
__author__ = "Sam Clements <[email protected]>"
13+
__version__ = "0.2.0"
814

15+
from pyparsing import ParseException
916
from dice.elements import Dice, Roll, Bag
10-
from dice.grammar import expression
1117

12-
__all__ = ['roll', 'main', 'Dice', 'Roll', 'Bag', 'ParseException']
18+
parser = argparse.ArgumentParser()
19+
parser.add_argument(
20+
'-v', '--version', action='version',
21+
version='dice v{0} by {1}'.format(__version__, __author__))
22+
parser.add_argument(
23+
'-s', '--single', action='store_true', dest='single',
24+
help="if a single element is returned, extract it from the list")
25+
parser.add_argument(
26+
'expression',
27+
help="the expression to parse and roll")
28+
29+
def roll(string, single=False):
30+
result = dice.grammar.notation.parseString(string, parseAll=True)
31+
return dice.utilities.single(result) if single else result
1332

14-
def roll(string):
15-
return expression.parseString(string, parseAll=True)
33+
def main(*argv):
34+
args = parser.parse_args(sys.argv if len(argv) == 0 else argv)
1635

17-
def main(argv=argv):
18-
return roll(argv)
36+
return roll(args.expression, single=args.single)

dice/utilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ def classname(obj):
88
"""Returns the name of an objects class"""
99
return obj.__class__.__name__
1010

11+
def single(iterable):
12+
"""Returns a single item if the iterable has only one item"""
13+
return iterable[0] if len(iterable) == 1 else list(iterable)
14+
1115
def patch_pyparsing(packrat=True, arity=True, verbose=True):
1216
"""Applies monkey-patches to pyparsing"""
1317
if packrat:

setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name = 'dice',
8-
version = '0.1.7',
8+
version = '0.2.0',
99

1010
author = "Sam Clements",
1111
author_email = "[email protected]",
@@ -16,12 +16,16 @@
1616

1717
packages = find_packages(),
1818
install_requires = [
19+
'argparse==1.2.1',
1920
'pyparsing==2.0.1',
2021
'six==1.3.0'
2122
],
2223

2324
entry_points = {
24-
'console_scripts': ['roll = dice:main']
25+
'console_scripts': [
26+
'dice = dice:main',
27+
'roll = dice:main'
28+
]
2529
},
2630

2731
classifiers = [

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ basepython=python2.7
3030
commands=
3131
coverage run --rcfile tox.ini --source dice -m py.test
3232
coverage html --rcfile tox.ini
33-
coverage report --rcfile tox.ini
3433
deps=
3534
{[testenv]deps}
3635
coverage

0 commit comments

Comments
 (0)