TheFuck — 實(shí)用的命令自動(dòng)糾正工具
The Fuck 是一款功能強(qiáng)大的、Python編寫(xiě)的應(yīng)用程序,可用于糾正控制臺(tái)命令中的錯(cuò)誤,非常強(qiáng)大。此外,用戶還可通過(guò)寫(xiě)Python代碼的方式自定義修復(fù)規(guī)則。
修復(fù)效果如下動(dòng)圖所示:

更多示例如:
自動(dòng)識(shí)別沒(méi)有權(quán)限,在命令前面添加 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
...識(shí)別到?jīng)]有推送到遠(yuǎn)程分支,自動(dòng)追加:
? 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.
...識(shí)別到拼寫(xiě)錯(cuò)誤:
? 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)
...如果你不擔(dān)心fuck修正的結(jié)果是錯(cuò)誤的,你可以禁用require_confirmation?選項(xiàng),讓fuck自動(dòng)運(yùn)行更正的命令:
? 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
...在開(kāi)發(fā)機(jī)上可以這么做,在生產(chǎn)機(jī)器上最好是謹(jǐn)慎一點(diǎn),不推薦這么做。
1.安裝
在OS X上,可以通過(guò)Homebrew(或在Linux上通過(guò)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在其他系統(tǒng)上,?使用pip安裝The Fuck:
pip install?thefuck2.配置
接下來(lái)需要把這個(gè)命令寫(xiě)入到啟動(dòng)腳本中,根據(jù)你的終端類(lèi)型,運(yùn)行相應(yīng)的命令即可:
Bash
chcp.com?65001?
eval?"$(thefuck --alias)"其中 chcp.com 65001 只有在windows環(huán)境下才需要運(yùn)行。
Zsh:
eval?"$(thefuck --alias)"其他的可見(jiàn):
https://github.com/nvbn/thefuck/wiki/Shell-aliases
3.原理
其實(shí)TheFuck的原理就是規(guī)則匹配(正則表達(dá)式),如果找到匹配規(guī)則的命令,則創(chuàng)建一個(gè)命令給用戶選擇或直接運(yùn)行。
默認(rèn)情況下的規(guī)則有:
cat_dir?- 當(dāng)你嘗試cat目錄的時(shí)候,用ls替換cat;cd_correction?– 拼寫(xiě)檢查和糾正失敗的cd命令;cd_mkdir?– 在進(jìn)入目錄之前創(chuàng)建目錄;cd_parent?– 更改?cd..?為cd ..;dry?– 修復(fù)類(lèi)似的重復(fù)問(wèn)題:git git push;fix_alt_space?– 用空格字符代替Alt + Space;
等等,具體可以在官方文檔中找到:
https://github.com/nvbn/thefuck
4. 創(chuàng)建自己的修復(fù)規(guī)則
要添加自己的規(guī)則,在 ~/.config/thefuck/rules?文件夾中,
創(chuàng)建一個(gè)文件名為 your-rule-name.py?的規(guī)則文件,其中必須包含兩個(gè)函數(shù):
match(command: Command) -> bool
get_new_command(command: Command) -> str?| list[str]下面是簡(jiǎn)單的 sudo 規(guī)則示例:
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如果命令運(yùn)行結(jié)果出現(xiàn) permission denied 或者 EACCES,則執(zhí)行 sudo xxx.
此外,還可以配置side_effect,如果你配置了enabled_by_default = True,side_effect函數(shù)內(nèi)的操作將會(huì)被執(zhí)行,本例中是對(duì)當(dāng)前目錄下的文件夾執(zhí)行賦權(quán)操作: chmod 777 .
大家可以動(dòng)手試試自己配一個(gè)修復(fù)命令,還是相當(dāng)有意思的。
我們的文章到此就結(jié)束啦,如果你喜歡今天的Python 實(shí)戰(zhàn)教程,請(qǐng)持續(xù)關(guān)注Python實(shí)用寶典。
有任何問(wèn)題,可以在公眾號(hào)后臺(tái)回復(fù):加群,回答相應(yīng)紅字驗(yàn)證信息,進(jìn)入互助群詢問(wèn)。
原創(chuàng)不易,希望你能在下面點(diǎn)個(gè)贊和在看支持我繼續(xù)創(chuàng)作,謝謝!
點(diǎn)擊下方閱讀原文可獲得更好的閱讀體驗(yàn)
Python實(shí)用寶典?(pythondict.com)
不只是一個(gè)寶典
歡迎關(guān)注公眾號(hào):Python實(shí)用寶典
