douyun7718 2015-12-02 21:59
浏览 204
已采纳

golang:如何将pflag与其他使用flag的软件包一起使用?

How does one use pflag while also using other packages that use flag?

Some of these packages define flags for the flag package (e.g. in their init functions) - and require flag.Parse() to be called.

Defining flags using the pflag package, require pflag.Parse() to be called.

One of the calls to flag.Parse() and pflag.Parse() will fail when arguments are mixed.

How does one use pflag with other packages that use flag?

  • 写回答

3条回答 默认 最新

  • doujian7132 2015-12-03 18:43
    关注

    I got a response from a maintainer for pflag.

    The solution is to "import" all flags defined for flag into pflag, and then make a call to pflag.Parse() alone.

    pflag provides a convenience function called AddGoFlagSet() to accomplish this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongmeng9048 2015-12-03 08:09
    关注

    How does one use pflag with other packages that use flag?

    You cannot. It is either or. plfag is a drop-in replacement for flag, not an addon.

    评论
  • dousi4950 2015-12-25 09:46
    关注

    I have found two approach for this.

    1. One with pflags AddGoFlags(). IE.

      f := pflag.NewFlagSet("goFlags", pflag.ExitOnError)
      f.AddGoFlagSet(flag.CommandLine)
      f.Parse([]string{
         "--v=10", // Your go flags that will be pass to other programs.
      })
      flag.Parse()
      
    2. Another approach you can use. This is not by the book but i have seen some used this.. get the flag valu by another pflag.

      pflag.StringVar(&f, "f", "**", "***")
      pflag.Parse()
      

    Set the value as flag value.

        flag.Set("v", f)
        flag.Parse()
    

    and you are good to go..

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥15 C语言:数据子序列基础版
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?
  • ¥15 嵌入式Linux固件,能直接告诉我crc32校验的区域在哪不,内核的校验我已经找到了,uboot没有