Python argparse ignore unrecognised arguments
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
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
Typical output is:
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:
Now this command works:
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 --:
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:
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, whileparse_known_args()returns them separately.' - Use
parse_known_args()for wrapper scripts and framework-injected arguments. - Use
argparse.REMAINDERand--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
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.