Python "argparse: error: unrecognized arguments" in CI
argparse prints error: unrecognized arguments: ... and exits with status 2 when the command line contains a flag or positional it does not define. In CI this aborts the job because exit 2 is non-zero.
What this error means
A CLI invocation fails with "usage: ... error: unrecognized arguments: --foo" and the step exits with code 2.
python
prog: error: unrecognized arguments: --new-flag valueCommon causes
The CI command passes a flag the parser version does not know
A workflow was updated to pass a new option, but the installed tool version predates it.
A subcommand argument placed before the subcommand
argparse with subparsers only accepts a subcommand-specific flag after the subcommand name.
How to fix it
Align the flags with the parser definition
- Run the command with
--helpto list the flags the installed version accepts. - Add the flag to the parser with add_argument, or remove it from the CI invocation.
- For subparsers, place subcommand flags after the subcommand name.
Python
parser.add_argument("--new-flag", help="...")How to prevent it
- Pin the CLI tool version so its accepted flags are stable.
- Keep the parser definition and the CI invocation in sync.
- Add a smoke test that runs the CLI with --help.
Related guides
Python "click.exceptions.UsageError" in CIFix "click.exceptions.UsageError" in CI - a Click CLI rejected its arguments (missing option, bad value, or n…
Python "subprocess.CalledProcessError: returned non-zero exit status" in CIFix "subprocess.CalledProcessError: Command returned non-zero exit status" in CI - a command run with check=T…
Python "yaml.scanner.ScannerError" in CIFix "yaml.scanner.ScannerError" in CI - PyYAML could not tokenize a YAML file, usually a tab character, bad i…