如何为 cli 参数(不是标志)指定"usage"



对于标志,我可以指定出现在--help命令中的描述

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

但我没有标志,只有参数,我像这个一样使用cli

mycommand start 4

是否可以使用帮助查看";"开始";(和其他(论点?

由于标志不直接支持这一点,我只知道alecthomas/kong,它确实包括参数用法:

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())
}
}

您将获得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.

最新更新