英文:
How to specify "usage" for cli arguments (not flags)
问题
对于标志(flags),我可以指定在--help命令中显示的描述信息。
flag.String("a", "", "这是一个标志")
但是我没有标志,只有参数,我像这样使用命令行界面(CLI):
mycommand start 4
是否可以使用--help来查看“start”(和其他)参数的描述信息?
英文:
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?
答案1
得分: 1
由于这不是直接由标志支持的功能,我只知道alecthomas/kong包含参数用法:
package main
import "github.com/alecthomas/kong"
var CLI struct {
  Rm struct {
    Force     bool `help:"强制删除。"`
    Recursive bool `help:"递归删除文件。"`
    Paths []string `arg:"" name:"path" help:"要删除的路径。" type:"path"`
  } `cmd:"" help:"删除文件。"`
  Ls struct {
    Paths []string `arg:"" optional:"" name:"path" help:"要列出的路径。" type:"path"`
  } `cmd:"" help:"列出路径。"`
}
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> ...
删除文件。
参数:
  <paths> ...  要删除的路径。      <======  "usage" for cli arguments (not flags)!
标志:
      --debug        调试模式。
  -f, --force        强制删除。
  -r, --recursive    递归删除文件。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论