argCountError assembles the guidance for a wrong argument count. The count the user supplied is deliberately not echoed back — it adds nothing they do not already know from the line they just typed.
(cmd *cobra.Command, requirement string)
| 55 | // user supplied is deliberately not echoed back — it adds nothing they do not |
| 56 | // already know from the line they just typed. |
| 57 | func argCountError(cmd *cobra.Command, requirement string) error { |
| 58 | var b strings.Builder |
| 59 | b.Grow(256) |
| 60 | |
| 61 | path := cmd.CommandPath() |
| 62 | fmt.Fprintf(&b, "%q %s", path, requirement) |
| 63 | if sig := positionalSignature(cmd); sig != "" { |
| 64 | fmt.Fprintf(&b, " (%s)", sig) |
| 65 | } |
| 66 | |
| 67 | if len(cmd.ValidArgs) > 0 { |
| 68 | b.WriteString("\n\nValid values: ") |
| 69 | b.WriteString(strings.Join(validArgNames(cmd.ValidArgs), ", ")) |
| 70 | } |
| 71 | |
| 72 | b.WriteString("\n\nUsage:\n ") |
| 73 | b.WriteString(cmd.UseLine()) |
| 74 | |
| 75 | if example := strings.TrimRight(cmd.Example, "\n"); example != "" { |
| 76 | b.WriteString("\n\nExample:\n") |
| 77 | b.WriteString(example) |
| 78 | } |
| 79 | |
| 80 | b.WriteString("\n\nRun '") |
| 81 | b.WriteString(path) |
| 82 | b.WriteString(" --help' for more information.") |
| 83 | |
| 84 | return errors.New(b.String()) |
| 85 | } |
| 86 | |
| 87 | // positionalSignature extracts the positional-argument part of cmd.Use, e.g. |
| 88 | // "<key> <value>" from "set <key> <value>" and "<path...>" from |