Yet another CLI app builder with commands, based on documentation.
Clone or download
Pull request Compare This branch is even with motemen:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
_example
gen
.travis.yml
LICENSE
README.adoc
cli.go
cli_example_test.go
cli_test.go

README.adoc

go-cli Build Status GoDoc Test Coverage

Yet another CLI app builder with commands, based on documentation.

Example

package main

import (
    "flag"
    "os"

    "github.com/motemen/go-cli"
)

func main() {
    cli.Use(&cli.Command{
        Name:  "foo",
        Short: "description in one line",
        Long: `foo [-v] <arg>

Description in paragraphs, starting with a usage line.
Blah blah blah`,
        Action: func(flags *flag.FlagSet, args []string) error {
            // Initialize and parse flags inside Action
            verbose := flags.Bool("v", false, "set verbosity")
            flags.Parse(args)

            args = flags.Args()
            if len(args) < 1 {
                // Return cli.ErrUsage to show the command usage to the user
                return cli.ErrUsage
            }

            ...

            return nil
        },
    })
    cli.Run(os.Args[1:])
}

Example output:

% prog
Usage: prog <command> [<args>]

Commands:
    foo    description in one line
% prog foo -h
Usage: foo [-v] <arg>

Description in paragraphs, starting with a usage line.
Blah blah blah

Options:
  -v=false: set verbosity

Registering commands using documentation

You may use github.com/motemen/go-cli/gen to automatically register commands and their usages using comment documentation. An example documentation for the example above:

// +command foo - description in one line
//
//   foo [-v] <arg>
//
// Description in paragraphs after a usage line.
// Blah blah blah
func actionFoo(flags *flag.FlagSet, args []string) error {
    ...
}

You can use gen.Generate() to generate a source file like below:

// auto-generated file

package main

import "github.com/motemen/go-cli"

func init() {
    cli.Use(
        &cli.Command{
            Name:   "foo",
            ...
        },
    )
}

Include this file to the build and you can maintain CLI commands using documentation. For complete example, refer to the _example directory.

Author

motemen <motemen@gmail.com>