
Python Command Line Interfaces with Argparse
Building Command Line Interfaces With `argparse`
Python’s argparse library is a powerful tool for creating command line interfaces (CLI) for your Python scripts. In this tutorial, we will explore the basics of argparse and how to use it to create robust and user-friendly command line interfaces.
What is argparse?
The argparse library is included in Python's standard library and provides a simple way to create command line interfaces for your scripts. It allows you to define the arguments that your script can accept and automatically generates help messages and usage information for your CLI.
Basic Usage of argparse
Let’s start by looking at the basic usage of argparse. Below is a simple example of a Python script that uses argparse to accept a single command line argument:
import argparse
def main():
parser = argparse.ArgumentParser(description='A simple CLI program')
parser.add_argument('number', type=int, help='An integer value')
args = parser.parse_args()
result = args.number * 2
print(f'The result is: {result}')
if __name__ == '__main__':
main()In this example, we import the argparse module and create an ArgumentParser object. We then define a single positional argument number, which is of type int. When the script is run with a number as the argument, it will print out the result of doubling that number.
To run the script, you can execute the following command in the terminal:
python script.py 5Advanced Usage of argparse
The argparse library also supports more advanced features such as optional arguments, flags, sub-commands, and custom argument types. Let's take a look at a more advanced example:
import argparse
def main():
parser = argparse.ArgumentParser(description='A program with advanced CLI options')
parser.add_argument('filename', help='The filename to process')
parser.add_argument('--verbose', '-v', action='store_true', help='Enable verbose mode')
parser.add_argument('--output', '-o', help='Specify the output file')
args = parser.parse_args()
print(f'Processing file: {args.filename}')
if args.verbose:
print('Verbose mode enabled')
if args.output:
print(f'Output file specified: {args.output}')
if __name__ == '__main__':
main()In this example, we define a positional argument for the filename, as well as two optional arguments for enabling verbose mode and specifying the output file. The action='store_true' parameter for the --verbose flag indicates that it is a boolean flag that does not require a value.
To run the script with different options, you can use commands like:
python script.py input.txt
python script.py input.txt --verbose
python script.py input.txt --output output.txtConclusion
In this tutorial, we’ve covered the basics of building command line interfaces with the argparse library in Python. We've seen how to define simple and advanced CLI options, allowing you to create robust and user-friendly command line tools for your Python scripts. With its rich feature set and ease of use, argparse is a valuable tool for any Python developer working with command line scripts.
