What you're asking for is called argument parsing.
To do this the proper way, you should definitively use argparse.
It's a neat and yet very powerful library to make argument parsing more efficient. Plus, it makes your scripts manage arguments the proper Linux way, by default.
Basic example:
import argparse
parser = argparse.ArgumentParser(description='My argparse program')
parser.add_argument('--verbose',
action='store_true',
help='sets output to verbose' )
args = parser.parse_args()
if args.verbose:
print("~ Verbose!")
else:
print("~ Not so verbose")
Then you can do cool stuff like:
$ python3 myscript.py --verbose
~ Verbose!
And what's even cooler, it provides an automatic --help
(or -h
) argument:
$ python3 myscript.py --help
usage: myscript.py [-h] [--verbose]
My argparse program
optional arguments:
-h, --help show this help message and exit
--verbose sets output to verbose
This is the kind of library that allows you to quite easily do complicated stuff like:
./myscript.py --password=no -c -o --keep_moving --name="Robert"
Here is a link to a nice tutorial from which the above example has been freely adapted.