|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# ============================================================================ |
| 5 | +# Clap - Command-line argument parser module |
| 6 | +# Copyright (C) 2018 by Ralf Kilian |
| 7 | +# Distributed under the MIT License (https://opensource.org/licenses/MIT) |
| 8 | +# |
| 9 | +# Website: http://www.urbanware.org |
| 10 | +# GitHub: https://github.com/urbanware-org/clap |
| 11 | +# ============================================================================ |
| 12 | + |
| 13 | +__version__ = "1.1.10" |
| 14 | + |
| 15 | + |
| 16 | +def get_version(): |
| 17 | + """ |
| 18 | + Return the version of this module. |
| 19 | + """ |
| 20 | + return __version__ |
| 21 | + |
| 22 | + |
| 23 | +class Parser(object): |
| 24 | + """ |
| 25 | + Project independent command-line argument parser class. |
| 26 | + """ |
| 27 | + __arg_grp_opt = None |
| 28 | + __arg_grp_req = None |
| 29 | + __arg_parser = None |
| 30 | + __is_argparser = False |
| 31 | + __conflict_handler = "resolve" # used by OptionParser, only |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + try: |
| 35 | + from argparse import ArgumentParser |
| 36 | + self.__arg_parser = ArgumentParser(add_help=False) |
| 37 | + self.__arg_grp_req = \ |
| 38 | + self.__arg_parser.add_argument_group("required arguments") |
| 39 | + self.__arg_grp_opt = \ |
| 40 | + self.__arg_parser.add_argument_group("optional arguments") |
| 41 | + self.__is_argparser = True |
| 42 | + return |
| 43 | + except ImportError: |
| 44 | + # Ignore the exception and proceed with the fallback |
| 45 | + pass |
| 46 | + |
| 47 | + try: |
| 48 | + from optparse import OptionParser |
| 49 | + self.__arg_parser = \ |
| 50 | + OptionParser(conflict_handler=self.__conflict_handler) |
| 51 | + self.__arg_grp_req = \ |
| 52 | + self.__arg_parser.add_option_group("Required arguments") |
| 53 | + self.__arg_grp_opt = \ |
| 54 | + self.__arg_parser.add_option_group("Optional arguments") |
| 55 | + return |
| 56 | + except ImportError: |
| 57 | + # This should never happen |
| 58 | + raise ImportError("Failed to initialize an argument parser.") |
| 59 | + |
| 60 | + def add_avalue(self, arg_short, arg_long, arg_help, arg_dest, arg_default, |
| 61 | + arg_required): |
| 62 | + """ |
| 63 | + Add an argument that expects a single user-defined value. |
| 64 | + """ |
| 65 | + if arg_required: |
| 66 | + obj = self.__arg_grp_req |
| 67 | + else: |
| 68 | + obj = self.__arg_grp_opt |
| 69 | + |
| 70 | + if arg_default is not None: |
| 71 | + # Enclose the value with quotes in case it is not an integer |
| 72 | + quotes = "'" |
| 73 | + try: |
| 74 | + arg_default = int(arg_default) |
| 75 | + quotes = "" |
| 76 | + except ValueError: |
| 77 | + pass |
| 78 | + |
| 79 | + if arg_help.strip().endswith(")"): |
| 80 | + arg_help = arg_help.rstrip(")") |
| 81 | + arg_help += ", default is %s%s%s)" % \ |
| 82 | + (quotes, str(arg_default), quotes) |
| 83 | + else: |
| 84 | + arg_help += " (default is %s%s%s)" % \ |
| 85 | + (quotes, str(arg_default), quotes) |
| 86 | + |
| 87 | + if self.__is_argparser: |
| 88 | + if arg_short is None: |
| 89 | + obj.add_argument(arg_long, help=arg_help, dest=arg_dest, |
| 90 | + default=arg_default, required=arg_required) |
| 91 | + else: |
| 92 | + obj.add_argument(arg_short, arg_long, help=arg_help, |
| 93 | + dest=arg_dest, default=arg_default, |
| 94 | + required=arg_required) |
| 95 | + else: |
| 96 | + if arg_short is None: |
| 97 | + obj.add_option(arg_long, help=arg_help, dest=arg_dest, |
| 98 | + default=arg_default) |
| 99 | + else: |
| 100 | + obj.add_option(arg_short, arg_long, help=arg_help, |
| 101 | + dest=arg_dest, default=arg_default) |
| 102 | + |
| 103 | + def add_predef(self, arg_short, arg_long, arg_help, arg_dest, arg_choices, |
| 104 | + arg_required): |
| 105 | + """ |
| 106 | + Add an argument that expects a certain predefined value. |
| 107 | + """ |
| 108 | + if arg_required: |
| 109 | + obj = self.__arg_grp_req |
| 110 | + else: |
| 111 | + obj = self.__arg_grp_opt |
| 112 | + |
| 113 | + if self.__is_argparser: |
| 114 | + if arg_short is None: |
| 115 | + obj.add_argument(arg_long, help=arg_help, dest=arg_dest, |
| 116 | + choices=arg_choices, required=arg_required) |
| 117 | + else: |
| 118 | + obj.add_argument(arg_short, arg_long, help=arg_help, |
| 119 | + dest=arg_dest, choices=arg_choices, |
| 120 | + required=arg_required) |
| 121 | + else: |
| 122 | + if arg_short is None: |
| 123 | + obj.add_option(arg_long, help=arg_help, dest=arg_dest, |
| 124 | + choices=arg_choices) |
| 125 | + else: |
| 126 | + # The OptionParser does not print the values to choose from, |
| 127 | + # so these have to be added manually to the description of |
| 128 | + # the argument first |
| 129 | + arg_help += " (choose from " |
| 130 | + for item in arg_choices: |
| 131 | + arg_help += "'%s', " % item |
| 132 | + arg_help = arg_help.rstrip(", ") + ")" |
| 133 | + |
| 134 | + obj.add_option(arg_short, arg_long, help=arg_help, |
| 135 | + dest=arg_dest) |
| 136 | + |
| 137 | + def add_switch(self, arg_short, arg_long, arg_help, arg_dest, arg_store, |
| 138 | + arg_required): |
| 139 | + """ |
| 140 | + Add an argument that does not expect anything, but returns a |
| 141 | + boolean value. |
| 142 | + """ |
| 143 | + if arg_required: |
| 144 | + obj = self.__arg_grp_req |
| 145 | + else: |
| 146 | + obj = self.__arg_grp_opt |
| 147 | + |
| 148 | + if arg_store: |
| 149 | + arg_store = "store_true" |
| 150 | + else: |
| 151 | + arg_store = "store_false" |
| 152 | + |
| 153 | + if self.__is_argparser: |
| 154 | + if arg_short is None: |
| 155 | + obj.add_argument(arg_long, help=arg_help, dest=arg_dest, |
| 156 | + action=arg_store, required=arg_required) |
| 157 | + else: |
| 158 | + obj.add_argument(arg_short, arg_long, help=arg_help, |
| 159 | + dest=arg_dest, action=arg_store, |
| 160 | + required=arg_required) |
| 161 | + else: |
| 162 | + if arg_short is None: |
| 163 | + obj.add_option(arg_long, help=arg_help, dest=arg_dest, |
| 164 | + action=arg_store) |
| 165 | + else: |
| 166 | + obj.add_option(arg_short, arg_long, help=arg_help, |
| 167 | + dest=arg_dest, action=arg_store) |
| 168 | + |
| 169 | + def dependency(self, arg_name, arg_value, dependency): |
| 170 | + """ |
| 171 | + Check the dependency of a command-line argument. |
| 172 | + """ |
| 173 | + if dependency is not None: |
| 174 | + if arg_value is None or str(arg_value) == "": |
| 175 | + raise Exception("The '%s' argument depends on %s'." % |
| 176 | + (arg_name, dependency)) |
| 177 | + |
| 178 | + def error(self, obj): |
| 179 | + """ |
| 180 | + Raise an error and cause the argument parser to print the error |
| 181 | + message. |
| 182 | + """ |
| 183 | + if type(obj) == str: |
| 184 | + obj = obj.strip() |
| 185 | + |
| 186 | + self.__arg_parser.error(obj) |
| 187 | + |
| 188 | + def parse_args(self): |
| 189 | + """ |
| 190 | + Parse and return the command-line arguments. |
| 191 | + """ |
| 192 | + if self.__is_argparser: |
| 193 | + args = self.__arg_parser.parse_args() |
| 194 | + else: |
| 195 | + (args, values) = self.__arg_parser.parse_args() |
| 196 | + return args |
| 197 | + |
| 198 | + def print_help(self): |
| 199 | + """ |
| 200 | + Print the usage, description, argument details and epilog. |
| 201 | + """ |
| 202 | + self.__arg_parser.print_help() |
| 203 | + |
| 204 | + def set_description(self, string): |
| 205 | + """ |
| 206 | + Set the description text. |
| 207 | + """ |
| 208 | + self.__arg_parser.description = string.strip() |
| 209 | + |
| 210 | + def set_epilog(self, string): |
| 211 | + """ |
| 212 | + Set the epilog text. |
| 213 | + """ |
| 214 | + self.__arg_parser.epilog = string.strip() |
| 215 | + |
| 216 | +# EOF |
0 commit comments