{"record":{"id":"1e13caa4f8c7eb2e","repo":"bazelbuild/bazel","slug":"c-cmd-flag-needs-an-argument","errorCode":null,"errorMessage":"-c <cmd> flag needs an argument","messagePattern":"-c <cmd> flag needs an argument","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/cmd/Main.java","lineNumber":188,"sourceCode":"\n  public static void main(String[] args) throws IOException {\n    String file = null;\n    String cmd = null;\n    String cpuprofile = null;\n\n    // parse flags\n    int i;\n    for (i = 0; i < args.length; i++) {\n      if (!args[i].startsWith(\"-\")) {\n        break;\n      }\n      if (args[i].equals(\"--\")) {\n        i++;\n        break;\n      }\n      if (args[i].equals(\"-c\")) {\n        if (i + 1 == args.length) {\n          throw new IOException(\"-c <cmd> flag needs an argument\");\n        }\n        cmd = args[++i];\n      } else if (args[i].equals(\"-cpuprofile\")) {\n        if (i + 1 == args.length) {\n          throw new IOException(\"-cpuprofile <file> flag needs an argument\");\n        }\n        cpuprofile = args[++i];\n      } else {\n        throw new IOException(\"unknown flag: \" + args[i]);\n      }\n    }\n    // positional arguments\n    if (i < args.length) {\n      if (i + 1 < args.length) {\n        throw new IOException(\"too many positional arguments\");\n      }\n      file = args[i];\n    }","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/cmd/Main.java#L170-L206","documentation":"The standalone Starlark interpreter command (net.starlark.java.cmd.Main) accepts only -c, -cpuprofile and one optional script file. If '-c' is the last argument, no command string follows, so argument parsing fails with this IOException before any script runs.","triggerScenarios":"Running `starlark -c` with nothing after it; a shell script that builds the command line with an unset variable (`starlark -c $CMD` where CMD is empty and unquoted expansion drops it).","commonSituations":"Shell variable quoting mistakes; CI script passing an empty -c payload; copy-paste truncation of an example command.","solutions":["Supply the command string: `starlark -c 'print(1)'`.","Quote shell expansions so empty variables become explicit empty strings or are checked first.","Validate arguments before invoking the process from Java (check length after '-c')."],"exampleFix":"# before\nstarlark -c\n\n# after\nstarlark -c 'print(\"hello\")'","handlingStrategy":"validation","validationCode":"# Validate argv before launching the CLI\nif [ \"$#\" -lt 2 ] || [ \"$1\" != \"-c\" ]; then echo \"usage: $0 -c <cmd> [file]\" >&2; exit 2; fi\nexec starlark \"$@\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always quote shell variables passed as flag arguments.","Check required flag arguments before invoking subprocesses.","Print usage on bad arguments in wrapper scripts."],"tags":["starlark","cli","argument-parsing"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}