validate(args, signature, flags=0, partial=False) args is a list of strings representing a possible command input following format of signature. Runs a validation; no exception means it's OK. Return a dict containing all arguments keyed by their descriptor name, with duplicat
(args: List[str],
signature: Sequence[argdesc],
flags: int = 0,
partial: Optional[bool] = False)
| 1130 | |
| 1131 | |
| 1132 | def validate(args: List[str], |
| 1133 | signature: Sequence[argdesc], |
| 1134 | flags: int = 0, |
| 1135 | partial: Optional[bool] = False) -> ValidatedArgs: |
| 1136 | """ |
| 1137 | validate(args, signature, flags=0, partial=False) |
| 1138 | |
| 1139 | args is a list of strings representing a possible |
| 1140 | command input following format of signature. Runs a validation; no |
| 1141 | exception means it's OK. Return a dict containing all arguments keyed |
| 1142 | by their descriptor name, with duplicate args per name accumulated |
| 1143 | into a list (or space-separated value for CephPrefix). |
| 1144 | |
| 1145 | Mismatches of prefix are non-fatal, as this probably just means the |
| 1146 | search hasn't hit the correct command. Mismatches of non-prefix |
| 1147 | arguments are treated as fatal, and an exception raised. |
| 1148 | |
| 1149 | This matching is modified if partial is set: allow partial matching |
| 1150 | (with partial dict returned); in this case, there are no exceptions |
| 1151 | raised. |
| 1152 | """ |
| 1153 | |
| 1154 | myargs = copy.deepcopy(args) |
| 1155 | mysig = copy.deepcopy(signature) |
| 1156 | reqsiglen = len([desc for desc in mysig if desc.req]) |
| 1157 | matchcnt = 0 |
| 1158 | d: ValidatedArgs = dict() |
| 1159 | save_exception = None |
| 1160 | |
| 1161 | arg_descs_by_name: Dict[str, argdesc] = \ |
| 1162 | dict((desc.name, desc) for desc in mysig if desc.t != CephPrefix) |
| 1163 | |
| 1164 | # Special case: detect "injectargs" (legacy way of modifying daemon |
| 1165 | # configs) and permit "--" string arguments if so. |
| 1166 | injectargs = myargs and myargs[0] == "injectargs" |
| 1167 | |
| 1168 | # Make a pass through all arguments |
| 1169 | for desc in mysig: |
| 1170 | desc.numseen = 0 |
| 1171 | |
| 1172 | while desc.numseen < desc.n: |
| 1173 | if myargs: |
| 1174 | myarg: Optional[str] = myargs.pop(0) |
| 1175 | else: |
| 1176 | myarg = None |
| 1177 | |
| 1178 | # no arg, but not required? Continue consuming mysig |
| 1179 | # in case there are later required args |
| 1180 | if myarg in (None, []): |
| 1181 | if not desc.req: |
| 1182 | break |
| 1183 | # did we already get this argument (as a named arg, earlier?) |
| 1184 | if desc.name in d: |
| 1185 | break |
| 1186 | |
| 1187 | # A keyword argument? |
| 1188 | if myarg: |
| 1189 | # argdesc for the keyword argument, if we find one |