|
| 1 | +// Copyright 2022 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
1 | 15 | package cobra2snooty
|
2 | 16 |
|
3 | 17 | import (
|
@@ -53,14 +67,12 @@ func Echo() *cobra.Command {
|
53 | 67 | return echoCmd
|
54 | 68 | }
|
55 | 69 | echoCmd = &cobra.Command{
|
56 |
| - Use: "echo <string to echo> [test param]", |
| 70 | + Use: "echo <string to print> [test param]", |
57 | 71 | Aliases: []string{"say"},
|
58 | 72 | Short: "Echo anything to the screen",
|
59 | 73 | Long: "an utterly useless command for testing",
|
60 | 74 | Example: "Just run root echo",
|
61 | 75 | Annotations: map[string]string{
|
62 |
| - "args": "string to print, test param", |
63 |
| - "requiredArgs": "string to print", |
64 | 76 | "string to printDesc": "A string to print",
|
65 | 77 | "test paramDesc": "just for testing",
|
66 | 78 | },
|
@@ -108,7 +120,7 @@ var deprecatedCmd = &cobra.Command{
|
108 | 120 | }
|
109 | 121 |
|
110 | 122 | func TestGenDocs(t *testing.T) {
|
111 |
| - // We generate on a subcommand so we have both subcommands and parents |
| 123 | + // We generate on a subcommand, so we have both subcommands and parents |
112 | 124 | buf := new(bytes.Buffer)
|
113 | 125 | Root() // init root
|
114 | 126 | if err := GenDocs(Echo(), buf); err != nil {
|
@@ -166,7 +178,13 @@ func TestGenDocsNoTag(t *testing.T) {
|
166 | 178 | }
|
167 | 179 |
|
168 | 180 | func TestGenTreeDocs(t *testing.T) {
|
169 |
| - c := &cobra.Command{Use: "do [OPTIONS] arg1 arg2"} |
| 181 | + c := &cobra.Command{ |
| 182 | + Use: "do <arg1> [arg2]", |
| 183 | + Annotations: map[string]string{ |
| 184 | + "arg1Desc": "desc", |
| 185 | + "arg2Desc": "desc", |
| 186 | + }, |
| 187 | + } |
170 | 188 |
|
171 | 189 | tmpdir, err := ioutil.TempDir("", "test-gen-rst-tree")
|
172 | 190 | if err != nil {
|
@@ -212,3 +230,48 @@ func checkStringOmits(t *testing.T, got, expected string) {
|
212 | 230 | t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)
|
213 | 231 | }
|
214 | 232 | }
|
| 233 | + |
| 234 | +func TestArgsRegex(t *testing.T) { |
| 235 | + t.Run("simple", func(t *testing.T) { |
| 236 | + result := argsRegex.FindAllString("<arg1> [arg2]", -1) |
| 237 | + expected := []string{"<arg1>", "[arg2]"} |
| 238 | + for i := range result { |
| 239 | + if result[i] != expected[i] { |
| 240 | + t.Fatalf("expected: %s, got: %s\n", expected[i], result[i]) |
| 241 | + } |
| 242 | + } |
| 243 | + }) |
| 244 | + t.Run("with spaces", func(t *testing.T) { |
| 245 | + result := argsRegex.FindAllString("<this arg1> [that arg2]", -1) |
| 246 | + expected := []string{"<this arg1>", "[that arg2]"} |
| 247 | + for i := range result { |
| 248 | + if result[i] != expected[i] { |
| 249 | + t.Fatalf("expected: %s, got: %s\n", expected[i], result[i]) |
| 250 | + } |
| 251 | + } |
| 252 | + }) |
| 253 | + t.Run("repeating", func(t *testing.T) { |
| 254 | + result := argsRegex.FindAllString("<arg1>... [arg2]...", -1) |
| 255 | + expected := []string{"<arg1>", "[arg2]"} |
| 256 | + for i := range result { |
| 257 | + if result[i] != expected[i] { |
| 258 | + t.Fatalf("expected: %s, got: %s\n", expected[i], result[i]) |
| 259 | + } |
| 260 | + } |
| 261 | + }) |
| 262 | + t.Run("empty", func(t *testing.T) { |
| 263 | + result := argsRegex.FindAllString("<> []", -1) |
| 264 | + if len(result) != 0 { |
| 265 | + t.Fatalf("expected no matches\n") |
| 266 | + } |
| 267 | + }) |
| 268 | + t.Run("complex", func(t *testing.T) { |
| 269 | + result := argsRegex.FindAllString("<this arg1> <that arg2> [optional] [long option]", -1) |
| 270 | + expected := []string{"<this arg1>", "<that arg2>", "[optional]", "[long option]"} |
| 271 | + for i := range result { |
| 272 | + if result[i] != expected[i] { |
| 273 | + t.Fatalf("expected: %s, got: %s\n", expected[i], result[i]) |
| 274 | + } |
| 275 | + } |
| 276 | + }) |
| 277 | +} |
0 commit comments