CLI: implement something like git commit (open a text editor and get value)

微笑、不失礼 提交于 2020-12-30 06:34:06

问题


I am making a command line app. I want to allow the user, when they are adding values for things in the terminal, to be able to add the value with their text editor. How does git achieve this?

I want to open their default editor, allow them to enter text, and when they save get the value and use that value for my app.

Thanks.


回答1:


Typically, you do four things:

  1. Create a temporary file
  2. Fork an external process which execs the program specified in the environment variable $EDITOR, giving the name of the temporary file as an argument.
  3. Wait for the process to return.
  4. Open and read from the temporary file to see what the user wrote.


来源:https://stackoverflow.com/questions/19456184/cli-implement-something-like-git-commit-open-a-text-editor-and-get-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!