vendor/commandeer.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
import parseopt2 import strutils import strtabs var argumentList = newSeq[string]() shortOptions = newStringTable(modeCaseSensitive) longOptions = newStringTable(modeCaseSensitive) argumentIndex = 0 errorMsgs : seq[string] = @[] customErrorMsg : string inSubcommand = false subcommandSelected = false ## String conversion proc convert(s : string, t : char): char = result = s[0] proc convert(s : string, t : int): int = result = parseInt(s) proc convert(s : string, t : float): float = result = parseFloat(s) proc convert(s : string, t : bool): bool = ## will accept "yes", "true" as true values if s == "": ## the only way we get an empty string here is because of a key ## with no value, in which case the presence of the key is enough ## to return true result = true else: result = parseBool(s) proc convert(s : string, t : string): string = result = s.strip template argumentIMPL(identifier : untyped, t : typeDesc): untyped = var identifier : t if (inSubcommand and subcommandSelected) or not inSubcommand: if len(argumentList) <= argumentIndex: let eMsg = "Missing command line arguments" if len(errorMsgs) == 0: errorMsgs.add(eMsg) else: if not (errorMsgs[high(errorMsgs)][0] == 'M'): errorMsgs.add(eMsg) else: var typeVar : t try: identifier = convert(argumentList[argumentIndex], typeVar) except ValueError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for argument " & $(argumentIndex+1) errorMsgs.add(eMsg) inc(argumentIndex) template argumentsIMPL(identifier : untyped, t : typeDesc, atLeast1 : bool): untyped = var identifier = newSeq[t]() if (inSubcommand and subcommandSelected) or not inSubcommand: if len(argumentList) <= argumentIndex: if atLeast1: let eMsg = "Missing command line arguments" if len(errorMsgs) == 0: errorMsgs.add(eMsg) else: if not (errorMsgs[high(errorMsgs)][0] == 'M'): errorMsgs.add(eMsg) else: discard else: var typeVar : t var firstError = true while true: if len(argumentList) == argumentIndex: break try: let argument = argumentList[argumentIndex] inc(argumentIndex) identifier.add(convert(argument, typeVar)) firstError = false except ValueError: if atLeast1 and firstError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for argument " & $(argumentIndex+1) errorMsgs.add(eMsg) break template optionDefaultIMPL(identifier : untyped, t : typeDesc, longName : string, shortName : string, default : t): untyped = var identifier : t if (inSubcommand and subcommandSelected) or not inSubcommand: var typeVar : t if strtabs.hasKey(longOptions, longName): try: identifier = convert(longOptions[longName], typeVar) except ValueError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for option --" & longName errorMsgs.add(eMsg) elif strtabs.hasKey(shortOptions, shortName): try: identifier = convert(shortOptions[shortName], typeVar) except ValueError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for option -" & shortName errorMsgs.add(eMsg) else: #default values identifier = default template optionIMPL(identifier : untyped, t : typeDesc, longName : string, shortName : string): untyped = var identifier : t if (inSubcommand and subcommandSelected) or not inSubcommand: var typeVar : t if strtabs.hasKey(longOptions, longName): try: identifier = convert(longOptions[longName], typeVar) except ValueError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for option --" & longName errorMsgs.add(eMsg) elif strtabs.hasKey(shortOptions, shortName): try: identifier = convert(shortOptions[shortName], typeVar) except ValueError: let eMsg = capitalizeAscii(getCurrentExceptionMsg()) & " for option -" & shortName errorMsgs.add(eMsg) template exitoptionIMPL(longName, shortName, msg : string): untyped = if (inSubcommand and subcommandSelected) or not inSubcommand: if strtabs.hasKey(longOptions, longName): quit msg, QuitSuccess elif strtabs.hasKey(shortOptions, shortName): quit msg, QuitSuccess template errormsgIMPL(msg : string): untyped = if (inSubcommand and subcommandSelected) or not inSubcommand: customErrorMsg = msg template subcommandIMPL(identifier : untyped, subcommandName : string, stmts : untyped): untyped = var identifier : bool = false inSubcommand = true if len(argumentList) > 0 and argumentList[0] == subcommandName: identifier = true inc(argumentIndex) subcommandSelected = true stmts subcommandSelected = false inSubcommand = false template commandline*(statements : untyped): untyped = template argument(identifier : untyped, t : typeDesc): untyped = argumentIMPL(identifier, t) template arguments(identifier : untyped, t : typeDesc, atLeast1 : bool = true): untyped = argumentsIMPL(identifier, t, atLeast1) template option(identifier : untyped, t : typeDesc, longName : string, shortName : string, default : untyped): untyped = optionDefaultIMPL(identifier, t, longName, shortName, default) template option(identifier : untyped, t : typeDesc, longName : string, shortName : string): untyped = optionIMPL(identifier, t, longName, shortName) template exitoption(longName, shortName, msg : string): untyped = exitoptionIMPL(longName, shortName, msg) template errormsg(msg : string): untyped = errormsgIMPL(msg) template subcommand(identifier : untyped, subcommandName : string, stmts : untyped): untyped = subcommandIMPL(identifier, subcommandName, stmts) for kind, key, value in parseopt2.getopt(): case kind of parseopt2.cmdArgument: argumentList.add(key) of parseopt2.cmdLongOption: longOptions[key] = value of parseopt2.cmdShortOption: shortOptions[key] = value else: discard #Call the passed statements so that the above templates are called statements if len(errorMsgs) > 0: if not customErrorMsg.isNil: errorMsgs.add(customErrorMsg) quit join(errorMsgs, "\n") when isMainModule: import unittest test "convert() returns converted type value from strings": var intVar : int var floatVar : float var boolVar : bool var stringVar : string var charVar : char check convert("10", intVar) == 10 check convert("10.0", floatVar) == 10 check convert("10", floatVar) == 10 check convert("yes", boolVar) == true check convert("false", boolVar) == false check convert("no ", stringVar) == "no" check convert("*", charVar) == '*' |