2013-07-09 12:40:59 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2013 Ejwa Software. All rights reserved.
|
|
|
|
#
|
|
|
|
# This file is part of gitinspector.
|
|
|
|
#
|
|
|
|
# gitinspector is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gitinspector is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-01-22 00:24:22 +01:00
|
|
|
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-09 12:40:59 +02:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2013-07-14 00:07:36 +02:00
|
|
|
import getopt
|
2013-07-09 12:40:59 +02:00
|
|
|
|
|
|
|
class InvalidOptionArgument(Exception):
|
2013-07-14 04:57:32 +02:00
|
|
|
def __init__(self, msg):
|
|
|
|
super(InvalidOptionArgument, self).__init__(msg)
|
|
|
|
self.msg = msg
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
def __find_arg_in_options__(arg, options):
|
|
|
|
for opt in options:
|
|
|
|
if opt[0].find(arg) == 0:
|
|
|
|
return opt
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
return None
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
def __find_options_to_extend__(long_options):
|
|
|
|
options_to_extend = []
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2013-07-14 04:47:34 +02:00
|
|
|
for num, arg in enumerate(long_options):
|
2013-07-14 00:07:36 +02:00
|
|
|
arg = arg.split(":")
|
|
|
|
if len(arg) == 2:
|
2013-07-14 04:47:34 +02:00
|
|
|
long_options[num] = arg[0] + "="
|
2013-07-14 00:07:36 +02:00
|
|
|
options_to_extend.append(("--" + arg[0], arg[1]))
|
2013-07-09 12:40:59 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
return options_to_extend
|
2013-07-10 03:11:15 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
# This is a duplicate of gnu_getopt, but with support for optional arguments in long options, in the form; "arg:default_value".
|
2013-07-10 03:11:15 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
def gnu_getopt(args, options, long_options):
|
|
|
|
options_to_extend = __find_options_to_extend__(long_options)
|
2013-07-12 03:36:03 +02:00
|
|
|
|
2013-07-14 04:47:34 +02:00
|
|
|
for num, arg in enumerate(args):
|
2013-07-14 00:07:36 +02:00
|
|
|
opt = __find_arg_in_options__(arg, options_to_extend)
|
|
|
|
if opt:
|
2013-07-14 04:47:34 +02:00
|
|
|
args[num] = arg + "=" + opt[1]
|
2013-07-12 03:36:03 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
return getopt.gnu_getopt(args, options, long_options)
|
2013-07-12 03:36:03 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
def get_boolean_argument(arg):
|
|
|
|
if isinstance(arg, bool):
|
|
|
|
return arg
|
|
|
|
elif arg == None or arg.lower() == "false" or arg.lower() == "f" or arg == "0":
|
|
|
|
return False
|
|
|
|
elif arg.lower() == "true" or arg.lower() == "t" or arg == "1":
|
|
|
|
return True
|
2013-07-12 03:36:03 +02:00
|
|
|
|
2013-07-14 00:07:36 +02:00
|
|
|
raise InvalidOptionArgument(_("The given option argument is not a valid boolean."))
|