dprfe04886 2016-08-01 23:53
浏览 184
已采纳

了解带有go-flags的子命令

I'm building a CLI tool to start learning some Go. I found this popular package that I wanted to use for some commands like this:

http://godoc.org/github.com/jessevdk/go-flags#Group
https://github.com/jessevdk/go-flags

ex list todos
ex list todos --completed
ex list events
ex list todos events
ex authenticate

The way I understand it is ex would be my main command.
list and authenticate are subcommands.

But what are todos and events in this scenario? And what would be --completed be. Is --completed called an Option? That's the verbage I see around, but not sure if I'm reading it correctly. I'm trying to find an example that has the different options for something like this scenario, but I could not find one. I tried using urfave/cli which worked at well at first, until I found a requirement to be able to pass multiple entities to ex list like ex list todos events. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • dsgsgs30201 2016-08-02 05:33
    关注

    I have used jessevdk/go-flags on multiple projects and really like it. The documentation is sparse, but the code is clean so self-documenting in many ways.

    I will try and address your questions as best I can:

    ex would be your binary, and would usually be a 'master' struct of all the top level commands (eg https://github.com/concourse/fly/blob/master/commands/fly.go). This will get parsed in your main function (eg https://github.com/concourse/fly/blob/master/main.go).

    todos and events would actually be the first level of subcommands. You would define those on the first level command (list) in the same way you define commands on the 'master' command struct.

    --completed is indeed an option.

    One example of an open source project that uses go-flags in the Concourse CI CLI https://github.com/concourse/fly. Although it doesn't you nested commands like you want to do, it gives a pretty solid foundation for go-flags usage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错