apache/hadoop · error · NotEnoughArgumentsException

Not enough arguments: expected {} but got {}

Error message

Not enough arguments: expected {} but got {}

What it means

Thrown by CommandFormat.parse() when, after all recognized options have been stripped from the argument list, fewer positional arguments remain than the command's declared minimum (minPar). NotEnoughArgumentsException extends IllegalArgumentException and reports 'Not enough arguments: expected <min> but got <n>'. Options with values (e.g. -t 4) also consume their value token before the count is taken, which can silently reduce the remaining count.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/CommandFormat.java:135

        if (pos < args.size() && (args.size() > minPar)
                && !args.get(pos).startsWith("-")) {
          arg = args.get(pos);
          args.remove(pos);
        } else {
          arg = "";
        }
        if (!arg.startsWith("-") || arg.equals("-")) {
          optionsWithValue.put(opt, arg);
        }
      } else if (ignoreUnknownOpts) {
        pos++;
      } else {
        throw new UnknownOptionException(arg);
      }
    }
    int psize = args.size();
    if (psize < minPar) {
      throw new NotEnoughArgumentsException(minPar, psize);
    }
    if (psize > maxPar) {
      throw new TooManyArgumentsException(maxPar, psize);
    }
  }
  
  /** Return if the option is set or not
   * 
   * @param option String representation of an option
   * @return true is the option is set; false otherwise
   */
  public boolean getOpt(String option) {
    return options.containsKey(option) ? options.get(option) : false;
  }

  /**
   * get the option's value
   *

View on GitHub (pinned to 2add963021)

Solutions

  1. Check the command's usage ('hdfs dfs -help <cmd>') and supply the required number of positional paths
  2. Set -u / check your shell variables before invoking FsShell so empty arguments are not silently dropped
  3. When using value-options like -t/-q, always attach the value in the same token ('-t 4') and ensure it is not the argument you intended as a path
  4. In code, assert args.length after option stripping before calling run()

Example fix

# before
hdfs dfs -cp /user/me/source.csv
# after
hdfs dfs -cp /user/me/source.csv /user/me/destination/
Defensive patterns

Strategy: validation

Validate before calling

// enforce the command's minimum positional count before invoking FsShell
long positional = Arrays.stream(argv).filter(a -> !a.startsWith("-")).count();
if (positional < 2) { // e.g. cp needs src + dst
  throw new IllegalArgumentException("cp requires <src> ... <dst>, got " + positional);
}

Try / catch

try {
  cf.parse(args);
} catch (CommandFormat.NotEnoughArgumentsException e) {
  // e.getMessage() == "Not enough arguments: expected N but got M"
  throw new UsageException(command.getUsage(), e);
}

Prevention

When it happens

Trigger: 'hdfs dfs -cp /src' (cp declares minPar=2, one path left), 'hadoop fs -test -e' with no path, 'hdfs dfs -appendToFile' with fewer than 2 arguments, or an option-with-value whose value token was itself the last positional argument (e.g. '-put -t dst' where 'dst' is swallowed as the -t value).

Common situations: Shell scripts where a variable expands to empty (e.g. 'hdfs dfs -mkdir $DIR' with unset DIR); value-options like -t/-q consuming the next token when it is not dash-prefixed; commands invoked from Java via FsShell.run() with programmatically-built argument lists that dropped an element.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/64ff06739da53669. Report an issue: GitHub.