TheFuck — 实用的命令自动纠正工具

简说Python

共 3339字,需浏览 7分钟

 ·

2022-04-16 01:15

The Fuck 是一款功能强大的、Python编写的应用程序,可用于纠正控制台命令中的错误,非常强大。此外,用户还可通过写Python代码的方式自定义修复规则。

修复效果如下动图所示:


更多示例如:

自动识别没有权限,在命令前面添加 sudo:

➜ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

➜ fuck
sudo apt-get install vim [enter/↑/↓/ctrl+c]
[sudo] password for nvbn:
Reading package lists... Done
...


识别到没有推送到远程分支,自动追加:

➜ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
 
    git push --set-upstream origin master

➜ fuck
git push --set-upstream origin master [enter/↑/↓/ctrl+c]
Counting objects: 9, done.
...


识别到拼写错误:

➜ puthon
No command 'puthon' found, did you mean:
 Command 'python' from package 'python-minimal' (main)
 Command 'python' from package 'python3' (main)
zsh: command not found: puthon

➜ fuck
python [enter/↑/↓/ctrl+c]
Python 3.4.2 (default, Oct 8 2014, 13:08:17)
...


如果你不担心fuck修正的结果是错误的,你可以禁用require_confirmation 选项,让fuck自动运行更正的命令:


➜ apt-get install vim
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

➜ fuck
sudo apt-get install vim
[sudo] password for nvbn:
Reading package lists... Done
...


在开发机上可以这么做,在生产机器上最好是谨慎一点,不推荐这么做。

1.安装

在OS X上,可以通过Homebrew(或在Linux上通过Linuxbrew)安装The Fuck

brew install thefuck


在Ubuntu / Mint上,使用以下命令安装The Fuck

sudo apt update
sudo apt install python3-dev python3-pip python3-setuptools
sudo pip3 install thefuck


在FreeBSD上,使用以下命令安装The Fuck

pkg install thefuck


在其他系统上, 使用pip安装The Fuck

pip install thefuck

2.配置

接下来需要把这个命令写入到启动脚本中,根据你的终端类型,运行相应的命令即可:

Bash

chcp.com 65001 
eval "$(thefuck --alias)"


其中 chcp.com 65001 只有在windows环境下才需要运行。

Zsh:

eval "$(thefuck --alias)"


其他的可见:

https://github.com/nvbn/thefuck/wiki/Shell-aliases

3.原理

其实TheFuck的原理就是规则匹配(正则表达式),如果找到匹配规则的命令,则创建一个命令给用户选择或直接运行。

默认情况下的规则有:

  • cat_dir - 当你尝试cat目录的时候,用ls替换cat;

  • cd_correction – 拼写检查和纠正失败的cd命令;

  • cd_mkdir – 在进入目录之前创建目录;

  • cd_parent – 更改 cd.. 为cd ..

  • dry – 修复类似的重复问题:git git push

  • fix_alt_space – 用空格字符代替Alt + Space;

等等,具体可以在官方文档中找到:
https://github.com/nvbn/thefuck


4. 创建自己的修复规则

要添加自己的规则,在 ~/.config/thefuck/rules 文件夹中,

创建一个文件名为 your-rule-name.py 的规则文件,其中必须包含两个函数:

match(command: Command) -> bool
get_new_command(command: Command) -> str | list[str]


下面是简单的 sudo 规则示例:

def match(command):
    return ('permission denied' in command.output.lower()
            or 'EACCES' in command.output)


def get_new_command(command):
    return 'sudo {}'.format(command.script)

# Optional:
enabled_by_default = True

def side_effect(command, fixed_command):
    subprocess.call('chmod 777 .', shell=True)

priority = 1000  # Lower first, default is 1000

requires_output = True


如果命令运行结果出现 permission denied 或者 EACCES,则执行 sudo xxx.

此外,还可以配置side_effect,如果你配置了enabled_by_default = True,side_effect函数内的操作将会被执行,本例中是对当前目录下的文件夹执行赋权操作: chmod 777 .

大家可以动手试试自己配一个修复命令,还是相当有意思的。

老表荐书

图书介绍Vue.js全家桶零基础入门到进阶项目实战

Vue.js 是一套构建用户界面的渐进式框架,本书旨在帮助读者全面掌握 Vue.js 全家桶技术和单页面前后端分离项目开发,理解 MVVM 框架思想,让前端和后端开发人员快速精通 Vue.js 全家桶技术。

本书包含的主要技术:NPM/CNPM、VS Code、Vue.js、 MVVM、Axios、Vue Router、webpack、ECMAScript 6、Vue Loader、Vue CLI、Element UI、Vuex、 Mock.js、Easy Mock、ECharts 、Promise、拦截器、组件通信、跨域问题、上线部署等。


【注意⚠️】近三个月内获得赠书的读者将无法再次获得赠书,想要多次获得赠书,可以查看下面的投稿规则及激励。


近期阅读学习推荐

服务器被黑客攻击,用来挖矿!怎么办?

Python超好用的命令行界面实现工具

Python自动化处理Excel表格实战完整代码分享(课表解析)

如何找到我

分享

收藏

点赞

在看

浏览 19
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报