2015-10-07 07:35:11 +02:00
|
|
|
"""
|
2016-01-08 23:49:06 +01:00
|
|
|
Command line argument parsing for helper scripts.
|
2015-10-07 07:35:11 +02:00
|
|
|
"""
|
2016-03-12 20:09:28 +01:00
|
|
|
from __future__ import print_function
|
2016-01-08 23:49:06 +01:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
import getopt
|
|
|
|
import sys
|
2015-10-07 07:35:11 +02:00
|
|
|
from re import sub
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-07 07:35:11 +02:00
|
|
|
def parse(argv, options, long_options, usage):
|
2016-01-08 23:49:06 +01:00
|
|
|
"""Parse command line arguments.
|
|
|
|
|
|
|
|
:param list(str) argv: Arguments passed to the program.
|
|
|
|
:param str options: String of characters for allowed short options.
|
|
|
|
:param list(str) long_options: List of strings of allowed long options.
|
|
|
|
:param str usage: Help text, to print in the case of an error or when
|
|
|
|
the user asks for it.
|
|
|
|
:returns: dict
|
|
|
|
"""
|
2015-10-07 07:35:11 +02:00
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(argv, options, long_options)
|
|
|
|
except getopt.GetoptError:
|
2016-03-12 20:09:28 +01:00
|
|
|
print(usage)
|
2015-10-07 07:35:11 +02:00
|
|
|
sys.exit(2)
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-07 07:35:11 +02:00
|
|
|
return_arguments = {}
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt == '-h':
|
2016-03-12 20:09:28 +01:00
|
|
|
print(usage)
|
2015-10-07 07:35:11 +02:00
|
|
|
sys.exit()
|
|
|
|
else:
|
|
|
|
return_arguments[sub('^-+', '', opt)] = arg
|
2015-10-14 05:26:55 +02:00
|
|
|
|
2015-10-07 07:35:11 +02:00
|
|
|
return return_arguments
|