duangan6731 2017-01-08 00:58
浏览 61

如何引用go-flag IsSet,需要功能代码示例

New to Go, and having a basic conceptual problem (I think)...

Trying to use github.com/jessevdk/go-flags and have it mostly working. --help and whatnot are working fine, flags are being passed, etc.

I need to understand if a option was set via a flag or via go-flags parser using the provided default value. It appears go-flags has an "IsSet" function, but I'm clueless how to reference it. Presume:

var opts struct {
    Port int `short:"p" long:"Port" description:"IP port" default:"1111"
}
_, err := flags.Parse(&opts) 

I can reference the value via "opts.Port", but how can I find out if the option was set via a flag or default? Many thanks in advance!

  • 写回答

1条回答 默认 最新

  • duanqiang7631 2017-01-10 02:21
    关注

    Figured this out:

    parser := flags.NewParser(&opts, flags.Default) o := parser.FindOptionByLongName("Port) if o.IsSet() {}

    Problme is that IsSet() is true if the flag was used on the command line OR if it was set via the default.

    So solved the surface problem of referencing IsSet() but still hunting for an ability to tell which occured, since I want the flag defaults to show in --help.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度