从Golang的管道输入中阅读后提示用户
我会从管道输入中阅读一些麻烦,然后提示用户在此之后提出其他问题。这可以做吗?
我在此处创建了该程序的简化版本。
我希望用户能够将输入输入到程序中,而不是被提示。 但是,如果他们在我编写的CLI程序中没有提供特定的标志,我仍然想提示他们第二个问题。
为避免任何提示,用户可以通过-y
(在下面的程序中未显示)以及文本输入中的管道传递以跳过“您确定吗?”迅速的。
package main
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
)
func main() {
value := ""
isPiped, err := isPipedInput()
if err != nil {
panic(err)
}
if isPiped {
_, err = fmt.Scanf("%s", &value)
if err != nil {
panic(err)
}
if value == "" {
panic(fmt.Errorf("must be greater than 0"))
}
} else {
err = survey.AskOne(&survey.Input{Message: "Enter value followed by [Enter]:"}, &value)
if err != nil {
panic(err)
}
if value == "" {
panic(fmt.Errorf("must be greater than 0"))
}
}
shouldProceed := false
err = survey.AskOne(&survey.Confirm{Message: "Are you sure?"}, &shouldProceed)
if err != nil {
panic(err)
}
if shouldProceed {
fmt.Println("Proceeding!", value)
} else {
fmt.Println("Not proceeding...", value)
}
}
func isPipedInput() (bool, error) {
fi, err := os.Stdin.Stat()
if err != nil {
return false, err
}
return (fi.Mode() & os.ModeCharDevice) == 0, nil
}
构建:
go build -o main
第一个按预期工作的用例:
./main
? Enter value followed by [Enter]: foo
? Are you sure? Yes
Proceeding! foo
第二种用例不起作用:
echo "foo" | ./main
? Are you sure? (y/N) ^[[23;256Rpanic: EOF
goroutine 1 [running]:
main.main()
/main.go:40 +0x2b0
^[[8;23R%
I'm having some troubles reading from piped input, and then prompting a user for a further question afterwards. Is this possible to do?
I've created a simplified version of the program here.
I want the user to be able to pipe input in to the program instead of being prompted for it.
However, I still want to prompt them with a second question if they don't provide a specific flag in a CLI program I'm writing.
To avoid any prompts, the user could pass in -y
(not shown in the program below) along with piping in the text input to skip the "are you sure?" prompt.
package main
import (
"fmt"
"os"
"github.com/AlecAivazis/survey/v2"
)
func main() {
value := ""
isPiped, err := isPipedInput()
if err != nil {
panic(err)
}
if isPiped {
_, err = fmt.Scanf("%s", &value)
if err != nil {
panic(err)
}
if value == "" {
panic(fmt.Errorf("must be greater than 0"))
}
} else {
err = survey.AskOne(&survey.Input{Message: "Enter value followed by [Enter]:"}, &value)
if err != nil {
panic(err)
}
if value == "" {
panic(fmt.Errorf("must be greater than 0"))
}
}
shouldProceed := false
err = survey.AskOne(&survey.Confirm{Message: "Are you sure?"}, &shouldProceed)
if err != nil {
panic(err)
}
if shouldProceed {
fmt.Println("Proceeding!", value)
} else {
fmt.Println("Not proceeding...", value)
}
}
func isPipedInput() (bool, error) {
fi, err := os.Stdin.Stat()
if err != nil {
return false, err
}
return (fi.Mode() & os.ModeCharDevice) == 0, nil
}
To build:
go build -o main
First use-case that works as expected:
./main
? Enter value followed by [Enter]: foo
? Are you sure? Yes
Proceeding! foo
Second use case that does not work:
echo "foo" | ./main
? Are you sure? (y/N) ^[[23;256Rpanic: EOF
goroutine 1 [running]:
main.main()
/main.go:40 +0x2b0
^[[8;23R%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您要做的事情不支持调查
https://github.com/alecaub.com/alecaivazisis /调查#常见问题解答
从代码的外观中,似乎没有就像您只是在提示用户输入时确实需要调查。最好自己实施提示。
Looks like what you're trying to do isn't supported with Survey
https://github.com/AlecAivazis/survey#faq
From the looks of your code, it doesn't seem like you really need Survey if you're just prompting the user for input. Might be best to implement the prompt yourself.