[打不过就加入]基于ChatGPT的bash命令转Powershell工具

448 阅读2分钟

背景

ChatGPT 绝对是一个可以改变当前编程范式的强大工具。只要你想,它就可以扩展到各种丰富的应用场景。然而,我注意到大多数应用场景仍然局限于聊天机器人等。在这种情况下,我想提出一个新的思路,并实现了一个叫b2p的开源库,以证明它的潜力。

Powershell是Windows平台上的现代命令行工具了,但其原生命令是非常难记的。事实上,我也只想记Linux的命令,不想记powershell命令。

这是两个平台命令对比:

Linux bash CommandWindows PowerShell Command
lsGet-ChildItem
cdSet-Location
pwdGet-Location
cpCopy-Item
mvMove-Item
rmRemove-Item
mkdirNew-Item (directory)
rmdirRemove-Item (directory)
echoWrite-Output

明显,在选择lsGet-ChildItem命令时,我会选择前者。即使使用了alias,也不是一种很舒适的方法。但现在有了ChatGPT,只需要设计适当的提示词,就可以在这两个命令之间直接转换。

工具链

  • TypeScript
  • Node.js
  • Webpack
  • pkg

设计propot

You are now playing the role of a command-line instruction translation tool, I will input Linux system supported instructions, and you translate them into the corresponding PowerShell instructions. For example, if I input ls, you answer dir. Your answer only needs to contain the translated instruction itself.

调用chatgpt接口

通过调用chatgpt的接口,将这个提示词传入,这样后续输入对应的bash命令,就可以得到powershell命令的结果了。

使用方法

从 发布页面下载应用程序,并将 b2p.exe 复制到系统盘的 Windows 文件夹中,

去 OpenAI 官方网站以获取 API 密钥。然后,在用户路径下%USERPROFILE%\.psgpt这一 JSON 文件中设置 API 密钥。

{"apiKey":"your api key"}

然后直接在命令行使用即可。

b2p ls *.jpg

如果在命令中存在需要转义的字符,则可以使用 包装该命令。例如,需要转义的字符,如 '|',如下:

b2p 'ls *.jpg | wc'

效果

usage.png

后续计划

  1. 把配置项改成自然语言输入,由GPT解析并执行。
  2. 如有需要,添加代理支持。
  3. 优化打包问题。

GitHub repo

github.com/rximg/b2p

zhuanlan.zhihu.com/p/604016156