首頁 > 後端開發 > Golang > 主體

使用promptui 在 Go 中嵌套提示

王林
發布: 2024-07-17 20:22:42
原創
1077 人瀏覽過

我正在開發一個用 Go 編寫的 CLI 工具,最近使用了 Cobra 工具,我有一個用例,我需要其中一個命令的巢狀提示。我使用 Promptui 作為提示,但找不到直接的方法來執行此操作。這篇短文將展示如何使用promptui 建立嵌套提示。完整的程式碼可以在這裡找到。

我們首先需要建立一個空的 Go 專案。我們稱之為嵌套提示:

$ mkdir nested-prompt && cd nested-prompt
$ go mod init github.com/Thwani47/nested-prompt 
登入後複製

然後我們將安裝 cobra、cobra-cli 和promptui 軟體包:

$ go get -u github.com/spf13/cobra@latest
$ go install github.com/spf13/cobra-cli@latest 
$ go get -u github.com/manifoldco/promptui
登入後複製

我們可以使用 cobra-cli 初始化一個新的 CLI 應用程式並向我們的 CLI 新增命令

$ cobra-cli init            # initializes a new CLI application
$ cobra-cli add config      # adds a new command to the CLI named 'config'
登入後複製

我們可以清理cmd/config.go檔案並刪除所有註解。應該是這樣的:

// cmd/config.go
package cmd

import (
    "fmt"

    "github.com/spf13/cobra"
)

var configCmd = &cobra.Command{
    Use:   "config",
    Short: "Configure settings for the application",
    Long: `Configure settings for the application`,
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("config called")
    },
}

func init() {
    rootCmd.AddCommand(configCmd)
}

登入後複製

我們首先需要為我們的提示建立一個自訂類型。我們透過定義一個提示項目結構來做到這一點,如下

type PromptType int

const (
    TextPrompt     PromptType = 0
    PasswordPrompt PromptType = 1
    SelectPrompt   PromptType = 2
)

type promptItem struct {
    ID            string
    Label         string
    Value         string
    SelectOptions []string
    promptType    PromptType
}
登入後複製

PromptType 枚舉讓我們可以從提示中收集不同類型的輸入,我們可以提示使用者輸入文字或敏感值(例如密碼或 API 金鑰),或提示使用者從定義值清單中進行選擇

然後我們定義一個promptInput函數來提示使用者輸入。此函數傳回使用者輸入的字串值,如果提示失敗則傳回錯誤。

func promptInput(item promptItem) (string, error) {
    prompt := promptui.Prompt{
        Label:       item.Label,
        HideEntered: true,
    }

    if item.promptType == PasswordPrompt {
        prompt.Mask = '*'
    }

    res, err := prompt.Run()

    if err != nil {
        fmt.Printf("Prompt failed %v\n", err)
        return "", err
    }

    return res, nil
}
登入後複製

然後我們定義一個 PromptSelect 函數,該函數將允許使用者從選項清單中進行選擇。函數傳回使用者選擇的字串值,如果提示失敗則傳回錯誤。

func promptSelect(item selectItem) (string, error) {
    prompt := promptui.Select{
        Label:        item.Label,
        Items:        item.SelectValues,
        HideSelected: true,
    }

    _, result, err := prompt.Run()

    if err != nil {
        fmt.Printf("Prompt failed %v\n", err)
        return "", err
    }

    return result, nil
}
登入後複製

為了模擬巢狀提示,我們將建立一個 PromptNested 函數,該函數將允許我們提示使用者輸入值,並且提示將保持活動狀態,直到使用者選擇「完成」。此函數傳回布林值,表示提示成功。

函數中的註解解釋了每個主要程式碼區塊的職責

func promptNested(promptLabel string, startingIndex int, items []*promptItem) bool {

    // Add a "Done" option to the prompt if it does not exist
    doneID := "Done"
    if len(items) > 0 && items[0].ID != doneID {
        items = append([]*promptItem{{ID: doneID, Label: "Done"}}, items...)
    }

    templates := &promptui.SelectTemplates{
        Label:    "{{ . }}?",
        Active:   "\U0001F336 {{ .Label | cyan }}",
        Inactive: "{{ .Label | cyan }}",
        Selected: "\U0001F336 {{ .Label | red  | cyan }}",
    }

    prompt := promptui.Select{
        Label:        promptLabel,
        Items:        items,
        Templates:    templates,
        Size:         3,
        HideSelected: true,
        CursorPos:    startingIndex, // Set the cursor to the last selected item
    }

    idx, _, err := prompt.Run()

    if err != nil {
        fmt.Printf("Error occurred when running prompt: %v\n", err)
        return false
    }

    selectedItem := items[idx]

    // if the user selects "Done", return true and exit from the function
    if selectedItem.ID == doneID {
        return true
    }

    var promptResponse string

    // if the prompt type is Text or Password, prompt the user for input
    if selectedItem.promptType == TextPrompt || selectedItem.promptType == PasswordPrompt {
        promptResponse, err = promptInput(*selectedItem)

        if err != nil {
            fmt.Printf("Error occurred when running prompt: %v\n", err)
            return false
        }

        items[idx].Value = promptResponse

    }

    // if the prompt type is Select, prompt the user to select from a list of options
    if selectedItem.promptType == SelectPrompt {
        promptResponse, err = promptSelect(*selectedItem)

        if err != nil {
            fmt.Printf("Error occurred when running prompt: %v\n", err)
            return false
        }
        items[idx].Value = promptResponse
    }

    if err != nil {
        fmt.Printf("Error occurred when running prompt: %v\n", err)
        return false
    }

    // recursively call the promptNested function to allow the user to select another option
    return promptNested(idx, items)
}
登入後複製

現在我們擁有了所需的所有方法,我們需要測試它們。在configCmd指令的Run函數中,我們將建立一個promptItem列表並呼叫promptNested函數來提示使用者輸入。 Run 函數應如下所示:

// create a list of prompt items
items := []*promptItem{
    {
        ID:         "APIKey",
        Label:      "API Key",
        promptType: PasswordPrompt,
    },
    {
        ID:            "Theme",
        Label:         "Theme",
        promptType:    SelectPrompt,
        SelectOptions: []string{"Dark", "Light"},
    },
    {
        ID:            "Language",
        Label:         "Preferred Language",
        promptType:    SelectPrompt,
        SelectOptions: []string{"English", "Spanish", "French", "German", "Chinese", "Japanese"},
    },
}

// set the starting index to 0 to start at the first item in the list
promptNested("Configuration Items", 0, items)

for _, v := range items {
    fmt.Printf("Saving configuration (%s) with value (%s)...\n", v.ID, v.Value)
}
登入後複製

如下建置並測試應用程式

$ go build . 
$ ./nested-prompt config
登入後複製

結果如下
Nested Prompts in Go using promptui

以上是使用promptui 在 Go 中嵌套提示的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!