Cobra: Square brackets in the description causes problems with zsh

Created on 27 May 2020  路  5Comments  路  Source: spf13/cobra

I think I found a bug with zsh completion in cobra 1.0.0: If I put square brackets in a command description they do not get escaped correctly causing problems.
Here there's an example of the line causing problems.
I "patched" this behaviour with:

buf := new(bytes.Buffer)
    cmd.Root().GenZshCompletion(buf)
    r := strings.NewReplacer("[", "\\[", "]", "\\]") //insert escaping before [ and ]
    s := r.Replace(buf.String())
    s = strings.ReplaceAll(s, "\\[1\\]", "[1]") // revert the case
    os.Stdout.WriteString(s)

I think this will be fixed in https://github.com/spf13/cobra/pull/1070 but I'm opening that just for reference

Most helpful comment

@jpmcb I believe we can close this one now that #1070 has been merged.

All 5 comments

I just realized this is a duplicate of #899.
And I can confirm the problem is not present with #1070.

This seems to be a problem if you have brackets in the flag line, and I can't figure out how to mitigate this. For example:

--log-format string   The log format [text,color,json]. (default "color")

This causes an issue. How would you mitigate this using #1070?

This causes an issue. How would you mitigate this using #1070?

Hi @bufdev, #1070 will fix this problem automatically, nothing special for you to do. However, #1070 is not yet part of an official Cobra release, which is why it is not working for you.

You could choose to point your go.mod file to a more recent commit of the Cobra repo if you really want to get access to #1070 right away; or else, you will need to wait for the next Cobra release.

Ah perfect! Thank you so much, makes sense!

@jpmcb I believe we can close this one now that #1070 has been merged.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

supershal picture supershal  路  4Comments

lukasmalkmus picture lukasmalkmus  路  4Comments

andygrunwald picture andygrunwald  路  6Comments

zevdg picture zevdg  路  5Comments

diwakergupta picture diwakergupta  路  5Comments