Skip to content

Commit 6d02e15

Browse files
authored
fix: fix spacing for multi args (#4)
1 parent 2b19f41 commit 6d02e15

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

cobra2snooty_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cobra2snooty
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -52,11 +53,17 @@ func Echo() *cobra.Command {
5253
return echoCmd
5354
}
5455
echoCmd = &cobra.Command{
55-
Use: "echo [string to echo]",
56+
Use: "echo <string to echo> [test param]",
5657
Aliases: []string{"say"},
5758
Short: "Echo anything to the screen",
5859
Long: "an utterly useless command for testing",
5960
Example: "Just run root echo",
61+
Annotations: map[string]string{
62+
"args": "string to print, test param",
63+
"requiredArgs": "string to print",
64+
"string to printDesc": "A string to print",
65+
"test paramDesc": "just for testing",
66+
},
6067
}
6168
echoCmd.PersistentFlags().StringP("strone", "s", "one", "help message for flag strone")
6269
echoCmd.PersistentFlags().BoolP("persistentbool", "p", false, "help message for flag persistentbool")
@@ -113,6 +120,9 @@ func TestGenDocs(t *testing.T) {
113120
checkStringContains(t, output, Echo().Example)
114121
checkStringContains(t, output, "boolone")
115122
checkStringContains(t, output, "rootflag")
123+
//
124+
checkStringContains(t, output, fmt.Sprintf(" * - string to print\n - string\n - true\n - %s\n", Echo().Annotations["string to printDesc"]))
125+
checkStringContains(t, output, fmt.Sprintf(" * - test param\n - string\n - false\n - %s\n", Echo().Annotations["test paramDesc"]))
116126
checkStringOmits(t, output, Root().Short)
117127
checkStringContains(t, output, EchoSubCmd().Short)
118128
checkStringOmits(t, output, deprecatedCmd.Short)
@@ -165,11 +175,11 @@ func TestGenTreeDocs(t *testing.T) {
165175
defer os.RemoveAll(tmpdir)
166176

167177
if err := GenTreeDocs(c, tmpdir); err != nil {
168-
t.Fatalf("GenReSTTree failed: %s", err.Error())
178+
t.Fatalf("GenTreeDocs failed: %s", err.Error())
169179
}
170180

171181
if _, err := os.Stat(filepath.Join(tmpdir, "do.txt")); err != nil {
172-
t.Fatalf("Expected file 'do.rst' to exist")
182+
t.Fatalf("Expected file 'do.txt' to exist")
173183
}
174184
}
175185

options.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ func printArgs(buf *bytes.Buffer, cmd *cobra.Command) error {
3232
}
3333

3434
for _, arg := range strings.Split(args, ",") {
35-
required := stringInSlice(requiredSlice, arg)
36-
if description, hasDescription := cmd.Annotations[arg+"Desc"]; hasDescription {
37-
line := fmt.Sprintf(" * - %s\n - string\n - %v\n - %s", arg, required, description)
35+
trimmedArg := strings.TrimSpace(arg)
36+
required := stringInSlice(requiredSlice, trimmedArg)
37+
if description, hasDescription := cmd.Annotations[trimmedArg+"Desc"]; hasDescription {
38+
line := fmt.Sprintf(" * - %s\n - string\n - %v\n - %s\n", trimmedArg, required, description)
3839
buf.WriteString(line)
3940
} else {
40-
return fmt.Errorf("%w: %s - %s", ErrMissingDescription, cmd.Use, arg)
41+
return fmt.Errorf("%w: %s - %s", ErrMissingDescription, cmd.Use, trimmedArg)
4142
}
4243
}
43-
buf.WriteString("\n\n")
44+
buf.WriteString("\n")
4445
}
4546
return nil
4647
}

0 commit comments

Comments
 (0)