Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
python3

What is partial parsing in Python argparse module?

Adnan Abbas

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

argparse is a Python module that streamlines command-line interfaces by providing automation and easy usability. The program implementation defines what arguments it requires, and argparse parses the arguments out of sys.argv. The module also generates help, errors, and usage messages in case a user provides invalid arguments.

Creating a parser

First, we create an ArgumentParser object:

parser = argparse.ArgumentParser(description='Process some integers.')

The parser object in the above example will be responsible for parsing the command line arguments into Python data types.

Partial parsing

The Python argparse module provides you the flexibility to only parse a few of the command-line arguments. Here, the parse_known_args method is helpful. It works similarly to parse_args, except it does not report any errors when extra arguments are present.

Syntax

parse_known_args(args=None, namespace=None)

Parameters

  • args is the list of strings to parse. It is taken by default from sys.argv.
  • namespace is the object in which attributes are stored. By default, it is a new empty Namespace object.

Return value

A two-item tuple is returned, which contains the namespace object and the list of remaining arguments.

Example

parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='store_true')
parser.add_argument('bar')
parser.parse_known_args(['--foo', '--flag', 'BAR', 'rand'])

We create two arguments in the example above: --foo and bar.

When we call the parse_known_args method, we provide more than two arguments in the argument list.

The output of the following function call would be:

(Namespace(bar='BAR', foo=True), ['--flag', 'rand'])

Since we did not add --flag and rand as our program arguments, they are stored at the second index of the tuple returned from the parse_known_args function.

In a script, we won’t be calling the parse_known_args method with a list of strings. The arguments would be supplied by sys.argv from the command line.

RELATED TAGS

python
python3

CONTRIBUTOR

Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring