Remove options whose flag set is a subset of (or equal to) another's. Covers both strict-subset (e.g. {Z} vs {-M, Z}) and exact-match duplicates (e.g. two entries for {O}). When one option's combined flags (short ∪ long) are ⊆ another's, the smaller entry is removed. For **exact-m
(options: list[Option])
| 114 | |
| 115 | |
| 116 | def dedup_options(options: list[Option]) -> tuple[list[Option], int]: |
| 117 | """Remove options whose flag set is a subset of (or equal to) another's. |
| 118 | |
| 119 | Covers both strict-subset (e.g. {Z} vs {-M, Z}) and exact-match |
| 120 | duplicates (e.g. two entries for {O}). When one option's combined flags |
| 121 | (short ∪ long) are ⊆ another's, the smaller entry is removed. |
| 122 | |
| 123 | For **exact-match** duplicates the first occurrence's position is kept. |
| 124 | If a later duplicate has a longer description, the first entry is |
| 125 | replaced with the later entry entirely (all fields, not just text). |
| 126 | |
| 127 | For **strict subsets** the superset entry survives with its own flags. |
| 128 | If the subset has a longer description, the text and ``meta`` are |
| 129 | transferred so that provenance (e.g. ``meta["lines"]``) stays |
| 130 | consistent with the description text. |
| 131 | |
| 132 | When multiple supersets qualify, the **closest** one (smallest number |
| 133 | of extra flags) is chosen so the subset merges into the most specific |
| 134 | match rather than depending on input order. |
| 135 | |
| 136 | For **strict** subsets an additional cross-reference check is applied: |
| 137 | the subset's description must mention at least one of the superset's |
| 138 | extra flags. This prevents false merges where dashless-option |
| 139 | normalisation creates spurious flag overlaps (see ``_subset_has_cross_reference``). |
| 140 | |
| 141 | Returns (deduped_list, num_removed). |
| 142 | """ |
| 143 | flags_list: list[frozenset[str]] = [] |
| 144 | for opt in options: |
| 145 | if opt.short or opt.long: |
| 146 | flags_list.append(frozenset(opt.short + opt.long)) |
| 147 | else: |
| 148 | flags_list.append(frozenset()) |
| 149 | |
| 150 | removed: set[int] = set() |
| 151 | |
| 152 | # Pass 1: exact-match dedup. First occurrence's position wins; |
| 153 | # if a later duplicate has a longer description, replace entirely. |
| 154 | seen: dict[frozenset[str], int] = {} |
| 155 | for i in range(len(options)): |
| 156 | fi = flags_list[i] |
| 157 | if not fi: |
| 158 | continue |
| 159 | if fi in seen: |
| 160 | first = seen[fi] |
| 161 | if len(options[i].text) > len(options[first].text): |
| 162 | options[first] = options[i] |
| 163 | removed.add(i) |
| 164 | else: |
| 165 | seen[fi] = i |
| 166 | |
| 167 | # Pass 2: strict-subset dedup. For each surviving option, search |
| 168 | # ALL other surviving options (bidirectional) for the closest |
| 169 | # qualifying superset. |
| 170 | for i in range(len(options)): |
| 171 | if i in removed or not flags_list[i]: |
| 172 | continue |
| 173 | fi = flags_list[i] |