Billizzard
Billizzard

Reputation: 518

How to specify "usage" for cli arguments (not flags)

For flags I can specify description which appers in --help command

flag.String("a", "", "Is is a flag")

But I don't have flags, only arguments, I use cli like this

mycommand start 4

Is it possible use --help to see description to "start" (and other) arguments?

Upvotes: 5

Views: 622

Answers (1)

VonC
VonC

Reputation: 1324997

Since this is not directly supported by flags, I know only of alecthomas/kong which does include argument usage:

package main

import "github.com/alecthomas/kong"

var CLI struct {
  Rm struct {
    Force     bool `help:"Force removal."`
    Recursive bool `help:"Recursively remove files."`

    Paths []string `arg:"" name:"path" help:"Paths to remove." type:"path"`
  } `cmd:"" help:"Remove files."`

  Ls struct {
    Paths []string `arg:"" optional:"" name:"path" help:"Paths to list." type:"path"`
  } `cmd:"" help:"List paths."`
}

func main() {
  ctx := kong.Parse(&CLI)
  switch ctx.Command() {
  case "rm <path>":
  case "ls":
  default:
    panic(ctx.Command())
  }
}

You will get with shell --help rm:

$ shell --help rm
usage: shell rm <paths> ...

Remove files.

Arguments:
  <paths> ...  Paths to remove.      <======  "usage" for cli arguments (not flags)!

Flags:
      --debug        Debug mode.

  -f, --force        Force removal.
  -r, --recursive    Recursively remove files.

Upvotes: 2

Related Questions