{"record":{"id":"8955434556baa8aa","repo":"urfave/cli","slug":"stoponntharg-must-be-non-negative-got-d","errorCode":null,"errorMessage":"StopOnNthArg must be non-negative, got %d","messagePattern":"StopOnNthArg must be non-negative, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command_run.go","lineNumber":105,"sourceCode":"\n\treturn args, nil\n}\n\n// Run is the entry point to the command graph. The positional\n// arguments are parsed according to the Flag and Command\n// definitions and the matching Action functions are run.\nfunc (cmd *Command) Run(ctx context.Context, osArgs []string) (deferErr error) {\n\t_, deferErr = cmd.run(ctx, osArgs)\n\treturn deferErr\n}\n\nfunc (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context, deferErr error) {\n\ttracef(\"running with arguments %[1]q (cmd=%[2]q)\", osArgs, cmd.Name)\n\tcmd.setupDefaults(osArgs)\n\n\t// Validate StopOnNthArg\n\tif cmd.StopOnNthArg != nil && *cmd.StopOnNthArg < 0 {\n\t\treturn ctx, fmt.Errorf(\"StopOnNthArg must be non-negative, got %d\", *cmd.StopOnNthArg)\n\t}\n\n\tif v, ok := ctx.Value(commandContextKey).(*Command); ok {\n\t\ttracef(\"setting parent (cmd=%[1]q) command from context.Context value (cmd=%[2]q)\", v.Name, cmd.Name)\n\t\tcmd.parent = v\n\t}\n\n\tif cmd.parent == nil {\n\t\tif cmd.ReadArgsFromStdin {\n\t\t\tif args, err := cmd.parseArgsFromStdin(); err != nil {\n\t\t\t\treturn ctx, err\n\t\t\t} else {\n\t\t\t\tosArgs = append(osArgs, args...)\n\t\t\t}\n\t\t}\n\t\t// handle the completion flag separately from the flagset since\n\t\t// completion could be attempted after a flag, but before its value was put\n\t\t// on the command line. this causes the flagset to interpret the completion","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/urfave/cli/blob/1a4deb4f5a35ee12706602698dc0527d44c14b19/command_run.go#L87-L123","documentation":"Before executing a command, run() validates that the StopOnNthArg option, when set, is not negative. A negative pointer value indicates a misconfiguration, so the command aborts early with 'StopOnNthArg must be non-negative, got %d'.","triggerScenarios":"Setting cmd.StopOnNthArg to a pointer initialized from an unvalidated config/int that is negative (e.g. from a config file, env var, or default of -1 used as a sentinel while the code treats non-nil as active).","commonSituations":"Config-driven CLIs where -1 was meant to mean 'disabled' but was stored via pointer instead of leaving the pointer nil; arithmetic producing negative index; copy-pasted sentinel values.","solutions":["Leave StopOnNthArg nil to disable it instead of pointing it at a negative number","Clamp or validate the value before assignment: if v < 0 { v = 0 } or skip assignment","Fix the config source (env/config file) that supplies the negative value"],"exampleFix":"// before\nn := -1\ncmd.StopOnNthArg = &n\n// after\nvar n *int // nil disables the feature, or ensure *n >= 0 before assigning","handlingStrategy":"validation","validationCode":"func setStopOnNthArg(cmd *cli.Command, n int) error {\n    if n < 0 { return fmt.Errorf(\"StopOnNthArg must be >= 0, got %d\", n) }\n    cmd.StopOnNthArg = &n\n    return nil\n}","typeGuard":"func validStopOnNthArg(p *int) bool { return p == nil || *p >= 0 }","tryCatchPattern":null,"preventionTips":["Never use negative sentinels with a pointer-typed option; use nil for 'disabled'","Validate config-file and env inputs that feed StopOnNthArg before wiring them","Add a startup assertion in tests covering the command's default construction"],"tags":["go","cli","configuration","validation"],"backgroundTag":"invalid-configuration-value","analyzedSha":"1a4deb4f5a35ee12706602698dc0527d44c14b19","analyzedAt":"2026-08-31T18:42:09.451Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}