使用codegangsta-cli框架自动完成Bash[https://github.com/codegangsta/c



我是golang的新手,我正在使用代码gangsta-cli框架[https://github.com/codegangsta/cli]开发命令行应用程序。我正在尝试为命令的标志实现自动完成功能,但它似乎没有按预期工作。有人尝试过使用这个框架来实现这个功能吗?

这是我的代码部分:

package main
import (
    "fmt"
    "os"
    "github.com/codegangsta/cli"
)
func main() {
    app := cli.NewApp()
    app.Name = "greet"
    app.Usage = "sample command-line app by greet"
    app.Author = "abc"
    app.Email = "xyz@aaa.com"
    app.EnableBashCompletion = true
    app.Commands = []cli.Command{
        {
            Name:      "read",
            ShortName: "r",
            Usage:     "read something",
            Subcommands: []cli.Command{
                {
                    Name:   "articles",
                    Usage:  "read articles",
                    Action: readArticles,
                },
                {
                    Name:  "tweets",
                    Usage: "read Tweets",
                    Flags: []cli.Flag{
                        cli.StringFlag{
                            Name:  "account",
                            Value: "SomeThing",
                            Usage: "name of Twitter account",
                        },
                    },
                    Action: readTwitter,
                },
            },
        },
    }
    app.Run(os.Args)
}
func readArticles(ctx *cli.Context) {
    fmt.Println("Go to http://www.google.com to read articles!")
}
func readTwitter(ctx *cli.Context) {
    fmt.Printf("Go to https://twitter.com/%s to read tweets!", ctx.String("account"))
}

以下是预期输出:

/问候阅读推文——[TAB][TAB]不起作用。

在Go代码中启用bash完成只是两步中的一步。

您还需要下载并获取此脚本的源代码。下载后,只需运行:

source bash_autocomplete

要使其永久化,请将以上命令添加到~/.bashrc或~/.bash _profile文件的完整路径中。

我将阅读cli repo自述文件的这一部分。

相关内容

最新更新