douan5151 2018-09-12 02:00
浏览 276

查询FlagSet的用法

I am trying to build a CLI where I expect a group of three arguments to appear together.

command --alpha "value-a" --bravo "value-b" --charlie "value-c"

This is how I am attempting to do it:

import (
    "github.com/spf13/pflag"
)

var (
    alpha         string
    bravo         string
    charlie       string
)

abcFlagSet := pflag.NewFlagSet("alpha", pflag.ContinueOnError)
abcFlagSet.StringVar(&bravo, "bravo", "", "bravo-description")
abcFlagSet.StringVar(&charlie, "charlie", "", "charlie-description")
cmd.Flags().AddFlagSet(abcFlagSet)

This recognizes the bravo and charlie flags correctly. However, I think it treats the alpha as a sub command of command and does not parse the value assigned to it i.e., value-a. Is FlagSet a wrong usage for this use case? How should I parse this scenario where the three arguments can appear all together or none at all?

  • 写回答

1条回答 默认 最新

  • dty3416 2018-09-12 03:14
    关注

    https://github.com/spf13/pflag/blob/298182f68c66c05229eb03ac171abe6e309ee79a/flag.go#L1202-L1213, as told here, you are forming a FlagSet with name alpha. Obviously your code does not consider that as a flag. The correct way to define a new flag with name alpha would be

    abcFlagSet.StringVar(&alpha, "alpha", "", "alpha-description").

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数