Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if input @Parameters is empty and interactive tty #1612

Closed
heuermh opened this issue Feb 26, 2022 · 6 comments
Closed

Warn if input @Parameters is empty and interactive tty #1612

heuermh opened this issue Feb 26, 2022 · 6 comments

Comments

@heuermh
Copy link
Contributor

heuermh commented Feb 26, 2022

I am thinking on how best to implement this recommendation:

If your command is expecting to have something piped to it and stdin is an interactive terminal, display help immediately and quit. This means it doesn’t just hang, like cat. Alternatively, you could print a log message to stderr.

https://clig.dev/

Is @IExecutionStrategy the right extension point for this?

Thank you in advance!

@remkop
Copy link
Owner

remkop commented Feb 27, 2022

I would put that logic in the call or run method of the command. Is there any reason not to put it there?

@remkop
Copy link
Owner

remkop commented Feb 28, 2022

@heuermh Perhaps I misunderstood your question...
If you are looking for how to detect whether the current process is connected to a TTY or not, this is not easy to do in pure Java.
The closest thing I have been able to find is invoke System.console() and see if it returns a non-null object. (See also https://picocli.info/#_forcing_ansi_onoff)

If you need more fine-grained information, you can use the Jansi library; it has an isTTY method, I believe.

You would call that method and, if the process is not connected to a TTY, then do not start reading from STDIN.
Another additional idea, if your application really cannot afford to hang forever, is to start reading from STDIN in a separate thread, and wait in the main thread for some timeout threshold (like 60 seconds), and then abort if no data has been entered before that timeout.

As for the place to put this logic, I would put it in the business logic of the application, so in the run/call method.
I usually only create a separate IExecutionStrategy if all commands need to do some shared initialization (like initializing logging or something).

Hope that is useful.

@heuermh
Copy link
Contributor Author

heuermh commented Feb 28, 2022

Thank you for the feedback!

Yes, I was looking into calcTTY(), isTTY(), isPseudoTTY(), etc. methods and noticed it is not exactly easy to determine.

My thinking was that checking if @Parameters was empty and in an interactive tty was more a command line parameter validation & warning rather than a business logic consideration. I'll code it up both ways and see what feels better.

@remkop
Copy link
Owner

remkop commented Feb 28, 2022

Sounds great, go for it!

@remkop
Copy link
Owner

remkop commented Mar 2, 2022

@heuermh Are you okay to close this ticket?
(You can always open another one if you have follow-up questions.)

@heuermh
Copy link
Contributor Author

heuermh commented Mar 2, 2022

Please do, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants