{"record":{"id":"ec67269ae2b355eb","repo":"caddyserver/caddy","slug":"invalid-command-name","errorCode":null,"errorMessage":"invalid command name","messagePattern":"invalid command name","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"cmd/commands.go","lineNumber":595,"sourceCode":"// This function should be used in init().\nfunc RegisterCommand(cmd Command) {\n\tcommandsMu.Lock()\n\tdefer commandsMu.Unlock()\n\n\tif cmd.Name == \"\" {\n\t\tpanic(\"command name is required\")\n\t}\n\tif cmd.Func == nil && cmd.CobraFunc == nil {\n\t\tpanic(\"command function missing\")\n\t}\n\tif cmd.Short == \"\" {\n\t\tpanic(\"command short string is required\")\n\t}\n\tif _, exists := commands[cmd.Name]; exists {\n\t\tpanic(\"command already registered: \" + cmd.Name)\n\t}\n\tif !commandNameRegex.MatchString(cmd.Name) {\n\t\tpanic(\"invalid command name\")\n\t}\n\tdefaultFactory.Use(func(rootCmd *cobra.Command) {\n\t\trootCmd.AddCommand(caddyCmdToCobra(cmd))\n\t})\n\tcommands[cmd.Name] = cmd\n}\n\nvar commandNameRegex = regexp.MustCompile(`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$`)\n","sourceCodeStart":577,"sourceCodeEnd":604,"githubUrl":"https://github.com/caddyserver/caddy/blob/50e54ee279aa1e504fe218ca49ab6ae16c100410/cmd/commands.go#L577-L604","documentation":"RegisterCommand panics when cmd.Name does not match commandNameRegex (^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$): lowercase alphanumerics with single hyphens, no leading/trailing hyphen, no uppercase, no underscores. The check keeps CLI subcommand names uniform and shell-friendly.","triggerScenarios":"Registering a command with a name like 'MyCmd', 'do_thing', '-run', or 'run--fast' from a plugin's init(); the regex fails and the process panics at startup.","commonSituations":"Porting commands from other ecosystems that allow underscores or camelCase; autogenerated names from constants; hyphen-doubled names from string concatenation.","solutions":["Rename to lowercase-with-hyphens (kebab-case), e.g. 'my-plugin-run'","Ensure no leading/trailing hyphen and no consecutive hyphens","Add a unit test in the plugin asserting commandNameRegex.MatchString(name)"],"exampleFix":"// before\ncaddy.RegisterCommand(caddy.Command{Name: \"MyPlugin_Run\", ...})\n\n// after\ncaddy.RegisterCommand(caddy.Command{Name: \"my-plugin-run\", ...})","handlingStrategy":"validation","validationCode":"var commandNameRegex = regexp.MustCompile(`^[a-z0-9]$|^([a-z0-9]+-?[a-z0-9]*)+[a-z0-9]$`)\nif !commandNameRegex.MatchString(cmd.Name) {\n    return fmt.Errorf(\"command name %q must be lowercase kebab-case\", cmd.Name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use lowercase kebab-case names with single hyphens only","Assert regex validity in the plugin's test suite"],"tags":["caddy","cli","plugin","panic","naming"],"backgroundTag":null,"analyzedSha":"50e54ee279aa1e504fe218ca49ab6ae16c100410","analyzedAt":"2026-08-15T09:20:21.641Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}