| 80 | def display_version_and_quit(self) : |
| 81 | """Displays version number, then exists successfully.""" |
| 82 | print version.__version__ |
| 83 | sys.exit(0) |
| 84 | |
| 85 | def display_usage_and_quit(self) : |
| 86 | """Displays command line usage, then exists successfully.""" |
| 87 | print self.documentation |
| 88 | sys.exit(0) |
| 89 | |
| 90 | def parseCommandline(self, argv, short, long) : |
| 91 | """Parses the command line, controlling options.""" |
| 92 | # split options in two lists: those which need an argument, those which don't need any |
| 93 | withoutarg = [] |
| 94 | witharg = [] |
| 95 | lgs = len(short) |
| 96 | i = 0 |
| 97 | while i < lgs : |
| 98 | ii = i + 1 |
| 99 | if (ii < lgs) and (short[ii] == ':') : |
| 100 | # needs an argument |
| 101 | witharg.append(short[i]) |
| 102 | ii = ii + 1 # skip the ':' |
| 103 | else : |
| 104 | # doesn't need an argument |
| 105 | withoutarg.append(short[i]) |
| 106 | i = ii |
| 107 | |
| 108 | for option in long : |
| 109 | if option[-1] == '=' : |
| 110 | # needs an argument |
| 111 | witharg.append(option[:-1]) |
| 112 | else : |
| 113 | # doesn't need an argument |
| 114 | withoutarg.append(option) |
| 115 | |
| 116 | # we begin with all possible options unset |
| 117 | parsed = {} |
| 118 | for option in withoutarg + witharg : |
| 119 | parsed[option] = None |
| 120 | |
| 121 | # then we parse the command line |
| 122 | args = [] # to not break if something unexpected happened |
| 123 | try : |
| 124 | options, args = getopt.getopt(argv, short, long) |
| 125 | if options : |
| 126 | for (o, v) in options : |
| 127 | # we skip the '-' chars |
| 128 | lgo = len(o) |
| 129 | i = 0 |
| 130 | while (i < lgo) and (o[i] == '-') : |
| 131 | i = i + 1 |
| 132 | o = o[i:] |
| 133 | if o in witharg : |
| 134 | # needs an argument : set it |
| 135 | parsed[o] = v |
| 136 | elif o in withoutarg : |
| 137 | # doesn't need an argument : boolean |
| 138 | parsed[o] = 1 |
| 139 | else : |
| 140 | # should never occur |
| 141 | raise PyKotaToolError, "Unexpected problem when parsing command line" |
| 142 | elif (not args) and sys.stdin.isatty() : # no option and no argument, we display help if we are a tty |
| 143 | self.display_usage_and_quit() |
| 144 | except getopt.error, msg : |
| 145 | sys.stderr.write("%s\n" % msg) |
| 146 | sys.stderr.flush() |
| 147 | self.display_usage_and_quit() |
| 148 | return (parsed, args) |
| 149 | |