工具简介
https://github.com/nvbn/thefuck/
工具使用
工具安装
-
安装要求
-
python (3.5+) -
pip -
python-dev
-
安装命令
pip install thefuck
-
工具配置
alias fuck='eval $(thefuck $(fc -ln -1)); history -r'
工具原理
import re
from thefuck.utils import for_app
from thefuck.specific.sudo import sudo_support
from thefuck.shells import shell
@sudo_support
@for_app('cd')
defmatch(command):
return (
command.script.startswith('cd ') andany((
'no such file or directory'in command.output.lower(),
'cd: can't cd to'in command.output.lower(),
'does not exist'in command.output.lower()
)))
@sudo_support
defget_new_command(command):
repl = shell.and_('mkdir -p \1', 'cd \1')
return re.sub(r'^cd (.*)', repl, command.script)
自定义规则
def match(command, settings):
return ('permission denied'in command.stderr.lower()
or'EACCES'in command.stderr)
# 获取 stderr 中的字符串和程序返回值(errno),
# 修正规则(命令前添加 sudo)
def get_new_command(command, settings):
return'sudo {}'.format(command.script)
# 以下为可选项
# 默认是否开启
enabled_by_default =True
# 附加命令
def side_effect(command, settings):
subprocess.call('chmod 777 .', shell=True)
# 优先级,数字越大优先级越低。
priority =1000
结合AI更好玩
原文始发于微信公众号(安全有术):一个好玩的终端命令自动纠错工具
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论