Browse Source

Let run and grep take arbitrary arguments

Doug Hellmann 4 years ago
parent
commit
0e15691158
2 changed files with 30 additions and 17 deletions
  1. 7
    10
      aeromancer/cli/grep.py
  2. 23
    7
      aeromancer/cli/run.py

+ 7
- 10
aeromancer/cli/grep.py View File

@@ -10,17 +10,14 @@ from aeromancer.cli.run import ProjectShellCommandBase
10 10
 
11 11
 
12 12
 class 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):
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
20
+    log = logging.getLogger(__name__)
24 21
 
25 22
     def _get_command(self, parsed_args):
26
-        return ['git', 'grep', parsed_args.pattern]
23
+        return ['git', 'grep'] + self._extra

+ 23
- 7
aeromancer/cli/run.py View File

@@ -10,6 +10,23 @@ from aeromancer import project_filter
10 10
 from cliff.command import Command
11 11
 
12 12
 
13
+class 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
+
13 30
 class ProjectShellCommandBase(Command):
14 31
     """Run a command for each project"""
15 32
 
@@ -27,7 +44,7 @@ class ProjectShellCommandBase(Command):
27 44
             help=('separator between project name and command output, '
28 45
                   'defaults to %(default)r'),
29 46
         )
30
-        return parser
47
+        return ArgumentParserWrapper(parser)
31 48
 
32 49
     def _show_text_output(self, parsed_args, project, out):
33 50
         for line in out.decode('utf-8').splitlines():
@@ -39,7 +56,10 @@ class ProjectShellCommandBase(Command):
39 56
     def _show_output(self, parsed_args, proj_obj, out, err):
40 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 63
         session = self.app.get_db_session()
44 64
         pm = project.ProjectManager(session)
45 65
         prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
@@ -58,11 +78,7 @@ class Run(ProjectShellCommandBase):
58 78
 
59 79
     def get_parser(self, prog_name):
60 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 81
         return parser
66 82
 
67 83
     def _get_command(self, parsed_args):
68
-        return shlex.shlex(parsed_args.command)
84
+        return self._extra

Loading…
Cancel
Save