When we call the program from the command line, Python provides several ways to handle the passed parameters.
So far, you have run programs from the REPL or using
python <filename>.py
When doing so, you can pass other parameters and options as follows:
python <filename>.py <argument1>
python <filename>.py <argument1> <argument2>
The basic way to deal with these parameters is to usesys
Modules in the standard library.
You can pass parameters tosys.argv
List:
import sys
print(len(sys.argv))
print(sys.argv)
Thissys.argv
list as the first item contains the name of the file that has been run, for example['main.py']
.
This is a simple method, but you have to do a lot of work. You need to verify the parameters to ensure that they are of the correct type, and if the user does not use the program correctly, you need to print feedback to the user.
Python provides another package in the standard library to help you:argparse
.
First you importargparse
Then you callargparse.ArgumentParser()
, Transfer program description:
import argparse
parser = argparse.ArgumentParser(
description=‘This program prints the name of my dogs’
)
Then, you continue to add the parameters you want to accept. For example, in this procedure, we accept-c
The options for passing colors are as follows:python program.py -c red
import argparse
parser = argparse.ArgumentParser(
description=‘This program prints a color HEX value’
)
parser.add_argument(’-c’, ‘–color’, metavar=‘color’, required=True, help=‘the color to search for’)
args = parser.parse_args()
print(args.color) # ‘red’
If no parameters are specified, the program will throw an error:
➜ python python program.py
usage: program.py [-h] -c color
program.py: error: the following arguments are required: -cYou can set an option to have a specific set of values, using choices
:
parser.add_argument('-c', '--color', metavar='color', required=True, choices={'red','yellow'}, help='the color to search for')
➜ python python program.py -c blue
usage: program.py [-h] -c color
program.py: error: argument -c/--color: invalid choice: 'blue' (choose from 'yellow', 'red')There are more options, but those are the basics.
And there are community packages that provide this functionality, too, like Click and Python Prompt Toolkit.
More python tutorials:
- Introduction to Python
- Installing Python 3 on macOS
- Running Python programs
- Python 2 vs Python 3
- The basics of working with Python
- Python Data Types
- Python Operators
- Python Strings
- Python Booleans
- Python Numbers
- Python, Accepting Input
- Python Control Statements
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python Functions
- Python Objects
- Python Loops
- Python Modules
- Python Classes
- The Python Standard Library
- Debugging Python
- Python variables scope
- Python, accept arguments from command line
- Python Recursion
- Python Nested Functions
- Python Lambda Functions
- Python Closures
- Python Virtual Environments
- Use a GoPro as a remote webcam using Python
- Python, how to create a list from a string
- Python Decorators
- Python Docstrings
- Python Introspection
- Python Annotations
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get the details of a file
- Python, how to check if a file or directory exists
- Python Exceptions
- Python, how to create a directory
- Python, how to create an empty file
- Python, the `with` statement
- Python, create a network request
- Python, installing 3rd party packages using `pip`
- Python, read the content of a file