Is there a way in the console application to determine when StandardInput is available?

I want to create a console application that can stop and request some input, in some conditions. But I want to request only if input will be available.

Is there a way to find out if Stdin is related to anything?

I think powershell does this, and I would like to do something like this. Powershell detects when it starts interactively, and asks when it is true. I think it is also possible to force powershell into silent mode. (Correct me if I am wrong)

I think, I think there are several possibilities, including but not limited to:

  • the program runs interactively on the cmd.exe command line. In this case, request input.
  • the program runs in a script, and something was sent to stdin. In this case, there is no invitation.
  • the program runs in a script that runs at cmd.exe command line. In this case, tell me.
  • the program starts from another program, and nothing was passed to stdin. In this case, there is no invitation.

Is it possible for a console application to distinguish between these situations and change behavior accordingly?

+3
source share
2 answers

Check out the GetStdHandle API. The call GetStdHandle(STD_INPUT_HANDLE)returns NULL if the associated stdin does not exist for the application.

+3
source

stdin, - (, --noprompt - ) .

+1

Source: https://habr.com/ru/post/1713787/


All Articles