Skip to content

可以支持命令行程序内的补全吗? #7

Closed Answered by hedzr
hellodword asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @hellodword,

首先,你说的这个能力不是 cmdr 所直接支持的,cmdr 只是一个命令行参数解释器。

其次,这个能力在 cmdr 中可以自行加以实现,它和 cmdr 的体系不冲突。所以你可以通过访问 cmdr 的 Command.subcommands 层次结构体系来加以实现。
但想要实现的完美的话,可能并不怎么容易:因为需要关照很多细节,以及考虑兼容性问题。

不过另外一方面,一个独立的组件 shell-mode 对此具有较好的实现。
cmdr-addons 中提供一个对 shell-mode 的支持插件,用于将其整合到 cmdr 体系中,然后通过 shell.WithShellModule() 可以使能该三方组件。所以可以这样:

import (
	"fmt"
	"github.com/hedzr/cmdr"
	"github.com/hedzr/cmdr-addons/pkg/plugins/shell"
	cmdrexamples "github.com/hedzr/cmdr-examples"
)

func Entry() {
	if err := cmdr.Exec(
		buildRootCmd(),
		shell.WithShellModule(),
	); err != nil {
		fmt.Printf("error: %+v\n", err)
	}
}

这个 addon 的完整示例,可以参考我的一个旧的示例:shellmode

Since the ugly network communication recently, so …

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@hellodword
Comment options

Answer selected by hellodword
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants