{"record":{"id":"2f5d19d305ef6562","repo":"apache/hadoop","slug":"too-many-arguments-expected-but-got","errorCode":null,"errorMessage":"Too many arguments: expected {} but got {}","messagePattern":"Too many arguments: expected (.+?) but got (.+?)","errorType":"exception","errorClass":"TooManyArgumentsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/CommandFormat.java","lineNumber":138,"sourceCode":"          args.remove(pos);\n        } else {\n          arg = \"\";\n        }\n        if (!arg.startsWith(\"-\") || arg.equals(\"-\")) {\n          optionsWithValue.put(opt, arg);\n        }\n      } else if (ignoreUnknownOpts) {\n        pos++;\n      } else {\n        throw new UnknownOptionException(arg);\n      }\n    }\n    int psize = args.size();\n    if (psize < minPar) {\n      throw new NotEnoughArgumentsException(minPar, psize);\n    }\n    if (psize > maxPar) {\n      throw new TooManyArgumentsException(maxPar, psize);\n    }\n  }\n  \n  /** Return if the option is set or not\n   * \n   * @param option String representation of an option\n   * @return true is the option is set; false otherwise\n   */\n  public boolean getOpt(String option) {\n    return options.containsKey(option) ? options.get(option) : false;\n  }\n\n  /**\n   * get the option's value\n   *\n   * @param option option name\n   * @return option value\n   * if option exists, but no value assigned, return \"\"","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/shell/CommandFormat.java#L120-L156","documentation":"Thrown by CommandFormat.parse() when more positional arguments remain after option stripping than the command's declared maximum (maxPar). TooManyArgumentsException extends IllegalArgumentException and reports 'Too many arguments: expected <max> but got <n>'. Only commands constructed with a finite maxPar (e.g. 'test' with maxPar=1) can raise it; commands declared with Integer.MAX_VALUE never do.","triggerScenarios":"'hdfs dfs -test -e /a /b' (test accepts at most one path), passing multiple paths to any single-path command, or a shell glob expanding ('*.csv' to many files) into a command with maxPar=1.","commonSituations":"Unquoted globs expanding unexpectedly in scripts; users assuming 'test' accepts multiple paths like 'ls'; wrapping loops that feed an array where a single path is expected; misreading an option as a value so a stray token lands in the positional list.","solutions":["Invoke the command once per path (loop) instead of passing all paths at once to single-path commands like test","Quote or pre-validate globs so they expand to the expected number of arguments before the call","Re-read 'hdfs dfs -help <command>' to see the accepted argument shape"],"exampleFix":"# before\nhdfs dfs -test -e /data/a /data/b\n# after\nhdfs dfs -test -e /data/a && hdfs dfs -test -e /data/b","handlingStrategy":"validation","validationCode":"// single-path commands: assert exactly one positional before running\nString[] positional = Arrays.stream(argv).filter(a -> !a.startsWith(\"-\")).toArray(String[]::new);\nif (positional.length > 1) {\n  throw new IllegalArgumentException(\"command takes exactly one path, got \" + positional.length);\n}","typeGuard":null,"tryCatchPattern":"try {\n  cf.parse(args);\n} catch (CommandFormat.TooManyArgumentsException e) {\n  // split the workload and run the command once per path\n  for (String p : extraPaths) runCommand(singlePathCmd(p));\n}","preventionTips":["Quote globs you want passed literally and count matches you want expanded","Read the command's USAGE - single-path commands (test) never accept a list","In wrappers, validate argument count ranges before delegating to FsShell"],"tags":["hadoop","hdfs","cli","argument-validation","shell","java"],"backgroundTag":"too-many-command-line-arguments","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}