问题
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:
- Create a temporary file
- Fork an external process which execs the program specified in the environment variable
$EDITOR
, giving the name of the temporary file as an argument. - Wait for the process to return.
- 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