Repository navigation
Commit 2b85f6f
fold: parse the obsolete -WIDTH form the way GNU does (#14248)
* fold: parse the obsolete -WIDTH form the way GNU does
`handle_obsolete` scanned every argument for `-DIGITS` and pulled the
first match out, without checking whether that argument was already
spoken for. Five GNU incompatibilities followed:
fold -w -1 FILE reported FILE as the invalid width, not -1, because
-1 was taken as the obsolete form and -w then bound
to the file name. Same for -bw, -sw and --width.
fold -w3 -5 folded at 3; GNU folds at 5, since it reads options
left to right and the last width wins.
fold -3 -5 failed with "unexpected argument '-5'", as only the
first obsolete width was consumed.
fold -w 3 -w 5 failed with "cannot be used multiple times".
fold -- -3 read stdin, swallowing the width past the -- that
should have made it a file name.
Rewrite the obsolete form into --width=N in place instead, leaving the
order intact, and let clap pick the last one via args_override_self.
Arguments that are the value of -w/--width, and anything after --, are
no longer eligible.
The remaining difference on `fold -w 0` ("Result too large" against
GNU's "Numerical result out of range") is a separate wording issue and
is left alone.
* Apply suggestion from @sylvestre
* fold: compare find('w') against checked_sub directly
* fold: cover the bare - operand with a test
The underflow fixed in the previous commit had no test behind it. This
pins `-` as an operand in each position, including as the value of -w
where GNU rejects it as a width.
* fold: fix a false positive in the -w-takes-a-value check
`takes_width_value` decided a trailing `w` by comparing `find('w')` to
`len().checked_sub(1)`. For a bare `-` (short = ""), both sides are
`None` -- "no 'w' found" and "length underflowed" are different reasons,
but the comparison can't tell them apart, so it reported a match:
$ fold - -5 <<< hello
error: unexpected argument '-5' found # uutils, before
hello # GNU, and uutils after
That made the `-` operand swallow whatever followed it as a width value
instead of leaving it for the obsolete-width scan, so `fold - -5` failed
to parse where GNU folds normally. `ends_with('w')` is false on an empty
string, so it doesn't share the bug.
Found while investigating an unrelated CI failure in this PR (a broken
pipe in test_dash_operand_reads_stdin, which turned out to be a flaky
cross-compiled job, not this) -- this bug doesn't explain that failure,
but turned up real and worth fixing while looking at this function.
---------
Co-authored-by: arbelonson-source <[email protected]>
Co-authored-by: Sylvestre Ledru <[email protected]>1 parent 5f3aa7b commit 2b85f6f
2 files changed
Lines changed: 163 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 63 | + | |
67 | 64 | | |
68 | 65 | | |
69 | 66 | | |
| |||
105 | 102 | | |
106 | 103 | | |
107 | 104 | | |
| 105 | + | |
| 106 | + | |
108 | 107 | | |
109 | 108 | | |
110 | 109 | | |
| |||
143 | 142 | | |
144 | 143 | | |
145 | 144 | | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 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 | + | |
153 | 209 | | |
| 210 | + | |
154 | 211 | | |
155 | | - | |
| 212 | + | |
| 213 | + | |
156 | 214 | | |
157 | 215 | | |
158 | 216 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1110 | 1110 | | |
1111 | 1111 | | |
1112 | 1112 | | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
| 1202 | + | |
| 1203 | + | |
| 1204 | + | |
0 commit comments