| 1 |
<!-- spell-checker:ignore numfmt OPTS ariadne mydir mypipe mydev ucase iflag oflag nocreat notrunc fdatasync noatime noctty --> |
| 2 |
<!-- markdownlint-disable MD033 --> |
| 3 |
|
| 4 |
# Error diagnostics |
| 5 |
|
| 6 |
GNU coreutils reports every error as a single line on stderr. That line |
| 7 |
answers *what* went wrong, but Not *where*. For utilities whose arguments form |
| 8 |
a small language of their own - a `test` expression, a `chmod` mode, a `sort` |
| 9 |
key - the interesting question is usually which argument, or which character |
| 10 |
inside an argument, broke the parse. |
| 11 |
|
| 12 |
When stderr is a terminal, uutils renders these errors as a compiler-style |
| 13 |
report instead: the command line is echoed back as a source line, and a caret |
| 14 |
points at the part that is at fault, often with a line of advice. Everywhere |
| 15 |
else - a script, a pipe, a test harness - the plain one-line message is kept, |
| 16 |
so nothing that reads stderr can tell the difference. |
| 17 |
|
| 18 |
<link rel="stylesheet" href="extensions-errors.css"> |
| 19 |
|
| 20 |
## Before and after |
| 21 |
|
| 22 |
Each utility below shows the plain one-line message first - what uutils |
| 23 |
prints when stderr is not a terminal, and in most cases exactly what GNU |
| 24 |
prints - then the report rendered on a terminal, colors included. |
| 25 |
|
| 26 |
### `test` |
| 27 |
|
| 28 |
Before: |
| 29 |
|
| 30 |
<pre class="diag"><span class="a-p">$</span> test 7 -eq zap |
| 31 |
test: invalid integer 'zap'</pre> |
| 32 |
|
| 33 |
After: |
| 34 |
|
| 35 |
<pre class="diag"><span class="a-p">$</span> test 7 -eq zap |
| 36 |
test: invalid integer 'zap' |
| 37 |
<span class="a-d">╭─[</span> test:1:7 <span class="a-d">]</span> |
| 38 |
<span class="a-d">│</span> |
| 39 |
<span class="a-d">1 │</span> <span class="a-s">7 -eq </span><span class="a-e">zap</span> |
| 40 |
<span class="a-f"> │</span> <span class="a-e">───</span> |
| 41 |
<span class="a-f"> │</span> |
| 42 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: -eq, -ne, -lt, -le, -gt and -ge compare integers; use =, !=, < or > to compare strings |
| 43 |
<span class="a-f"> │</span> -eq equal, -ne not equal, -lt less than, -le less than or equal, -gt greater than, -ge greater than or equal |
| 44 |
<span class="a-d">───╯</span></pre> |
| 45 |
|
| 46 |
[Try it in the playground](https://uutils.org/playground/?cmd=test+7+-eq+zap). |
| 47 |
|
| 48 |
### `cut` |
| 49 |
|
| 50 |
A list of ranges is often long, and only one item in it is wrong: |
| 51 |
|
| 52 |
Before: |
| 53 |
|
| 54 |
<pre class="diag"><span class="a-p">$</span> cut -f 1,4-2,9-12 notes.txt |
| 55 |
cut: invalid decreasing range</pre> |
| 56 |
|
| 57 |
After: |
| 58 |
|
| 59 |
<pre class="diag"><span class="a-p">$</span> cut -f 1,4-2,9-12 notes.txt |
| 60 |
cut: invalid decreasing range |
| 61 |
<span class="a-d">╭─[</span> cut:1:10 <span class="a-d">]</span> |
| 62 |
<span class="a-d">│</span> |
| 63 |
<span class="a-d">1 │</span> <span class="a-s">cut -f 1,</span><span class="a-e">4-2</span><span class="a-s">,9-12 notes.txt</span> |
| 64 |
<span class="a-f"> │</span> <span class="a-e">─┬─</span> |
| 65 |
<span class="a-f"> │</span> <span class="a-e">╰───</span> this range ends before it starts |
| 66 |
<span class="a-f"> │</span> |
| 67 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a list is N, N-M, N- or -M, separated by commas, as in -f1,4-6,9- |
| 68 |
<span class="a-d">───╯</span></pre> |
| 69 |
|
| 70 |
### `expr` |
| 71 |
|
| 72 |
Before: |
| 73 |
|
| 74 |
<pre class="diag"><span class="a-p">$</span> expr 9 + foo |
| 75 |
expr: non-integer argument</pre> |
| 76 |
|
| 77 |
After: |
| 78 |
|
| 79 |
<pre class="diag"><span class="a-p">$</span> expr 9 + foo |
| 80 |
expr: non-integer argument |
| 81 |
<span class="a-d">╭─[</span> expr:1:5 <span class="a-d">]</span> |
| 82 |
<span class="a-d">│</span> |
| 83 |
<span class="a-d">1 │</span> <span class="a-s">9 + </span><span class="a-e">foo</span> |
| 84 |
<span class="a-f"> │</span> <span class="a-e">───</span> |
| 85 |
<span class="a-f"> │</span> |
| 86 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: arithmetic operators need integers; use = or != to compare strings instead |
| 87 |
<span class="a-d">───╯</span></pre> |
| 88 |
|
| 89 |
[Try it in the playground](https://uutils.org/playground/?cmd=expr+9+%2B+foo). |
| 90 |
|
| 91 |
### `chmod` |
| 92 |
|
| 93 |
The caret can point *inside* an argument, at the exact character that broke |
| 94 |
the parse: |
| 95 |
|
| 96 |
Before: |
| 97 |
|
| 98 |
<pre class="diag"><span class="a-p">$</span> chmod 'g+rw?x' notes.txt |
| 99 |
chmod: invalid operator (expected +, -, or =, but found ?)</pre> |
| 100 |
|
| 101 |
After: |
| 102 |
|
| 103 |
<pre class="diag"><span class="a-p">$</span> chmod 'g+rw?x' notes.txt |
| 104 |
chmod: invalid operator (expected +, -, or =, but found ?) |
| 105 |
<span class="a-d">╭─[</span> chmod:1:5 <span class="a-d">]</span> |
| 106 |
<span class="a-d">│</span> |
| 107 |
<span class="a-d">1 │</span> <span class="a-s">g+rw</span><span class="a-e">?</span><span class="a-s">x notes.txt</span> |
| 108 |
<span class="a-f"> │</span> <span class="a-e">─</span> |
| 109 |
<span class="a-f"> │</span> |
| 110 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a mode is either octal, as in 644, or clauses such as u+rwx,go-w |
| 111 |
<span class="a-d">───╯</span></pre> |
| 112 |
|
| 113 |
### `tr` |
| 114 |
|
| 115 |
Before: |
| 116 |
|
| 117 |
<pre class="diag"><span class="a-p">$</span> tr 'qw[y-b]' x |
| 118 |
tr: range-endpoints of 'y-b' are in reverse collating sequence order</pre> |
| 119 |
|
| 120 |
After: |
| 121 |
|
| 122 |
<pre class="diag"><span class="a-p">$</span> tr 'qw[y-b]' x |
| 123 |
tr: range-endpoints of 'y-b' are in reverse collating sequence order |
| 124 |
<span class="a-d">╭─[</span> tr:1:7 <span class="a-d">]</span> |
| 125 |
<span class="a-d">│</span> |
| 126 |
<span class="a-d">1 │</span> <span class="a-s">tr qw[</span><span class="a-e">y-b</span><span class="a-s">] x</span> |
| 127 |
<span class="a-f"> │</span> <span class="a-e">─┬─</span> |
| 128 |
<span class="a-f"> │</span> <span class="a-e">╰───</span> did you mean 'b-y'? |
| 129 |
<span class="a-f"> │</span> |
| 130 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a range goes from the lower character to the higher one, as in a-z |
| 131 |
<span class="a-d">───╯</span></pre> |
| 132 |
|
| 133 |
[Try it in the playground](https://uutils.org/playground/?cmd=tr+%27qw%5By-b%5D%27+x). |
| 134 |
|
| 135 |
### `csplit` |
| 136 |
|
| 137 |
A pattern is a small language of its own, and the regex engine already knows |
| 138 |
which character of it it choked on - so the caret can say so too: |
| 139 |
|
| 140 |
Before: |
| 141 |
|
| 142 |
<pre class="diag"><span class="a-p">$</span> csplit notes.txt '/a{2,1}/' |
| 143 |
csplit: '/a{2,1}/': invalid pattern</pre> |
| 144 |
|
| 145 |
After: |
| 146 |
|
| 147 |
<pre class="diag"><span class="a-p">$</span> csplit notes.txt '/a{2,1}/' |
| 148 |
csplit: '/a{2,1}/': invalid pattern |
| 149 |
<span class="a-d">╭─[</span> csplit:1:20 <span class="a-d">]</span> |
| 150 |
<span class="a-d">│</span> |
| 151 |
<span class="a-d">1 │</span> <span class="a-s">csplit notes.txt /a</span><span class="a-e">{2,1}</span><span class="a-s">/</span> |
| 152 |
<span class="a-f"> │</span> <span class="a-e">──┬──</span> |
| 153 |
<span class="a-f"> │</span> <span class="a-e">╰──── </span>invalid repetition count range, the start must be <= the end |
| 154 |
<span class="a-f"> │</span> |
| 155 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a pattern is a line number N, /REGEXP/[OFFSET] or %REGEXP%[OFFSET], each optionally followed by {N} or {*} |
| 156 |
<span class="a-d">───╯</span></pre> |
| 157 |
|
| 158 |
The label is quoted from the regex engine rather than translated, since that |
| 159 |
is the only place the wording exists. |
| 160 |
|
| 161 |
### `sort` |
| 162 |
|
| 163 |
Before: |
| 164 |
|
| 165 |
<pre class="diag"><span class="a-p">$</span> sort -k2.3x notes.txt |
| 166 |
sort: stray character in field spec: invalid field specification '2.3x'</pre> |
| 167 |
|
| 168 |
After: |
| 169 |
|
| 170 |
<pre class="diag"><span class="a-p">$</span> sort -k2.3x notes.txt |
| 171 |
sort: stray character in field spec: invalid field specification '2.3x' |
| 172 |
<span class="a-d">╭─[</span> sort:1:11 <span class="a-d">]</span> |
| 173 |
<span class="a-d">│</span> |
| 174 |
<span class="a-d">1 │</span> <span class="a-s">sort -k2.3</span><span class="a-e">x</span><span class="a-s"> notes.txt</span> |
| 175 |
<span class="a-f"> │</span> <span class="a-e">─</span> |
| 176 |
<span class="a-f"> │</span> |
| 177 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a key is FIELD[.CHAR][OPTS][,FIELD[.CHAR][OPTS]], as in -k2.3,4nr |
| 178 |
<span class="a-d">───╯</span></pre> |
| 179 |
|
| 180 |
[Try it in the playground](https://uutils.org/playground/?cmd=sort+-k2.3x+fruits.txt). |
| 181 |
|
| 182 |
### `numfmt` |
| 183 |
|
| 184 |
Before: |
| 185 |
|
| 186 |
<pre class="diag"><span class="a-p">$</span> numfmt --format=%q 1000 |
| 187 |
numfmt: invalid format '%q', directive must be %[0]['][-][N][.][N]f</pre> |
| 188 |
|
| 189 |
After: |
| 190 |
|
| 191 |
<pre class="diag"><span class="a-p">$</span> numfmt --format=%q 1000 |
| 192 |
numfmt: invalid format '%q', directive must be %[0]['][-][N][.][N]f |
| 193 |
<span class="a-d">╭─[</span> numfmt:1:18 <span class="a-d">]</span> |
| 194 |
<span class="a-d">│</span> |
| 195 |
<span class="a-d">1 │</span> <span class="a-s">numfmt --format=%</span><span class="a-e">q</span><span class="a-s"> 1000</span> |
| 196 |
<span class="a-f"> │</span> <span class="a-e">┬</span> |
| 197 |
<span class="a-f"> │</span> <span class="a-e">╰──</span> f is the only conversion numfmt has; %d, %e, %g and the other C conversions are not accepted |
| 198 |
<span class="a-f"> │</span> |
| 199 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a format is [PREFIX]%[0]['][-][WIDTH][.PRECISION]f[SUFFIX], as in "%'-10.2f" |
| 200 |
<span class="a-d">───╯</span></pre> |
| 201 |
|
| 202 |
[Try it in the playground](https://uutils.org/playground/?cmd=numfmt+--format%3D%25q+1000). |
| 203 |
|
| 204 |
### `printf` |
| 205 |
|
| 206 |
Before: |
| 207 |
|
| 208 |
<pre class="diag"><span class="a-p">$</span> printf %5.2c q |
| 209 |
printf: %5.2c: invalid conversion specification</pre> |
| 210 |
|
| 211 |
After: |
| 212 |
|
| 213 |
<pre class="diag"><span class="a-p">$</span> printf %5.2c q |
| 214 |
printf: %5.2c: invalid conversion specification |
| 215 |
<span class="a-d">╭─[</span> printf:1:8 <span class="a-d">]</span> |
| 216 |
<span class="a-d">│</span> |
| 217 |
<span class="a-d">1 │</span> <span class="a-s">printf </span><span class="a-e">%5.2c</span><span class="a-s"> q</span> |
| 218 |
<span class="a-f"> │</span> <span class="a-e">─────</span> |
| 219 |
<span class="a-f"> │</span> |
| 220 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: %d, %s, %x, %f and the other C conversions are accepted, plus %b and %q; a literal % is written %% |
| 221 |
<span class="a-d">───╯</span></pre> |
| 222 |
|
| 223 |
The same goes for a broken escape: `printf 'a\xzb'` puts the caret under the |
| 224 |
`\x` that is missing its hexadecimal digits. |
| 225 |
|
| 226 |
[Try it in the playground](https://uutils.org/playground/?cmd=printf+%255.2c+q). |
| 227 |
|
| 228 |
### `env` |
| 229 |
|
| 230 |
`env -S` takes a whole command line and splits it as a shell would. Its |
| 231 |
messages name an offset, which is precisely the thing a caret can show |
| 232 |
instead: |
| 233 |
|
| 234 |
Before: |
| 235 |
|
| 236 |
<pre class="diag"><span class="a-p">$</span> env -S 'echo ${1FOO}' |
| 237 |
env: only ${VARNAME} expansion is supported, error at: ${1FOO}</pre> |
| 238 |
|
| 239 |
After: |
| 240 |
|
| 241 |
<pre class="diag"><span class="a-p">$</span> env -S 'echo ${1FOO}' |
| 242 |
env: only ${VARNAME} expansion is supported, error at: ${1FOO} |
| 243 |
<span class="a-d">╭─[</span> env:1:14 <span class="a-d">]</span> |
| 244 |
<span class="a-d">│</span> |
| 245 |
<span class="a-d">1 │</span> <span class="a-s">env -S 'echo </span><span class="a-e">${1</span><span class="a-s">FOO}'</span> |
| 246 |
<span class="a-f"> │</span> <span class="a-e">─┬─</span> |
| 247 |
<span class="a-f"> │</span> <span class="a-e">╰───</span> a variable name cannot start with a digit |
| 248 |
<span class="a-f"> │</span> |
| 249 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: only $NAME and ${NAME} are expanded; the other shell forms are not |
| 250 |
<span class="a-d">───╯</span></pre> |
| 251 |
|
| 252 |
Note that the `-S` string holds spaces, so it is echoed back quoted - and the |
| 253 |
caret still points inside it. |
| 254 |
|
| 255 |
### `head` |
| 256 |
|
| 257 |
A SIZE is a number and a unit, and the caret says which of the two was |
| 258 |
rejected: |
| 259 |
|
| 260 |
Before: |
| 261 |
|
| 262 |
<pre class="diag"><span class="a-p">$</span> head -c 1fb notes.txt |
| 263 |
head: invalid number of bytes: '1fb'</pre> |
| 264 |
|
| 265 |
After: |
| 266 |
|
| 267 |
<pre class="diag"><span class="a-p">$</span> head -c 1fb notes.txt |
| 268 |
head: invalid number of bytes: '1fb' |
| 269 |
<span class="a-d">╭─[</span> head:1:10 <span class="a-d">]</span> |
| 270 |
<span class="a-d">│</span> |
| 271 |
<span class="a-d">1 │</span> <span class="a-s">head -c 1</span><span class="a-e">fb</span><span class="a-s"> notes.txt</span> |
| 272 |
<span class="a-f"> │</span> <span class="a-e">─┬</span> |
| 273 |
<span class="a-f"> │</span> <span class="a-e">╰──</span> not a known unit |
| 274 |
<span class="a-f"> │</span> |
| 275 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 276 |
<span class="a-d">───╯</span></pre> |
| 277 |
|
| 278 |
### `dd` |
| 279 |
|
| 280 |
Every `dd` operand is a `KEY=VALUE` pair, and a value can be a comma-separated |
| 281 |
list of flags, so there are three things the caret can pick out: the key, the |
| 282 |
whole value, or one flag inside the list. A flag is underlined where it sits |
| 283 |
in the list rather than wherever its text first turns up, and the advice names |
| 284 |
the flags that operand accepts: |
| 285 |
|
| 286 |
Before: |
| 287 |
|
| 288 |
<pre class="diag"><span class="a-p">$</span> dd conv=ucase,zap |
| 289 |
dd: invalid conversion: 'zap'</pre> |
| 290 |
|
| 291 |
After: |
| 292 |
|
| 293 |
<pre class="diag"><span class="a-p">$</span> dd conv=ucase,zap |
| 294 |
dd: invalid conversion: 'zap' |
| 295 |
<span class="a-d">╭─[</span> dd:1:15 <span class="a-d">]</span> |
| 296 |
<span class="a-d">│</span> |
| 297 |
<span class="a-d">1 │</span> <span class="a-s">dd conv=ucase,</span><span class="a-e">zap</span> |
| 298 |
<span class="a-f"> │</span> <span class="a-e">─┬─</span> |
| 299 |
<span class="a-f"> │</span> <span class="a-e">╰───</span> not a known conversion |
| 300 |
<span class="a-f"> │</span> |
| 301 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: conv= is one of ascii, ebcdic, ibm, lcase, ucase, block, unblock, swab, sync, noerror, sparse, excl, nocreat, notrunc, fdatasync or fsync |
| 302 |
<span class="a-d">───╯</span></pre> |
| 303 |
|
| 304 |
`iflag=` and `oflag=` are reported apart, so an output flag is no longer |
| 305 |
blamed on the input, and each lists its own flags: |
| 306 |
|
| 307 |
Before: |
| 308 |
|
| 309 |
<pre class="diag"><span class="a-p">$</span> dd oflag=zap |
| 310 |
dd: invalid output flag: 'zap'</pre> |
| 311 |
|
| 312 |
After: |
| 313 |
|
| 314 |
<pre class="diag"><span class="a-p">$</span> dd oflag=zap |
| 315 |
dd: invalid output flag: 'zap' |
| 316 |
<span class="a-d">╭─[</span> dd:1:10 <span class="a-d">]</span> |
| 317 |
<span class="a-d">│</span> |
| 318 |
<span class="a-d">1 │</span> <span class="a-s">dd oflag=</span><span class="a-e">zap</span> |
| 319 |
<span class="a-f"> │</span> <span class="a-e">─┬─</span> |
| 320 |
<span class="a-f"> │</span> <span class="a-e">╰───</span> not a known output flag |
| 321 |
<span class="a-f"> │</span> |
| 322 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: oflag= is one of direct, directory, dsync, sync, nocache, nonblock, noatime, noctty, nofollow, append or seek_bytes |
| 323 |
<span class="a-d">───╯</span></pre> |
| 324 |
|
| 325 |
An unknown key is underlined without its value, since the value is not what |
| 326 |
was rejected: |
| 327 |
|
| 328 |
Before: |
| 329 |
|
| 330 |
<pre class="diag"><span class="a-p">$</span> dd zap=1 |
| 331 |
dd: unrecognized operand 'zap=1'</pre> |
| 332 |
|
| 333 |
After: |
| 334 |
|
| 335 |
<pre class="diag"><span class="a-p">$</span> dd zap=1 |
| 336 |
dd: unrecognized operand 'zap=1' |
| 337 |
<span class="a-d">╭─[</span> dd:1:4 <span class="a-d">]</span> |
| 338 |
<span class="a-d">│</span> |
| 339 |
<span class="a-d">1 │</span> <span class="a-s">dd </span><span class="a-e">zap</span><span class="a-s">=1</span> |
| 340 |
<span class="a-f"> │</span> <span class="a-e">───</span> |
| 341 |
<span class="a-f"> │</span> |
| 342 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: an operand is KEY=VALUE, as in if=file bs=4k count=10 |
| 343 |
<span class="a-d">───╯</span></pre> |
| 344 |
|
| 345 |
A number that does not fit is rejected rather than quietly clamped, and the |
| 346 |
caret covers the whole value: |
| 347 |
|
| 348 |
Before: |
| 349 |
|
| 350 |
<pre class="diag"><span class="a-p">$</span> dd count=99999999999999999999999 |
| 351 |
dd: invalid number: '99999999999999999999999': Value too large for defined data type</pre> |
| 352 |
|
| 353 |
After: |
| 354 |
|
| 355 |
<pre class="diag"><span class="a-p">$</span> dd count=99999999999999999999999 |
| 356 |
dd: invalid number: '99999999999999999999999': Value too large for defined data type |
| 357 |
<span class="a-d">╭─[</span> dd:1:10 <span class="a-d">]</span> |
| 358 |
<span class="a-d">│</span> |
| 359 |
<span class="a-d">1 │</span> <span class="a-s">dd count=</span><span class="a-e">99999999999999999999999</span> |
| 360 |
<span class="a-f"> │</span> <span class="a-e">───────────────────────</span> |
| 361 |
<span class="a-f"> │</span> |
| 362 |
<span class="a-f"> │</span> <span class="a-h">Help</span>: a number may be followed by a multiplier: c, w, b, then K, M, G and so on for 1024, kB, MB, GB for 1000 |
| 363 |
<span class="a-d">───╯</span></pre> |
| 364 |
|
| 365 |
[Try it in the playground](https://uutils.org/playground/?cmd=dd+conv%3Ducase%2Czap). |
| 366 |
|
| 367 |
## Compatibility |
| 368 |
|
| 369 |
This is strictly an interactive nicety; nothing that reads our output can tell |
| 370 |
the difference: |
| 371 |
|
| 372 |
- Reports are only rendered when **stderr is a terminal**, unless `UUTILS_DIAG` |
| 373 |
says otherwise (see below). In a script, a pipe, or a test harness, the |
| 374 |
utility keeps printing its plain one-line message, so existing scripts that |
| 375 |
match on stderr keep working. |
| 376 |
- A report is drawn only when the error can be **tied to something on the |
| 377 |
command line**. When it cannot - the error is not about any one argument, or |
| 378 |
the operand it names was rewritten or consumed before it could be located - |
| 379 |
the plain one-line message is printed instead, even at a terminal. A utility |
| 380 |
in the table below therefore reports the parse errors it can place, not |
| 381 |
every error it has. |
| 382 |
- Exit codes are unchanged, and so is the `Try '... --help'` hint: an error |
| 383 |
that is a usage error still prints it under the report. |
| 384 |
- Colors follow the usual conventions: they are used only on a terminal, and |
| 385 |
[`NO_COLOR`](https://no-color.org/) disables them. |
| 386 |
- All messages, labels and help lines are localized like the rest of uutils |
| 387 |
(see [Localization](l10n.md)). |
| 388 |
- The rendering can be compiled out entirely: it sits behind the |
| 389 |
`feat_diagnostics` cargo feature, which is on by default. Building with |
| 390 |
`--no-default-features` (plus a `feat_os_*` selection) drops the renderer |
| 391 |
and its `ariadne` dependency, and every utility keeps its plain one-line |
| 392 |
messages. |
| 393 |
|
| 394 |
## Turning it on and off |
| 395 |
|
| 396 |
The default keys off stderr being a terminal, and nothing else - which is |
| 397 |
usually what you want, but not always. `UUTILS_DIAG` overrides it: |
| 398 |
|
| 399 |
| Value | Effect | |
| 400 |
| ----- | ------ | |
| 401 |
| `always` | Draw the report even when stderr is a file or a pipe. | |
| 402 |
| `never` | Keep the plain one-line message even at a terminal. | |
| 403 |
| `auto`, unset, anything else | Decide from stderr, as above. | |
| 404 |
|
| 405 |
An unrecognized value is deliberately not an error - this is the kind of |
| 406 |
variable that gets exported from a shell profile once and forgotten, and no |
| 407 |
spelling of it should be able to make a utility fail. |
| 408 |
|
| 409 |
`always` is the one to reach for when the error has to leave the terminal |
| 410 |
it happened in - a CI log, or a report to paste into a bug: |
| 411 |
|
| 412 |
``` |
| 413 |
$ UUTILS_DIAG=always sort -k2.3x notes.txt 2> parse.log |
| 414 |
``` |
| 415 |
|
| 416 |
Colors are a separate question, and one the terminal still answers: a report |
| 417 |
forced into a file is written without them, so nothing has to strip escape |
| 418 |
sequences back out. [`NO_COLOR`](https://no-color.org/) is the middle setting |
| 419 |
at a terminal - the report is still drawn, just in plain text. |
| 420 |
|
| 421 |
Without the variable, both directions are still one command away. To get the |
| 422 |
plain line while sitting at a terminal, send stderr somewhere that is not one: |
| 423 |
|
| 424 |
``` |
| 425 |
$ sort -k2.3x notes.txt 2>&1 | cat |
| 426 |
sort: stray character in field spec: invalid field specification '2.3x' |
| 427 |
``` |
| 428 |
|
| 429 |
And to get a report out of a command that has to run under a real terminal, |
| 430 |
give it a pty with `script -qec "..." /dev/null`, or `unbuffer` from expect. |
| 431 |
|
| 432 |
## Supported utilities |
| 433 |
|
| 434 |
| Utility | What the caret points at | Try it | |
| 435 |
| -------- | ------------------------ | ------ | |
| 436 |
| `test` | the argument that made the expression fail | [`test 7 -eq zap`](https://uutils.org/playground/?cmd=test+7+-eq+zap) | |
| 437 |
| `expr` | the argument that made the expression fail | [`expr 9 + foo`](https://uutils.org/playground/?cmd=expr+9+%2B+foo) | |
| 438 |
| `chmod` | the failing clause (or character) of an invalid symbolic or octal mode | [`chmod 'g+rw?x' fruits.txt`](https://uutils.org/playground/?cmd=chmod+%27g%2Brw%3Fx%27+fruits.txt) | |
| 439 |
| `mkdir` | the failing part of the mode given to `-m`/`--mode` | [`mkdir -m u+q mydir`](https://uutils.org/playground/?cmd=mkdir+-m+u%2Bq+mydir) | |
| 440 |
| `mkfifo` | the failing part of the mode given to `-m`/`--mode` | [`mkfifo -m u+q mypipe`](https://uutils.org/playground/?cmd=mkfifo+-m+u%2Bq+mypipe) | |
| 441 |
| `mknod` | the failing part of the mode given to `-m`/`--mode` | [`mknod -m u+q mydev c 1 3`](https://uutils.org/playground/?cmd=mknod+-m+u%2Bq+mydev+c+1+3) | |
| 442 |
| `install`| the failing part of the mode given to `-m`/`--mode` | [`install -m u+q fruits.txt dest`](https://uutils.org/playground/?cmd=install+-m+u%2Bq+fruits.txt+dest) | |
| 443 |
| `tr` | the part of a set that is at fault (bad class, backwards range, bad repeat count, …) | [`tr 'qw[y-b]' x`](https://uutils.org/playground/?cmd=tr+%27qw%5By-b%5D%27+x) | |
| 444 |
| `sort` | the failing part of a `-k`/`--key` or field specification, or of the SIZE given to `-S` | [`sort -k2.3x fruits.txt`](https://uutils.org/playground/?cmd=sort+-k2.3x+fruits.txt) | |
| 445 |
| `numfmt` | the failing part of a `--field` or `--format` specification | [`numfmt --format=%q 1000`](https://uutils.org/playground/?cmd=numfmt+--format%3D%25q+1000) | |
| 446 |
| `printf` | the failing conversion or escape in the format string | [`printf %5.2c q`](https://uutils.org/playground/?cmd=printf+%255.2c+q) | |
| 447 |
| `seq` | the failing conversion in the format given to `-f`/`--format` | [`seq -f %5.2c 1 3`](https://uutils.org/playground/?cmd=seq+-f+%255.2c+1+3) | |
| 448 |
| `stat` | the failing directive of a `-c`/`--format` or `--printf` format | [`stat -c %d%.3 fruits.txt`](https://uutils.org/playground/?cmd=stat+-c+%25d%25.3+fruits.txt) | |
| 449 |
| `env` | the failing part of a `-S`/`--split-string` string | [`env -S 'echo ${1FOO}'`](https://uutils.org/playground/?cmd=env+-S+%27echo+%24%7B1FOO%7D%27) | |
| 450 |
| `dd` | the failing key, value or flag of a `KEY=VALUE` operand | [`dd conv=ucase,zap`](https://uutils.org/playground/?cmd=dd+conv%3Ducase%2Czap) | |
| 451 |
| `join` | the failing field of the output format given to `-o` | [`join -o 1.2,2.x fruits.txt fruits.txt`](https://uutils.org/playground/?cmd=join+-o+1.2%2C2.x+fruits.txt+fruits.txt) | |
| 452 |
| `cut` | the failing range in the list given to `-b`, `-c`, `-f` or `-F` | [`cut -f 1,4-2 fruits.txt`](https://uutils.org/playground/?cmd=cut+-f+1%2C4-2+fruits.txt) | |
| 453 |
| `csplit` | the failing pattern operand, the character of its regex that broke, or the format given to `-b`/`-n` | [`csplit fruits.txt '/a(b/'`](https://uutils.org/playground/?cmd=csplit+fruits.txt+%27%2Fa%28b%2F%27) | |
| 454 |
| `split` | the failing part of the SIZE given to `-b`, `-C` or `-l` | [`split -b 7zq fruits.txt`](https://uutils.org/playground/?cmd=split+-b+7zq+fruits.txt) | |
| 455 |
| `shred` | the failing part of the SIZE given to `-s`/`--size` | [`shred -s 4vv fruits.txt`](https://uutils.org/playground/?cmd=shred+-s+4vv+fruits.txt) | |
| 456 |
| `head` | the failing part of the SIZE given to `-c` or `-n` | [`head -c 1fb fruits.txt`](https://uutils.org/playground/?cmd=head+-c+1fb+fruits.txt) | |
| 457 |
| `tail` | the failing part of the SIZE given to `-c` or `-n` | [`tail -c 1fb fruits.txt`](https://uutils.org/playground/?cmd=tail+-c+1fb+fruits.txt) | |
| 458 |
| `truncate` | the failing part of the SIZE given to `-s`/`--size` | [`truncate -s 10fb fruits.txt`](https://uutils.org/playground/?cmd=truncate+-s+10fb+fruits.txt) | |
| 459 |
| `od` | the failing part of the SIZE given to `-j`, `-N`, `-S` or `-w` | [`od -N 3zz fruits.txt`](https://uutils.org/playground/?cmd=od+-N+3zz+fruits.txt) | |
| 460 |
| `du` | the failing part of the SIZE given to `-B`/`--block-size` or `-t`/`--threshold` | [`du -B 1fb`](https://uutils.org/playground/?cmd=du+-B+1fb) | |
| 461 |
| `df` | the failing part of the SIZE given to `-B`/`--block-size` | [`df -B 1fb`](https://uutils.org/playground/?cmd=df+-B+1fb) | |
| 462 |
| `ls` | the failing part of the SIZE given to `--block-size` (also `dir` and `vdir`) | [`ls --block-size=1fb`](https://uutils.org/playground/?cmd=ls+--block-size%3D1fb) | |
| 463 |
| `stdbuf` | the failing part of the buffering mode given to `-i`, `-o` or `-e` | [`stdbuf -o 6pq head`](https://uutils.org/playground/?cmd=stdbuf+-o+6pq+head) | |
| 464 |
|
| 465 |
## How it works |
| 466 |
|
| 467 |
The rendering lives in `uucore::features::diagnostics` and is built on |
| 468 |
[ariadne](https://crates.io/crates/ariadne). A utility that opts in: |
| 469 |
|
| 470 |
1. Takes a `Snapshot` of its argument list. The snapshot joins the arguments |
| 471 |
into one line (quoting where needed, and rendering non-UTF-8 arguments the |
| 472 |
way the shell would) and remembers the byte range of each argument. |
| 473 |
2. Maps its own error type to a position, and optionally a label and a line |
| 474 |
of advice, in a small per-utility `diagnostics.rs` module. A label is only |
| 475 |
used when it adds something the message does not say - an expectation, or |
| 476 |
a fix such as tr's `did you mean 'b-y'?` - never to restate it; with no |
| 477 |
label the span is drawn as a bare underline. Everything user-facing is |
| 478 |
passed in already localized. |
| 479 |
3. Locates the argument the operand came from - with |
| 480 |
`Snapshot::index_of_value` for an option's value in whatever spelling it |
| 481 |
was given (`-k 2.3x`, `-k2.3x`, `-rk2.3x`, `--key 2.3x`, `--key=2.3x`), |
| 482 |
`Snapshot::index_of_positional` for a positional operand, or an index the |
| 483 |
utility tracked itself - and calls `Snapshot::render` to point at the |
| 484 |
whole argument, or `Snapshot::render_inside_at` to point at a byte range |
| 485 |
*inside* the operand it carries. Because the argument is named rather than |
| 486 |
searched for, a file, another option, or the program name that happens to |
| 487 |
share the operand's text can never take the caret. |
| 488 |
`Snapshot::render_option_value` does the two steps in one, which is what |
| 489 |
most option values want. |
| 490 |
|
| 491 |
An argument holding a space is echoed back quoted, and the caret still points |
| 492 |
inside it: the quotes only wrap the operand, so its bytes are found where they |
| 493 |
were printed and offsets count from there. An argument that could not be |
| 494 |
printed as-is - a non-UTF-8 one, or one whose quoting had to be broken up - is |
| 495 |
underlined as a whole instead, since no offset into it would line up with what |
| 496 |
the reader sees. |
| 497 |
|
| 498 |
Callers check `diagnostics::enabled()` first, so the non-interactive path |
| 499 |
costs nothing and keeps its GNU-compatible message. |
| 500 |
|
| 501 |
Errors that come out of a shared parser are rendered by a shared helper, so |
| 502 |
that a syntax common to several utilities is explained the same way in all of |
| 503 |
them, and its labels live in the common locale strings rather than being |
| 504 |
repeated per utility. Three parsers work this way: |
| 505 |
|
| 506 |
- **Modes** (`uucore::mode`), for `chmod`, `mkdir`, `mkfifo`, `mknod` and |
| 507 |
`install`. |
| 508 |
- **Range lists** (`uucore::ranges`), for `cut`'s `-b`, `-c` and `-f` and for |
| 509 |
`numfmt --field`. `Range::from_list` reports which item of the list failed |
| 510 |
and where it sat. |
| 511 |
- **Sizes** (`uucore::parser::parse_size`), for `head`, `tail`, `truncate`, |
| 512 |
`split`, `shred`, `stdbuf`, `sort`, `od`, `du`, `df` and `ls` (with `dir` |
| 513 |
and `vdir`) today, and available to the other callers of the parser. |
| 514 |
`ParseSizeError::span` works out from the operand which of its two parts - |
| 515 |
the number or the unit - was rejected, so the error type keeps the shape its |
| 516 |
callers build by hand. |
| 517 |
|
| 518 |
Taking modes as the worked example: the parser reports errors as a structured |
| 519 |
`ModeError` carrying the byte range at fault, and its rendering places the |
| 520 |
caret for every utility that takes a mode - `ModeError::render_option_value` |
| 521 |
finds the mode as the value of `-m`/`--mode` for `mkdir`, `mkfifo`, `mknod` |
| 522 |
and `install`, while `chmod`, which accepts modes clap cannot see (such as |
| 523 |
`chmod -w -r file`), tracks where each mode operand sat and passes the index |
| 524 |
to `ModeError::render_at`. |