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

Always use default value when stdin is pipe. #6

Closed
pyama86 opened this issue Mar 28, 2020 · 2 comments
Closed

Always use default value when stdin is pipe. #6

pyama86 opened this issue Mar 28, 2020 · 2 comments

Comments

@pyama86
Copy link
Contributor

pyama86 commented Mar 28, 2020

Hi! Songmu san!
Yattemasuka???

I have happened that always use default value when stdin is pipe.

package main

import (
	"fmt"

	"github.com/Songmu/prompter"
)

func main() {

	if prompter.YesNo("example", false) {
		fmt.Println("say yes")
	}
}
% go run /tmp/main.go
example (yes/no) [no]: yes
say yes
% yes | go run /tmp/main.go
example (yes/no) [no]: % 

I think that the cause is like the code below.
https://github.com/Songmu/prompter/blob/master/prompter.go#L67

So I want to ask, what is the intention of this code? I want to discuss the amendment policy.

こんにちは!
prompter便利に利用させてもらってます!最高です!

darwinでの利用時にpipeだと常にデフォルトバリューが利用されるようで、yesコマンドが上手くう使えないので、上記の箇所のもとの意図を聞きたいです。

@Songmu
Copy link
Owner

Songmu commented Aug 31, 2020

This is a vexing issue.

I've merged #8 for now.

This is what happens when stdin is not connected to the tty, it is considered as "not interactive" and uses the default value not the piped value. Yes, as you say, I don't also think this is an intuitive behavior.

However, in the first place, I don't think it's a good idea to give the value to prompt via a pipe.

Prompts should only be used to interact with the user, and if you want to give input from stdin, you should take care of that in some other way.

For example, you could do the following

# Passed as a command line argument
echo "hoge" | xargs -I{} yourcmd --input={}

# When a specific environment variable is set, adopt it without prompting.
YOURCMD_INPUT=xxxxx yourcmd

# Provide an option to get the value from the stdin explicitly.
echo "xxxxx" | yourcmd --password-stdin

@pyama86
Copy link
Contributor Author

pyama86 commented Sep 1, 2020

I mostly agree.
I want to test including the prompt operation from user, and in that case, surrogate input using the yes command is useful.

I think it's good to have a dedicated option for testing as suggested, but it can be a bit confusing for normal use.

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

No branches or pull requests

2 participants