Skip to content

Commit

Permalink
Renaming sub-command action (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwatts15 committed Jun 16, 2018
1 parent a5d7842 commit f4f6684
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions PyOpenWorm/cli_command_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class CLIArgMapper(object):
def __init__(self):
self.mappings = dict()
self.methodname = None
self.runfunc = None
self.runners = dict()
''' Mapping from subcommand names to functions which run for them '''

Expand All @@ -59,16 +58,14 @@ def apply(self, runner):

if self.methodname:
runmethod = self.runners.get(self.methodname, None)
elif self.runfunc:
runmethod = self.runfunc

if runmethod is None:
if callable(runner):
runmethod = runner
else:
self.argparser.print_help(file=sys.stderr)
print(file=sys.stderr)
raise CLIUserError("")
raise CLIUserError('Please specify a sub-command')

for k, v in iattrs.items():
setattr(runner, k, v)
Expand Down Expand Up @@ -119,9 +116,9 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, items)


class CLISubCommandMethodAction(argparse._SubParsersAction):
class CLISubCommandAction(argparse._SubParsersAction):
def __init__(self, mapper, *args, **kwargs):
super(CLISubCommandMethodAction, self).__init__(*args, **kwargs)
super(CLISubCommandAction, self).__init__(*args, **kwargs)
self.mapper = mapper

def __call__(self, *args, **kwargs):
Expand All @@ -131,7 +128,7 @@ def __call__(self, *args, **kwargs):
' set.'.format(self.dest, self.mapper.methodname))

self.mapper.methodname = args[2][0]
super(CLISubCommandMethodAction, self).__call__(*args, **kwargs)
super(CLISubCommandAction, self).__call__(*args, **kwargs)


def _ensure_value(namespace, name, value):
Expand Down Expand Up @@ -177,7 +174,7 @@ def parser(self, parser=None):
if parser is None:
parser = argparse.ArgumentParser()
self.mapper.argparser = parser
sp = parser.add_subparsers(dest='subparser', mapper=self.mapper, action=CLISubCommandMethodAction)
sp = parser.add_subparsers(dest='subparser', mapper=self.mapper, action=CLISubCommandAction)
for key, val in vars(self.runner).items():
if not key.startswith('_'):
parser.add_argument('--' + key, help=key.__doc__)
Expand Down Expand Up @@ -258,4 +255,4 @@ def main(self, args=None):
try:
self.mapper.apply(self.runner)
except CLIUserError as e:
print(e)
print(e, file=sys.stderr)

0 comments on commit f4f6684

Please sign in to comment.