Command-line argument parsing is a common task in Python scripting where you pass parameters externally to a script to control its execution. Python's argparse
module is a powerful tool for creating user-friendly command-line interfaces.
The argparse
module makes it easy to write user-friendly command-line interfaces and allows the user to specify the inputs from the terminal.
-
Import the Module:
import argparse
-
Create a Parser: Create an
ArgumentParser
object which will hold all the information necessary to parse the command line into Python data types.parser = argparse.ArgumentParser(description='Example script.')
-
Add Arguments: Use the
add_argument
method to specify which command-line options the program is willing to accept.parser.add_argument('echo', help='echo the string you use here') parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')
-
Parsing Arguments: Call
parse_args()
to convert the args at the command line into an object with attributes.args = parser.parse_args()
-
Using the Arguments: Access the arguments using the dot notation.
if args.verbosity: print(f"Verbosity turned on. Input: {args.echo}")
Here's a simple script (example.py
) that echoes the user input and optionally increases verbosity:
import argparse
def main():
parser = argparse.ArgumentParser(description='Example script.')
parser.add_argument('echo', help='echo the string you use here')
parser.add_argument('--verbosity', help='increase output verbosity', action='store_true')
args = parser.parse_args()
if args.verbosity:
print(f"Verbosity turned on. Input: {args.echo}")
else:
print(args.echo)
if __name__ == '__main__':
main()
You can run this script from the command line like so:
python example.py hello --verbosity
- Meaningful Names: Choose argument names that are meaningful and easy to understand.
- Help Messages: Provide clear help messages for each argument.
- Default Values: Set sensible defaults where appropriate.
- Error Handling:
argparse
automatically generates error messages for invalid arguments, but you can also customize error handling as needed.
argparse
is a robust and flexible module for parsing command-line arguments in Python. It simplifies the process of creating command-line interfaces, making it easy to build scripts that are user-friendly and maintainable. By following best practices in defining and handling arguments, you can create scripts that effectively handle various input scenarios and provide a great user experience.