ordigi/elodie/arguments.py

36 lines
936 B
Python
Raw Normal View History

"""
2016-01-08 23:49:06 +01:00
Command line argument parsing for helper scripts.
"""
2016-03-12 20:09:28 +01:00
from __future__ import print_function
2016-01-08 23:49:06 +01:00
import getopt
import sys
from re import sub
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
"""
try:
opts, args = getopt.getopt(argv, options, long_options)
except getopt.GetoptError:
2016-03-12 20:09:28 +01:00
print(usage)
sys.exit(2)
return_arguments = {}
for opt, arg in opts:
if opt == '-h':
2016-03-12 20:09:28 +01:00
print(usage)
sys.exit()
else:
return_arguments[sub('^-+', '', opt)] = arg
return return_arguments