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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)