Golang眼镜蛇CLI-获取当前运行子命令
眼镜蛇是否在某个地方存储了当前运行子命令的名称?例如,如果用户使用以下命令启动该工具,例如:
- 工具子命令-flag1 -flag2等。
- 工具-globalflag1 sub -command -flag1 -flag2
是否可以在程序内部确定哪个子命令在某些可变或方法中使用哪个子命令?
Does cobra store the name of currently running sub-command somewhere? E.g. if user started the tool with commands like:
- tool sub-command -flag1 -flag2, etc.
- tool -globalflag1 sub-command -flag1 -flag2
is it possible to determine inside the program which sub-command is used from some variable or method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
COBRA命令结构提供了几种访问当前正在运行的命令的方法:
假设有一个子命令
subcmd
注册和一个argmyarg
,执行mycli subcmd myarg 返回:
并且,如您的评论中所述,您可以通过在空间上拆分来获得命令路径的特定部分:
The cobra Command struct offers a few methods to access what command is currently running:
Assuming there's a subcommand
subcmd
registered and one argmyarg
, executingmycli subcmd myarg
returns:And, as mentioned in your comment, you can get a specific part of the command path by splitting on space: