Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --version support #26

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
ErrNoRunner = errors.New("gommand: command has no run function")
ErrNoSubcommand = errors.New("gommand: must specify a subcommand")
errShowHelp = errors.New("show help")
errShowVersion = errors.New("show version")
)

// Command represents a command line command
Expand Down Expand Up @@ -66,6 +67,15 @@ type Command struct {
// Description is the longer description of the command printed out by the help text
Description string

// Version is the value that will be printed when `--version` is passed to the command.
// When retrieving the command version, the call tree is traversed backwards until a Command
// is reached that has a non-zero value for the version. This means that it is possible
// to version individual branches of the call tree, though this is not recommended. It is
// intended to be set at the root of the tree, ideally through a package level var that can
// be set using ldflags at build time
// ie: go build -ldflags="cmd.Version=1.1.0"
Version string

// ArgValidator is an ArgValidator to be called on the args of the function being executed. This is called before any of
// the functions for this command are called.
// If this is not defined ArgsNone is used.
Expand Down Expand Up @@ -158,6 +168,15 @@ func (c *Command) ExecuteContext(ctx context.Context) error {
cmdCtx.cmd.help()
return nil
}
if errors.Is(err, errShowVersion) {
v := c._version()
if v == "" {
v = "N/A"
}
fmt.Println(v)
return nil
}

if mErr := errors.Join(err, c.err); mErr != nil {
if !cmdCtx.silenceHelp {
cmdCtx.cmd.help()
Expand Down Expand Up @@ -185,6 +204,17 @@ func (c *Command) name() (name string) {
return
}

func (c *Command) _version() string {
_c := c
for _c.Version == "" {
if _c.parent == nil {
break
}
_c = _c.parent
}
return _c.Version
}

func (c *Command) help() {
if c.Description != "" {
fmt.Println(c.Description)
Expand All @@ -194,6 +224,11 @@ func (c *Command) help() {
fmt.Println()
}

if v := c._version(); v != "" {
fmt.Println("Version:", v)
fmt.Println()
}

fmt.Println("Usage:")
usage := []string{c.Name}
for parent := c.parent; parent != nil; parent = parent.parent {
Expand Down Expand Up @@ -352,6 +387,9 @@ func (c *Command) execute(ctx *Context) error {
if token.Name == "help" {
return errShowHelp
}
if token.Name == "version" {
return errShowVersion
}
f = fs.FromName(token.Name)
}

Expand Down
1 change: 1 addition & 0 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var rootCmd = &gommand.Command{
Name: "sum",
Usage: "sum [...n]",
Description: "sum all provided numbers",
Version: "1.0.0",
ArgValidator: gommand.ArgsEvery(gommand.ArgsMin(1), ints),
Run: func(ctx *gommand.Context) error {
var total int
Expand Down
1 change: 1 addition & 0 deletions examples/subcommands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
rootCmd = &gommand.Command{
Name: "math",
Usage: "a collection of math commands",
Version: "1.0.0",
SilenceError: true,
PersistentFlagSet: flags.NewFlagSet().AddFlags(
flags.StringFlag("host", "", "host address"),
Expand Down
Loading