mirror of
https://github.com/nvbn/thefuck.git
synced 2025-01-18 20:11:17 +00:00
#614: Refine argument_parser
This commit is contained in:
parent
cfa831c88d
commit
b36cf59b46
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user