Let run and grep take arbitrary arguments

This commit is contained in:
Doug Hellmann 2015-01-21 21:11:10 -05:00
parent 820caf5555
commit 0e15691158
2 changed files with 30 additions and 17 deletions

View File

@ -10,17 +10,14 @@ from aeromancer.cli.run import ProjectShellCommandBase
class Grep(ProjectShellCommandBase):
"""Search the contents of files"""
"""Search the contents of files
Accepts most of the arguments of git-grep, unless they conflict
with other arguments to this command.
"""
log = logging.getLogger(__name__)
def get_parser(self, prog_name):
parser = super(Grep, self).get_parser(prog_name)
parser.add_argument('pattern',
action='store',
help='regular expression',
)
return parser
def _get_command(self, parsed_args):
return ['git', 'grep', parsed_args.pattern]
return ['git', 'grep'] + self._extra

View File

@ -10,6 +10,23 @@ from aeromancer import project_filter
from cliff.command import Command
class ArgumentParserWrapper(object):
"""Wrap a regular argument parser to replace
parse_args with parse_known_args.
Cliff calls parse_args() for subcommands, but we want
parse_known_args() so any extra values that look like command
switches will be ignored.
"""
def __init__(self, parser):
self._parser = parser
def parse_args(self, argv):
return self._parser.parse_known_args(argv)
class ProjectShellCommandBase(Command):
"""Run a command for each project"""
@ -27,7 +44,7 @@ class ProjectShellCommandBase(Command):
help=('separator between project name and command output, '
'defaults to %(default)r'),
)
return parser
return ArgumentParserWrapper(parser)
def _show_text_output(self, parsed_args, project, out):
for line in out.decode('utf-8').splitlines():
@ -39,7 +56,10 @@ class ProjectShellCommandBase(Command):
def _show_output(self, parsed_args, proj_obj, out, err):
self._show_text_output(parsed_args, proj_obj, err or out)
def take_action(self, parsed_args):
def take_action(self, parsed_args_tuple):
# Handle the tuple we'll get from the parser wrapper.
parsed_args, extra = parsed_args_tuple
self._extra = extra
session = self.app.get_db_session()
pm = project.ProjectManager(session)
prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
@ -58,11 +78,7 @@ class Run(ProjectShellCommandBase):
def get_parser(self, prog_name):
parser = super(Run, self).get_parser(prog_name)
parser.add_argument('command',
action='store',
help='the command to run, probably quoted',
)
return parser
def _get_command(self, parsed_args):
return shlex.shlex(parsed_args.command)
return self._extra