Skip to content

About

Lightweight CLI flag parser with subcommand support for Go. Zero-dependency Go stdlib-only library.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

go-cliflags

A lightweight, zero-dependency command-line flag parser for Go that adds subcommand support on top of the ergonomics of the standard library's flag package.

Go's stdlib flag package has no notion of subcommands. go-cliflags lets you register independent subcommands (e.g. myapp deploy --env=prod or myapp build --count=3), each with its own private, isolated flag set. Two different subcommands can define a flag with the same name but different types (or different defaults) with zero cross-contamination between them.

Installation

go get github.com/kasapdev/go-cliflags

Usage

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/kasapdev/go-cliflags"
)

func main() {
	app := cliflags.NewApp("myapp")

	// "deploy" subcommand: myapp deploy --env=prod --replicas=3 --dry-run
	deploy := app.Command("deploy")
	env := deploy.String("env", "staging", "target environment")
	replicas := deploy.Int("replicas", 1, "number of replicas")
	dryRun := deploy.Bool("dry-run", false, "print actions without executing them")
	deploy.Action(func(args []string) error {
		fmt.Printf("deploying to %s with %d replicas (dry-run=%v), extra args=%v\n",
			*env, *replicas, *dryRun, args)
		return nil
	})

	// "build" subcommand: myapp build --target=linux/amd64 --jobs=8
	// Note: "build" and "deploy" can safely define unrelated flags, or even
	// a same-named flag with a different type, without any interference.
	build := app.Command("build")
	target := build.String("target", "linux/amd64", "build target platform")
	jobs := build.Int("jobs", 4, "parallel build jobs")
	build.Action(func(args []string) error {
		fmt.Printf("building %s with %d jobs, extra args=%v\n", *target, *jobs, args)
		return nil
	})

	if err := app.Run(os.Args[1:]); err != nil {
		log.Fatal(err)
	}
}

Running the above:

$ myapp deploy --env=prod --replicas=3 --dry-run extra-arg
deploying to prod with 3 replicas (dry-run=true), extra args=[extra-arg]

$ myapp build --target=darwin/arm64 --jobs=16
building darwin/arm64 with 16 jobs, extra args=[]

$ myapp launch
cliflags: unknown subcommand: "launch"

$ myapp
cliflags: no subcommand given

API

type App

The top-level entry point for a CLI tool.

  • func NewApp(name string) *App — creates a new App with the given program name.
  • func (a *App) Command(name string) *Command — registers and returns a new subcommand with its own independent flag set.
  • func (a *App) Commands() []string — returns the names of all registered subcommands, in registration order.
  • func (a *App) Lookup(name string) (*Command, bool) — looks up a registered subcommand by name.
  • func (a *App) Run(args []string) error — determines the invoked subcommand from args[0], parses the rest of args against that subcommand's flag set, and invokes its action (if any) with the remaining positional arguments.
  • func (a *App) Name() string — returns the program name.

Run returns:

  • ErrNoCommand (check with errors.Is) if args is empty.
  • An error wrapping ErrUnknownCommand (check with errors.Is) if args[0] does not name a registered subcommand.
  • An error if flag parsing fails (unknown flag, malformed value, etc.).
  • Whatever error the subcommand's action returns.

type Command

A single subcommand with its own independent flag set. Obtain one via (*App).Command.

  • func (c *Command) String(name, value, usage string) *string
  • func (c *Command) Int(name string, value int, usage string) *int
  • func (c *Command) Bool(name string, value bool, usage string) *bool
  • func (c *Command) Float64(name string, value float64, usage string) *float64
  • func (c *Command) Action(fn func(args []string) error) — attaches a handler invoked after flag parsing, with the remaining positional args.
  • func (c *Command) Args() []string — the remaining positional arguments from the most recent parse.
  • func (c *Command) Name() string — the subcommand's registered name.
  • func (c *Command) FlagSet() *flag.FlagSet — the underlying stdlib flag set, for lower-level access.

Errors

  • var ErrNoCommand error — sentinel returned when no subcommand is given.
  • var ErrUnknownCommand error — sentinel wrapped into the error returned when an unregistered subcommand name is used; use errors.Is(err, cliflags.ErrUnknownCommand).

Testing

go test ./...

License

MIT — see LICENSE.

About

Lightweight CLI flag parser with subcommand support for Go. Zero-dependency Go stdlib-only library.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages