{"record":{"id":"976ae184147349a0","repo":"apache/pulsar","slug":"number-of-times-need-to-be-positive-number","errorCode":null,"errorMessage":"Number of times need to be positive number.","messagePattern":"Number of times need to be positive number\\.","errorType":"validation","errorClass":"CommandLine.ParameterException","httpStatus":null,"severity":"error","filePath":"pulsar-client-tools/src/main/java/org/apache/pulsar/client/cli/CmdProduce.java","lineNumber":247,"sourceCode":"            return out.toByteArray();\n        } catch (IOException e) {\n            throw new RuntimeException(\"Cannot convert \" + m + \" to AVRO \" + e.getMessage(), e);\n        }\n    }\n\n    @Spec\n    private CommandSpec commandSpec;\n\n    /**\n     * Run the producer.\n     *\n     * @return 0 for success, < 0 otherwise\n     * @throws Exception\n     */\n    @SuppressWarnings({\"rawtypes\", \"unchecked\"})\n    public int run() throws PulsarClientException {\n        if (this.numTimesProduce <= 0) {\n            throw new CommandLine.ParameterException(commandSpec.commandLine(),\n                    \"Number of times need to be positive number.\");\n        }\n\n        if (messages.size() > 0) {\n            messages = messages.stream().map(str -> str.split(separator)).flatMap(Stream::of).toList();\n        }\n\n        if (messages.size() == 0 && messageFileNames.size() == 0) {\n            throw new CommandLine.ParameterException(commandSpec.commandLine(),\n                    \"Please supply message content with either --messages or --files\");\n        }\n\n        if (keyValueEncodingType == null) {\n            keyValueEncodingType = KEY_VALUE_ENCODING_TYPE_NOT_SET;\n        } else if (!KEY_VALUE_ENCODING_TYPE_NOT_SET.equals(keyValueEncodingType)) {\n            // KeyValue schemas are not yet supported by the V5-based pulsar-client.\n            throw new IllegalArgumentException(\"KeyValue schemas (--key-value-encoding-type) are not \"\n                    + \"supported by this version of pulsar-client; produce with a plain value schema \"","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client-tools/src/main/java/org/apache/pulsar/client/cli/CmdProduce.java#L229-L265","documentation":"CmdProduce.run validates numTimesProduce and throws a CommandLine.ParameterException when it is <= 0. The --num-times flag controls how many times the message set is produced, so only positive counts are accepted.","triggerScenarios":"Running the produce command with --num-times 0 or a negative value (or the flag left defaulted to 0 in a script).","commonSituations":"Loop-count variable computed as 0 (empty input, wrong arithmetic); using 0 to mean 'no-op' instead of omitting the flag; copy-pasted command with a placeholder like -n $COUNT where COUNT is unset/empty coerced oddly.","solutions":["Pass a positive value: --num-times 1 (or -n 1)","Skip invoking the produce command at all when the intended count is 0","Fix the script arithmetic so the loop count is >= 1 before calling the CLI"],"exampleFix":"// before\nCOUNT=0\npulsar-client produce ... --num-times $COUNT\n// after\nCOUNT=${COUNT:-1}\nif [ \"$COUNT\" -lt 1 ]; then echo \"nothing to produce\"; exit 0; fi\npulsar-client produce ... --num-times \"$COUNT\"","handlingStrategy":"validation","validationCode":"if [ -z \"$COUNT\" ] || [ \"$COUNT\" -lt 1 ] 2>/dev/null; then\n  echo \"--num-times must be a positive number\" >&2; exit 2;\nfi","typeGuard":"static int requirePositive(int n) {\n    if (n <= 0) throw new IllegalArgumentException(\"--num-times must be positive: \" + n);\n    return n;\n}","tryCatchPattern":"try {\n    int rc = cmdProduce.run();\n} catch (CommandLine.ParameterException e) {\n    if (e.getMessage().contains(\"Number of times\")) {\n        System.err.println(\"Fix --num-times: must be >= 1, got \" + numTimesArg);\n        System.exit(2);\n    } else { throw e; }\n}","preventionTips":["Default --num-times to 1 in wrapper scripts when unset","Validate the count is a positive integer (grep -E '^[0-9]+$') before invoking","Skip the produce invocation entirely when there is nothing to produce rather than passing 0","Guard loop-arithmetic in CI so empty inputs can't yield 0/negative counts"],"tags":["cli","validation","arguments","picocli"],"backgroundTag":"invalid-argument-value","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}