matchnum(s, signature, partial=False) Returns number of arguments matched in s against signature. Can be used to determine most-likely command for full or partial matches (partial applies to string matches).
(args: List[str],
signature: List[argdesc],
partial: bool = False)
| 1051 | |
| 1052 | |
| 1053 | def matchnum(args: List[str], |
| 1054 | signature: List[argdesc], |
| 1055 | partial: bool = False) -> int: |
| 1056 | """ |
| 1057 | matchnum(s, signature, partial=False) |
| 1058 | |
| 1059 | Returns number of arguments matched in s against signature. |
| 1060 | Can be used to determine most-likely command for full or partial |
| 1061 | matches (partial applies to string matches). |
| 1062 | """ |
| 1063 | words = args[:] |
| 1064 | mysig = copy.deepcopy(signature) |
| 1065 | matchcnt = 0 |
| 1066 | for desc in mysig: |
| 1067 | desc.numseen = 0 |
| 1068 | while desc.numseen < desc.n: |
| 1069 | # if there are no more arguments, return |
| 1070 | if not words: |
| 1071 | return matchcnt |
| 1072 | word = words.pop(0) |
| 1073 | |
| 1074 | try: |
| 1075 | # only allow partial matching if we're on the last supplied |
| 1076 | # word; avoid matching foo bar and foot bar just because |
| 1077 | # partial is set |
| 1078 | validate_one(word, desc, False, partial and (len(words) == 0)) |
| 1079 | valid = True |
| 1080 | except ArgumentError: |
| 1081 | # matchnum doesn't care about type of error |
| 1082 | valid = False |
| 1083 | |
| 1084 | if not valid: |
| 1085 | if not desc.req: |
| 1086 | # this wasn't required, so word may match the next desc |
| 1087 | words.insert(0, word) |
| 1088 | break |
| 1089 | else: |
| 1090 | # it was required, and didn't match, return |
| 1091 | return matchcnt |
| 1092 | if desc.req: |
| 1093 | matchcnt += 1 |
| 1094 | return matchcnt |
| 1095 | |
| 1096 | |
| 1097 | ValidatedArg = Union[bool, int, float, str, |
no test coverage detected