Cobra: Adding commands with dashes("-") in the name generates bad code

Created on 14 Apr 2016  路  8Comments  路  Source: spf13/cobra

For example when running cobra add-user the name of the command in the code is add-userCmd which is wrong.

arecmd kinbug

Most helpful comment

It seems that, dash in command name will be camel cased now. What should I do if I want to have a command name with dash ?

I got it. Just change "Use" in cobra.Command {}

All 8 comments

Exact issue for me.
I need - because external app calls appname backup-end and I can't change that

Just clarifying, this is only an issue with the Cobra generation tool, not with the library itself, correct?

No idea, I'm still learning Go :)

@spf13 yes I think it's just generation.

Yes, just in generation:

$ cobra add get-metadata
$ go run main.go
# github.com/jdub/cfn-init-tools/cmd
cmd\get-metadata.go:30: syntax error: unexpected -
cmd\get-metadata.go:39: syntax error: unexpected {, expecting name
cmd\get-metadata.go:41: syntax error: unexpected fmt, expecting (
cmd\get-metadata.go:41: method has multiple receivers
cmd\get-metadata.go:42: syntax error: non-declaration statement outside function body

Lines 30 and 46:

var get-metadataCmd = &cobra.Command{
...
RootCmd.AddCommand(get-metadataCmd)

Probably makes sense to automagically camel case dashed (or underscored, ew, why) commands, e.g. getMetadataCmd.

It seems that, dash in command name will be camel cased now. What should I do if I want to have a command name with dash ?

It seems that, dash in command name will be camel cased now. What should I do if I want to have a command name with dash ?

I got it. Just change "Use" in cobra.Command {}

@lzhecheng More specifically, the alias field can be used. I was wondering the same.

var foobarCmd = &cobra.Command{
    Use:     "foobar",
    Aliases: []string{"foo-bar", "FooBar"},
...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rogercoll picture rogercoll  路  5Comments

garthk picture garthk  路  3Comments

alapidas picture alapidas  路  3Comments

groenborg picture groenborg  路  5Comments

mdaymard picture mdaymard  路  5Comments