Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 420
Billizzard Avatar asked Nov 06 '22 23:11

Billizzard


1 Answers

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.
like image 141
VonC Avatar answered Nov 11 '22 03:11

VonC