Skip to content

Commit

Permalink
stackoverflow to the rescue so sync script doesnt require arguments. c…
Browse files Browse the repository at this point in the history
…loses #31
  • Loading branch information
Dean Malmgren committed Dec 15, 2015
1 parent f114a9b commit 370961c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions a_model/argparsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
from . import reports


# cherry picked from http://stackoverflow.com/a/8527629/564709
class DefaultListAction(argparse.Action):
CHOICES = list(reports.AVAILABLE_REPORTS)
def __call__(self, parser, namespace, values, option_string=None):
if values:
for value in values:
if value not in self.CHOICES:
actions = ', '.join([repr(action)
for action in self.CHOICES])
message = ("invalid choice: {0!r} (choose from {1})"
.format(value, actions))
raise argparse.ArgumentError(self, message)
setattr(namespace, self.dest, values)


class SyncParser(argparse.ArgumentParser):

def __init__(self, *args, **kwargs):
Expand All @@ -14,8 +29,8 @@ def __init__(self, *args, **kwargs):
nargs='*',
type=str,
help='specific reports you would like to sync',
choices=list(reports.AVAILABLE_REPORTS),
default=[],
action=DefaultListAction,
default=DefaultListAction.CHOICES,
)


Expand Down

0 comments on commit 370961c

Please sign in to comment.