apache/pulsar · error · CommandLine.ParameterException

Number of times need to be positive number.

Error message

Number of times need to be positive number.

What it means

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.

Source

Thrown at pulsar-client-tools/src/main/java/org/apache/pulsar/client/cli/CmdProduce.java:247

            return out.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("Cannot convert " + m + " to AVRO " + e.getMessage(), e);
        }
    }

    @Spec
    private CommandSpec commandSpec;

    /**
     * Run the producer.
     *
     * @return 0 for success, < 0 otherwise
     * @throws Exception
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public int run() throws PulsarClientException {
        if (this.numTimesProduce <= 0) {
            throw new CommandLine.ParameterException(commandSpec.commandLine(),
                    "Number of times need to be positive number.");
        }

        if (messages.size() > 0) {
            messages = messages.stream().map(str -> str.split(separator)).flatMap(Stream::of).toList();
        }

        if (messages.size() == 0 && messageFileNames.size() == 0) {
            throw new CommandLine.ParameterException(commandSpec.commandLine(),
                    "Please supply message content with either --messages or --files");
        }

        if (keyValueEncodingType == null) {
            keyValueEncodingType = KEY_VALUE_ENCODING_TYPE_NOT_SET;
        } else if (!KEY_VALUE_ENCODING_TYPE_NOT_SET.equals(keyValueEncodingType)) {
            // KeyValue schemas are not yet supported by the V5-based pulsar-client.
            throw new IllegalArgumentException("KeyValue schemas (--key-value-encoding-type) are not "
                    + "supported by this version of pulsar-client; produce with a plain value schema "

View on GitHub (pinned to 820761864e)

Solutions

  1. Pass a positive value: --num-times 1 (or -n 1)
  2. Skip invoking the produce command at all when the intended count is 0
  3. Fix the script arithmetic so the loop count is >= 1 before calling the CLI

Example fix

// before
COUNT=0
pulsar-client produce ... --num-times $COUNT
// after
COUNT=${COUNT:-1}
if [ "$COUNT" -lt 1 ]; then echo "nothing to produce"; exit 0; fi
pulsar-client produce ... --num-times "$COUNT"
Defensive patterns

Strategy: validation

Validate before calling

if [ -z "$COUNT" ] || [ "$COUNT" -lt 1 ] 2>/dev/null; then
  echo "--num-times must be a positive number" >&2; exit 2;
fi

Type guard

static int requirePositive(int n) {
    if (n <= 0) throw new IllegalArgumentException("--num-times must be positive: " + n);
    return n;
}

Try / catch

try {
    int rc = cmdProduce.run();
} catch (CommandLine.ParameterException e) {
    if (e.getMessage().contains("Number of times")) {
        System.err.println("Fix --num-times: must be >= 1, got " + numTimesArg);
        System.exit(2);
    } else { throw e; }
}

Prevention

When it happens

Trigger: Running the produce command with --num-times 0 or a negative value (or the flag left defaulted to 0 in a script).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/976ae184147349a0. Report an issue: GitHub.