Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/motemen/go-cli
Yet another CLI app builder with commands, based on documentation.
https://github.com/motemen/go-cli
cli command-line go
Last synced: 3 months ago
JSON representation
Yet another CLI app builder with commands, based on documentation.
- Host: GitHub
- URL: https://github.com/motemen/go-cli
- Owner: motemen
- License: mit
- Created: 2015-03-21T15:26:34.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-18T15:48:32.000Z (almost 7 years ago)
- Last Synced: 2024-06-19T01:45:05.437Z (7 months ago)
- Topics: cli, command-line, go
- Language: Go
- Size: 19.5 KB
- Stars: 30
- Watchers: 5
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.adoc
- License: LICENSE
Awesome Lists containing this project
README
= go-cli image:https://travis-ci.org/motemen/go-cli.svg?branch=master["Build Status", link="https://travis-ci.org/motemen/go-cli"] image:http://godoc.org/github.com/motemen/go-cli?status.svg["GoDoc", link="http://godoc.org/github.com/motemen/go-cli"] image:http://gocover.io/_badge/github.com/motemen/go-cli["Test Coverage", link="http://gocover.io/github.com/motemen/go-cli"]
Yet another CLI app builder with commands, based on documentation.
== Example
[source,go]
----
package mainimport (
"flag"
"os""github.com/motemen/go-cli"
)func main() {
cli.Use(&cli.Command{
Name: "foo",
Short: "description in one line",
Long: `foo [-v]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 []Commands:
foo description in one line
--------
% prog foo -h
Usage: foo [-v]Description in paragraphs, starting with a usage line.
Blah blah blahOptions:
-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:[source,go]
----// +command foo - description in one line
//
// foo [-v]
//
// 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:
[source,go]
----
// auto-generated filepackage 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