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)
| 44 | // user supplied is deliberately not echoed back — it adds nothing they do not |
| 45 | // already know from the line they just typed. |
| 46 | func argCountError(cmd *cobra.Command, requirement string) error { |
| 47 | var b strings.Builder |
| 48 | b.Grow(256) |
| 49 | |
| 50 | path := cmd.CommandPath() |
| 51 | fmt.Fprintf(&b, "%q %s", path, requirement) |
| 52 | if sig := positionalSignature(cmd); sig != "" { |
| 53 | fmt.Fprintf(&b, " (%s)", sig) |
| 54 | } |
| 55 | |
| 56 | if len(cmd.ValidArgs) > 0 { |
| 57 | b.WriteString("\n\nValid values: ") |
| 58 | b.WriteString(strings.Join(validArgNames(cmd.ValidArgs), ", ")) |
| 59 | } |
| 60 | |
| 61 | b.WriteString("\n\nUsage:\n ") |
| 62 | b.WriteString(cmd.UseLine()) |
| 63 | |
| 64 | if example := strings.TrimRight(cmd.Example, "\n"); example != "" { |
| 65 | b.WriteString("\n\nExample:\n") |
| 66 | b.WriteString(example) |
| 67 | } |
| 68 | |
| 69 | b.WriteString("\n\nRun '") |
| 70 | b.WriteString(path) |
| 71 | b.WriteString(" --help' for more information.") |
| 72 | |
| 73 | return errors.New(b.String()) |
| 74 | } |
| 75 | |
| 76 | // positionalSignature extracts the positional-argument part of cmd.Use, e.g. |
| 77 | // "<key> <value>" from "set <key> <value>" and "<path...>" from |