{"record":{"id":"5c7f7848211c93b6","repo":"larksuite/cli","slug":"name-q-must-not-include-leading-dashes","errorCode":null,"errorMessage":"name %q must not include leading dashes","messagePattern":"name %q must not include leading dashes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/flagalias/flagalias.go","lineNumber":276,"sourceCode":"\t}\n\tif value, ok := flag.Value.(*trackedSliceValue); ok {\n\t\treturn value.trackedValue\n\t}\n\ttracked := &trackedValue{Value: flag.Value, canonical: flag.Name}\n\tif slice, ok := flag.Value.(pflag.SliceValue); ok {\n\t\tflag.Value = &trackedSliceValue{trackedValue: tracked, slice: slice}\n\t} else {\n\t\tflag.Value = tracked\n\t}\n\treturn tracked\n}\n\nfunc validateAliasName(name string) error {\n\tswitch {\n\tcase name == \"\":\n\t\treturn fmt.Errorf(\"name must not be empty\")\n\tcase strings.HasPrefix(name, \"-\"):\n\t\treturn fmt.Errorf(\"name %q must not include leading dashes\", name)\n\tcase strings.ContainsAny(name, \" \\t\\r\\n\"):\n\t\treturn fmt.Errorf(\"name %q must not contain whitespace\", name)\n\tcase strings.Contains(name, \"=\"):\n\t\treturn fmt.Errorf(\"name %q must not contain '='\", name)\n\tdefault:\n\t\treturn nil\n\t}\n}\n\nfunc collectRegistered(dst map[string]string, set *pflag.FlagSet) {\n\tif set == nil {\n\t\treturn\n\t}\n\tset.VisitAll(func(flag *pflag.Flag) {\n\t\tdst[flag.Name] = flag.Name\n\t})\n}\n","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/internal/flagalias/flagalias.go#L258-L294","documentation":"The flagalias package binds flag alias specs onto a cobra command, and every alias name must be usable as a bare flag token. validateAliasName rejects names starting with '-' because pflag convention treats leading dashes as prefix syntax, not part of the flag name; a dashed alias would be unmatchable or ambiguous when users type it. Bind calls validateAliasName on each Spec's name before registering, so a malformed spec fails fast at command construction.","triggerScenarios":"Calling flagalias.Bind(cmd, specs) where any Spec.Name (or alias entry validated through validateAliasName) begins with one or more '-' characters, e.g. Name: \"--verbose\" or Name: \"-v\" instead of \"verbose\" or \"v\".","commonSituations":"Copy-pasting a full CLI invocation like \"--output json\" into a spec literal; building spec names dynamically from user/flag strings that already carry dashes; converting old shell wrapper scripts where flags were written with dashes.","solutions":["Strip leading dashes from the spec name before building the Spec (strings.TrimLeft(name, \"-\")).","Pass only the bare flag token: use \"verbose\", not \"--verbose\"; the dash spelling is what users type on the command line, not what goes in the spec.","If the name comes from config or user input, validate/normalize it at config-load time with the same rules (no leading '-', whitespace, or '=').","Log or list all specs on failure since Bind validates names one at a time and stops at the first bad one."],"exampleFix":"// before\nspecs := []flagalias.Spec{{Name: \"--json-output\", Target: \"json\"}}\nerr := flagalias.Bind(cmd, specs)\n// after\nspecs := []flagalias.Spec{{Name: \"json-output\", Target: \"json\"}}\nerr := flagalias.Bind(cmd, specs)","handlingStrategy":"validation","validationCode":"func validAliasName(name string) bool {\n\treturn name != \"\" && !strings.HasPrefix(name, \"-\") &&\n\t\t!strings.ContainsAny(name, \" \\t\\r\\n\") && !strings.Contains(name, \"=\")\n}\n// before Bind:\nfor _, s := range specs {\n\tif !validAliasName(s.Name) {\n\t\treturn fmt.Errorf(\"invalid alias spec name %q\", s.Name)\n\t}\n}","typeGuard":"func isRegistrableName(name string) bool {\n\treturn name != \"\" && !strings.HasPrefix(name, \"-\") &&\n\t\t!strings.ContainsAny(name, \" \\t\\r\\n\") && !strings.Contains(name, \"=\")\n}","tryCatchPattern":null,"preventionTips":["Store spec names as bare tokens; never write them with '-' prefixes in code or config.","Add a unit test that runs every declared Spec through the same validation rules as validateAliasName.","Validate names once where specs are constructed, not only at Bind time."],"tags":["go","cli","flags","input-validation"],"backgroundTag":"invalid-flag-name","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}