summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2015-01-21 21:11:10 -0500
committerDoug Hellmann <doug@doughellmann.com>2015-01-21 21:12:22 -0500
commit0e15691158bd64dd0e29c0144b42029da500400b (patch)
treef672436898efa82114094f8a8709bfae754e25dc
parent820caf5555e95f6d5dc0a3c151c9252673f1b376 (diff)
Let run and grep take arbitrary arguments
-rw-r--r--aeromancer/cli/grep.py17
-rw-r--r--aeromancer/cli/run.py30
2 files changed, 30 insertions, 17 deletions
diff --git a/aeromancer/cli/grep.py b/aeromancer/cli/grep.py
index 3ad58b2..657bdea 100644
--- a/aeromancer/cli/grep.py
+++ b/aeromancer/cli/grep.py
@@ -10,17 +10,14 @@ from aeromancer.cli.run import ProjectShellCommandBase
10 10
11 11
12class Grep(ProjectShellCommandBase): 12class Grep(ProjectShellCommandBase):
13 """Search the contents of files""" 13 """Search the contents of files
14 14
15 log = logging.getLogger(__name__) 15 Accepts most of the arguments of git-grep, unless they conflict
16 with other arguments to this command.
17
18 """
16 19
17 def get_parser(self, prog_name): 20 log = logging.getLogger(__name__)
18 parser = super(Grep, self).get_parser(prog_name)
19 parser.add_argument('pattern',
20 action='store',
21 help='regular expression',
22 )
23 return parser
24 21
25 def _get_command(self, parsed_args): 22 def _get_command(self, parsed_args):
26 return ['git', 'grep', parsed_args.pattern] 23 return ['git', 'grep'] + self._extra
diff --git a/aeromancer/cli/run.py b/aeromancer/cli/run.py
index 4363c0e..c60d6d9 100644
--- a/aeromancer/cli/run.py
+++ b/aeromancer/cli/run.py
@@ -10,6 +10,23 @@ from aeromancer import project_filter
10from cliff.command import Command 10from cliff.command import Command
11 11
12 12
13class ArgumentParserWrapper(object):
14 """Wrap a regular argument parser to replace
15 parse_args with parse_known_args.
16
17 Cliff calls parse_args() for subcommands, but we want
18 parse_known_args() so any extra values that look like command
19 switches will be ignored.
20
21 """
22
23 def __init__(self, parser):
24 self._parser = parser
25
26 def parse_args(self, argv):
27 return self._parser.parse_known_args(argv)
28
29
13class ProjectShellCommandBase(Command): 30class ProjectShellCommandBase(Command):
14 """Run a command for each project""" 31 """Run a command for each project"""
15 32
@@ -27,7 +44,7 @@ class ProjectShellCommandBase(Command):
27 help=('separator between project name and command output, ' 44 help=('separator between project name and command output, '
28 'defaults to %(default)r'), 45 'defaults to %(default)r'),
29 ) 46 )
30 return parser 47 return ArgumentParserWrapper(parser)
31 48
32 def _show_text_output(self, parsed_args, project, out): 49 def _show_text_output(self, parsed_args, project, out):
33 for line in out.decode('utf-8').splitlines(): 50 for line in out.decode('utf-8').splitlines():
@@ -39,7 +56,10 @@ class ProjectShellCommandBase(Command):
39 def _show_output(self, parsed_args, proj_obj, out, err): 56 def _show_output(self, parsed_args, proj_obj, out, err):
40 self._show_text_output(parsed_args, proj_obj, err or out) 57 self._show_text_output(parsed_args, proj_obj, err or out)
41 58
42 def take_action(self, parsed_args): 59 def take_action(self, parsed_args_tuple):
60 # Handle the tuple we'll get from the parser wrapper.
61 parsed_args, extra = parsed_args_tuple
62 self._extra = extra
43 session = self.app.get_db_session() 63 session = self.app.get_db_session()
44 pm = project.ProjectManager(session) 64 pm = project.ProjectManager(session)
45 prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args) 65 prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
@@ -58,11 +78,7 @@ class Run(ProjectShellCommandBase):
58 78
59 def get_parser(self, prog_name): 79 def get_parser(self, prog_name):
60 parser = super(Run, self).get_parser(prog_name) 80 parser = super(Run, self).get_parser(prog_name)
61 parser.add_argument('command',
62 action='store',
63 help='the command to run, probably quoted',
64 )
65 return parser 81 return parser
66 82
67 def _get_command(self, parsed_args): 83 def _get_command(self, parsed_args):
68 return shlex.shlex(parsed_args.command) 84 return self._extra