简体   繁体   中英

Bash autocomplete using codegangsta cli framework [https://github.com/codegangsta/cli]

I am a newbie to golang and i am using the code gangsta cli framework [ https://github.com/codegangsta/cli] to develop a command line application. I am trying to implement the autocomplete feature for the flags of the commands, but looks like it is not working as expected. Has anybody tried to implement this feature using this framework?

Here is my part of the code:

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"))
}

Here is the expected output:

./greet read tweets --a [TAB][TAB] does not work.

Enabling bash completion in the Go code is just one step out of two.

You need also to download and source this script. After downloading, just run:

source bash_autocomplete

To make it permanent, add the above command with full path to your ~/.bashrc or ~/.bash_profile files.

我将在自述文件的这一部分阅读有关cli repo的内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM