从Golang的管道输入中阅读后提示用户

发布于 2025-02-12 09:21:25 字数 1815 浏览 1 评论 0原文

我会从管道输入中阅读一些麻烦,然后提示用户在此之后提出其他问题。这可以做吗?

我在此处创建了该程序的简化版本。

我希望用户能够将输入输入到程序中,而不是被提示。 但是,如果他们在我编写的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2025-02-19 09:21:25

看起来您要做的事情不支持调查

https://github.com/alecaub.com/alecaivazisis /调查#常见问题解答

调查旨在支持大多数终端模拟器;它预计支持ANSI逃生序列。这意味着不支持从管道的stdin或写作到管道的Stdout的阅读,并且在这些情况下可能会破坏您的应用程序。参见 337

从代码的外观中,似乎没有就像您只是在提示用户输入时确实需要调查。最好自己实施提示。

Looks like what you're trying to do isn't supported with Survey

https://github.com/AlecAivazis/survey#faq

survey aims to support most terminal emulators; it expects support for ANSI escape sequences. This means that reading from piped stdin or writing to piped stdout is not supported, and likely to break your application in these situations. See 337

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文