https://github.com/pierrec/cmdflag
Augment the flag package with commands
https://github.com/pierrec/cmdflag
flag flags nested-commands subcommands
Last synced: 5 months ago
JSON representation
Augment the flag package with commands
- Host: GitHub
- URL: https://github.com/pierrec/cmdflag
- Owner: pierrec
- License: bsd-3-clause
- Created: 2019-05-11T18:21:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-06T14:06:05.000Z (about 7 years ago)
- Last Synced: 2025-08-15T12:54:51.396Z (10 months ago)
- Topics: flag, flags, nested-commands, subcommands
- Language: Go
- Size: 37.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cmdflag : command support to the standard library flag package
## Overview [](https://godoc.org/github.com/pierrec/cmdflag) [](https://goreportcard.com/report/github.com/pierrec/cmdflag)
Building on top of the excellent `flag` package from the standard library, `cmdflag` adds a simple way of specifying nested commands. Its intent is to blend with the usage of `flag` by keeping its idioms and simply augment it with commands.
## Install
```
go get github.com/pierrec/cmdflag
```
## Usage
A `Command` is a set of flags (flag.FlagSet) with a (potentially empty) set of other Commands
(sometimes referred to as subcommands). To define a subcommand, `Command.Add` an `Application`
defining its properties:
- Name - the subcommand name
- Descr - a short desciption of the subcommand
- Args - the list of arguments expected by the subcommand
- Help - a long description of the subcommand
- Err - what to do in case of error (same as in the flag package)
- Init - the function to be run once the subcommand is encountered (lazily initialized)
Nested commands are supported, so a subcommand can also have its own subcommands.
## Example
```
./chef cook -toppings cheese,mushrooms pizza
c := cmdflag.New(nil)
c.Add(cmdflag.Application{
Name: "cook",
Init: func(fs *flag.FlagSet) cmdflag.Handler {
var toppings string
fs.StringVar(&toppings, "toppings", "", "coma separated list of toppings")
return func(args ...string) (int, error) {
fmt.Printf("courses: %v\n", args)
fmt.Printf("toppings: %v\n", strings.Split(toppings, ","))
return len(args), nil
}
}
}
```
## Extra features
To make life easier, a few common uses of a command line library are provided and can be easily activated:
- flags:
defining them as a flag activates them
- -version - see `VersionBoolFlag`
- -fullversion - see `FullVersionBoolFlag`
- the standard -h and -help flags are supported to display the usage of the command they apply to
- commands:
- help - provides a way to display `Application.Help` for a given command
(activated by `Command.AddHelp`)
## Contributing
Contributions welcome via pull requests. Please provide tests.