The answer is yes. A quick look at the argparse documentation would have answered as well.
Here is a very simple example, argparse is able to handle far more specific needs.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', '-f', help="a random options", type= str)
parser.add_argument('--bar', '-b', help="a more random option", type= int, default= 0)
print(parser.format_help())
# usage: test_args_4.py [-h] [--foo FOO] [--bar BAR]
#
# optional arguments:
# -h, --help show this help message and exit
# --foo FOO, -f FOO a random options
# --bar BAR, -b BAR a more random option
args = parser.parse_args("--foo pouet".split())
print(args) # Namespace(bar=0, foo='pouet')
print(args.foo) # pouet
print(args.bar) # 0
Off course, in a real script, you won't hard-code the command-line options and will call parser.parse_args()
(without argument) instead. It will make argparse take the sys.args
list as command-line arguments.
You will be able to call this script this way:
test_args_4.py -h # prints the help message
test_args_4.py -f pouet # foo="pouet", bar=0 (default value)
test_args_4.py -b 42 # foo=None, bar=42
test_args_4.py -b 77 -f knock # foo="knock", bar=77
You will discover a lot of other features by reading the doc ;)