argparse
Python
command-line
arguments
unrecognized

Python argparse ignore unrecognised arguments

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

By default, argparse is strict: if the command line contains an option your parser does not know, the program exits with an error. That is usually correct, but wrapper scripts, notebook environments, and pass-through CLIs often need to accept extra arguments without failing.

The normal behavior of parse_args

parse_args() expects every option to be declared in the parser. If an unknown flag appears, it stops execution and prints usage text.

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("--name")
5
6parser.parse_args(["--name", "Alice", "--verbose"])

That code exits with an error because --verbose was never registered.

Strict parsing is helpful when you want typos to be caught immediately. It is less helpful when your script needs to keep some arguments for another tool.

Use parse_known_args() to keep unknown values

The standard solution is parse_known_args(). It returns two values:

  • a namespace with recognized options
  • a list of everything left over
python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("--name")
5parser.add_argument("--output", default="result.txt")
6
7known, unknown = parser.parse_known_args(
8    ["--name", "Alice", "--verbose", "--count", "5"]
9)
10
11print(known)
12print(unknown)

Typical output is:

text
Namespace(name='Alice', output='result.txt')
['--verbose', '--count', '5']

This is the right tool when your script understands some flags itself and forwards the rest elsewhere.

A common wrapper-script pattern

Suppose a Python script handles local config but passes the remaining flags to pytest:

python
1import argparse
2import subprocess
3import sys
4
5parser = argparse.ArgumentParser()
6parser.add_argument("--config", required=True)
7
8args, passthrough = parser.parse_known_args()
9
10command = ["pytest"] + passthrough
11print("config:", args.config)
12print("running:", command)
13
14completed = subprocess.run(command)
15sys.exit(completed.returncode)

Now this command works:

bash
python run_tests.py --config test.ini -k login -q

Your script consumes --config, while -k login -q is forwarded untouched.

Use REMAINDER when the split should be explicit

Sometimes you do not want partial parsing at all after a certain point. In that case, use argparse.REMAINDER together with --:

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("--config")
5parser.add_argument("rest", nargs=argparse.REMAINDER)
6
7args = parser.parse_args(["--config", "app.ini", "--", "-v", "--debug"])
8print(args.config)
9print(args.rest)

This makes the boundary explicit. It is often cleaner than relying on unknown-argument capture when you are intentionally building a pass-through interface.

When this shows up in notebooks and frameworks

Some environments add their own command-line flags. Jupyter is a common example. If you call parse_args() in notebook code, the kernel arguments may trigger an error even though your own parser is fine.

In those situations, this pattern is safer:

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument("--epochs", type=int, default=10)
5
6args, _ = parser.parse_known_args()
7print(args.epochs)

The same idea applies when external tooling injects flags your script should ignore.

What to do with the unknown list

Unknown arguments are just strings. argparse does not interpret them for you after returning them.

That means you may need to:

  • forward them to another command
  • log them for debugging
  • parse them with another parser
  • reject them manually if certain patterns are unsafe

If you plan to pass them to a subprocess, keep shell=True out of the picture unless you have a strong reason and proper sanitization.

Common Pitfalls

The biggest pitfall is hiding user mistakes. With parse_known_args(), a typo like --naem does not raise an error if it lands in the unknown list. That can make bugs harder to spot.

Another issue is mixing pass-through arguments with positionals without tests. argparse may consume tokens differently than you expect when optional and positional parsing interact.

People also assume unknown flags become structured data automatically. They do not. You get a plain list of strings and must decide what to do next.

Finally, choose between parse_known_args() and REMAINDER intentionally. The first is flexible partial parsing. The second is explicit "everything after this point belongs to someone else."

Summary

  • 'parse_args() fails on unknown options, while parse_known_args() returns them separately.'
  • Use parse_known_args() for wrapper scripts and framework-injected arguments.
  • Use argparse.REMAINDER and -- when you want a clear pass-through boundary.
  • Unknown arguments remain raw strings, so you must handle them yourself.
  • Be careful: silent acceptance can hide misspelled options.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.