Skip to content

Commit

Permalink
Add ActiveHelp to the cobra-cli (#42)
Browse files Browse the repository at this point in the history
The below messages have been added.

$ cobra-cli init <TAB>
Optionally specify the path of the go module to initialize
--
CONDUCT.md       LICENSE.txt      Makefile         bin/             go.mod           main.go
CONTRIBUTING.md  MAINTAINERS      README.md        cmd/             go.sum           tpl/

$ cobra-cli init newprogram <TAB>
This command does not take any more arguments (but may accept flags)

$ cobra-cli init newprogram extra <TAB>
ERROR: Too many arguments specified

$ cobra-cli add <TAB>
Please specify the name for the new command

$ cobra-cli add command <TAB>
This command does not take any more arguments (but may accept flags)

$ cobra-cli add command command2 <TAB>
ERROR: Too many arguments specified

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
  • Loading branch information
marckhouzam committed Jun 14, 2023
1 parent 0d35670 commit a06124f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cmd/add.go
Expand Up @@ -37,7 +37,17 @@ If you want your command to be public, pass in the command name
with an initial uppercase letter.
Example: cobra add server -> resulting in a new cmd/server.go`,

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
if len(args) == 0 {
comps = cobra.AppendActiveHelp(comps, "Please specify the name for the new command")
} else if len(args) == 1 {
comps = cobra.AppendActiveHelp(comps, "This command does not take any more arguments (but may accept flags)")
} else {
comps = cobra.AppendActiveHelp(comps, "ERROR: Too many arguments specified")
}
return comps, cobra.ShellCompDirectiveNoFileComp
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cobra.CheckErr(fmt.Errorf("add needs a name for the command"))
Expand Down
16 changes: 15 additions & 1 deletion cmd/init.go
Expand Up @@ -36,7 +36,21 @@ and the appropriate structure for a Cobra-based CLI application.
Cobra init must be run inside of a go module (please run "go mod init <MODNAME>" first)
`,

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var comps []string
var directive cobra.ShellCompDirective
if len(args) == 0 {
comps = cobra.AppendActiveHelp(comps, "Optionally specify the path of the go module to initialize")
directive = cobra.ShellCompDirectiveDefault
} else if len(args) == 1 {
comps = cobra.AppendActiveHelp(comps, "This command does not take any more arguments (but may accept flags)")
directive = cobra.ShellCompDirectiveNoFileComp
} else {
comps = cobra.AppendActiveHelp(comps, "ERROR: Too many arguments specified")
directive = cobra.ShellCompDirectiveNoFileComp
}
return comps, directive
},
Run: func(_ *cobra.Command, args []string) {
projectPath, err := initializeProject(args)
cobra.CheckErr(err)
Expand Down

0 comments on commit a06124f

Please sign in to comment.