Merge branch '614-repeate-option'
This commit is contained in:
+42
-10
@@ -1,12 +1,21 @@
|
||||
import sys
|
||||
from argparse import ArgumentParser
|
||||
from argparse import ArgumentParser, SUPPRESS
|
||||
from .const import ARGUMENT_PLACEHOLDER
|
||||
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,19 +29,42 @@ class Parser(object):
|
||||
'-h', '--help',
|
||||
action='store_true',
|
||||
help='show this help message and exit')
|
||||
self._parser.add_argument(
|
||||
'-y', '--yes',
|
||||
action='store_true',
|
||||
help='execute fixed command without confirmation')
|
||||
self._add_conflicting_arguments()
|
||||
self._parser.add_argument(
|
||||
'-d', '--debug',
|
||||
action='store_true',
|
||||
help='enable debug output')
|
||||
self._parser.add_argument('command',
|
||||
nargs='*',
|
||||
help='command that should be fixed')
|
||||
self._parser.add_argument(
|
||||
'--force-command',
|
||||
action='store',
|
||||
help=SUPPRESS)
|
||||
self._parser.add_argument(
|
||||
'command',
|
||||
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]
|
||||
@@ -42,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):
|
||||
|
||||
@@ -121,6 +121,8 @@ class Settings(dict):
|
||||
from_args['require_confirmation'] = not args.yes
|
||||
if args.debug:
|
||||
from_args['debug'] = args.debug
|
||||
if args.repeat:
|
||||
from_args['repeat'] = args.repeat
|
||||
return from_args
|
||||
|
||||
|
||||
|
||||
+3
-1
@@ -34,6 +34,7 @@ DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||
'wait_slow_command': 15,
|
||||
'slow_commands': ['lein', 'react-native', 'gradle',
|
||||
'./gradlew', 'vagrant'],
|
||||
'repeat': False,
|
||||
'env': {'LC_ALL': 'C', 'LANG': 'C', 'GIT_TRACE': '1'}}
|
||||
|
||||
ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
@@ -46,7 +47,8 @@ ENV_TO_ATTR = {'THEFUCK_RULES': 'rules',
|
||||
'THEFUCK_HISTORY_LIMIT': 'history_limit',
|
||||
'THEFUCK_ALTER_HISTORY': 'alter_history',
|
||||
'THEFUCK_WAIT_SLOW_COMMAND': 'wait_slow_command',
|
||||
'THEFUCK_SLOW_COMMANDS': 'slow_commands'}
|
||||
'THEFUCK_SLOW_COMMANDS': 'slow_commands',
|
||||
'THEFUCK_REPEAT': 'repeat'}
|
||||
|
||||
SETTINGS_HEADER = u"""# The Fuck settings file
|
||||
#
|
||||
|
||||
+3
-1
@@ -20,9 +20,11 @@ def fix_command(known_args):
|
||||
settings.init(known_args)
|
||||
with logs.debug_time('Total'):
|
||||
logs.debug(u'Run with settings: {}'.format(pformat(settings)))
|
||||
raw_command = ([known_args.force_command] if known_args.force_command
|
||||
else known_args.command)
|
||||
|
||||
try:
|
||||
command = types.Command.from_raw_script(known_args.command)
|
||||
command = types.Command.from_raw_script(raw_command)
|
||||
except EmptyCommand:
|
||||
logs.debug('Empty command, nothing to do')
|
||||
return
|
||||
|
||||
@@ -66,6 +66,9 @@ class Fish(Generic):
|
||||
def and_(self, *commands):
|
||||
return u'; and '.join(commands)
|
||||
|
||||
def or_(self, *commands):
|
||||
return u'; or '.join(commands)
|
||||
|
||||
def how_to_configure(self):
|
||||
return self._create_shell_configuration(
|
||||
content=u"eval (thefuck --alias | tr '\n' ';')",
|
||||
|
||||
@@ -66,6 +66,9 @@ class Generic(object):
|
||||
def and_(self, *commands):
|
||||
return u' && '.join(commands)
|
||||
|
||||
def or_(self, *commands):
|
||||
return u' || '.join(commands)
|
||||
|
||||
def how_to_configure(self):
|
||||
return
|
||||
|
||||
|
||||
+19
-1
@@ -9,6 +9,7 @@ from .shells import shell
|
||||
from .conf import settings
|
||||
from .const import DEFAULT_PRIORITY, ALL_ENABLED
|
||||
from .exceptions import EmptyCommand
|
||||
from .utils import get_alias
|
||||
|
||||
|
||||
class Command(object):
|
||||
@@ -276,6 +277,22 @@ class CorrectedCommand(object):
|
||||
return u'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
|
||||
self.script, self.side_effect, self.priority)
|
||||
|
||||
def _get_script(self):
|
||||
"""Returns fixed commands script.
|
||||
|
||||
If `settings.repeat` is `True`, appends command with second attempt
|
||||
of running fuck in case fixed command fails again.
|
||||
|
||||
"""
|
||||
if settings.repeat:
|
||||
repeat_fuck = '{} --repeat {}--force-command {}'.format(
|
||||
get_alias(),
|
||||
'--debug ' if settings.debug else '',
|
||||
shell.quote(self.script))
|
||||
return shell.or_(self.script, repeat_fuck)
|
||||
else:
|
||||
return self.script
|
||||
|
||||
def run(self, old_cmd):
|
||||
"""Runs command from rule for passed command.
|
||||
|
||||
@@ -289,4 +306,5 @@ class CorrectedCommand(object):
|
||||
# This depends on correct setting of PYTHONIOENCODING by the alias:
|
||||
logs.debug(u'PYTHONIOENCODING: {}'.format(
|
||||
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
|
||||
print(self.script)
|
||||
|
||||
print(self._get_script())
|
||||
|
||||
Reference in New Issue
Block a user