diff --git a/thefuck/argument_parser.py b/thefuck/argument_parser.py index 583d929a..8776dd52 100644 --- a/thefuck/argument_parser.py +++ b/thefuck/argument_parser.py @@ -5,8 +5,17 @@ from .utils import get_alias class Parser(object): + """Argument parser that can handle arguments with our special + placeholder. + + """ + def __init__(self): self._parser = ArgumentParser(prog='thefuck', add_help=False) + self._add_arguments() + + def _add_arguments(self): + """Adds arguments to parser.""" self._parser.add_argument( '-v', '--version', action='store_true', @@ -20,15 +29,7 @@ class Parser(object): '-h', '--help', action='store_true', help='show this help message and exit') - group = self._parser.add_mutually_exclusive_group() - group.add_argument( - '-y', '--yes', - action='store_true', - help='execute fixed command without confirmation') - group.add_argument( - '-r', '--repeat', - action='store_true', - help='repeat on failure') + self._add_conflicting_arguments() self._parser.add_argument( '-d', '--debug', action='store_true', @@ -42,7 +43,28 @@ class Parser(object): nargs='*', help='command that should be fixed') - def _get_arguments(self, argv): + def _add_conflicting_arguments(self): + """It's too dangerous to use `-y` and `-r` together.""" + group = self._parser.add_mutually_exclusive_group() + group.add_argument( + '-y', '--yes', + action='store_true', + help='execute fixed command without confirmation') + group.add_argument( + '-r', '--repeat', + action='store_true', + help='repeat on failure') + + def _prepare_arguments(self, argv): + """Prepares arguments by: + + - removing placeholder and moving arguments after it to beginning, + we need this to distinguish arguments from `command` with ours; + + - adding `--` before `command`, so our parse would ignore arguments + of `command`. + + """ if ARGUMENT_PLACEHOLDER in argv: index = argv.index(ARGUMENT_PLACEHOLDER) return argv[index + 1:] + ['--'] + argv[:index] @@ -52,7 +74,7 @@ class Parser(object): return argv def parse(self, argv): - arguments = self._get_arguments(argv[1:]) + arguments = self._prepare_arguments(argv[1:]) return self._parser.parse_args(arguments) def print_usage(self):