Skip to content

Commit a54f589

Browse files
committed
usage: surround required options with parens
When there's a `CHOICE` that is `REQUIRED`, surround the options with parentheses in the usage.
1 parent f9d3770 commit a54f589

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

adopt.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,17 @@ int adopt_usage_fprint(
305305
const adopt_spec specs[])
306306
{
307307
const adopt_spec *spec;
308-
int choice = 0;
308+
int choice = 0, next_choice = 0, optional = 0;
309309
int error;
310310

311311
if ((error = fprintf(file, "usage: %s", command)) < 0)
312312
goto done;
313313

314314
for (spec = specs; spec->type; ++spec) {
315-
int optional = !(spec->usage & ADOPT_USAGE_REQUIRED);
315+
if (!choice)
316+
optional = !(spec->usage & ADOPT_USAGE_REQUIRED);
317+
318+
next_choice = !!((spec + 1)->usage & ADOPT_USAGE_CHOICE);
316319

317320
if (spec->usage & ADOPT_USAGE_HIDDEN)
318321
continue;
@@ -326,6 +329,11 @@ int adopt_usage_fprint(
326329
goto done;
327330

328331
if (optional && !choice && (error = fprintf(file, "[")) < 0)
332+
error = fprintf(file, "[");
333+
if (!optional && !choice && next_choice)
334+
error = fprintf(file, "(");
335+
336+
if (error < 0)
329337
goto done;
330338

331339
if (spec->type == ADOPT_VALUE && spec->alias)
@@ -350,10 +358,15 @@ int adopt_usage_fprint(
350358
if (error < 0)
351359
goto done;
352360

353-
choice = !!((spec+1)->usage & ADOPT_USAGE_CHOICE);
361+
if (!optional && choice && !next_choice)
362+
error = fprintf(file, ")");
363+
else if (optional && !next_choice)
364+
error = fprintf(file, "]");
354365

355-
if (optional && !choice && (error = fprintf(file, "]")) < 0)
366+
if (error < 0)
356367
goto done;
368+
369+
choice = next_choice;
357370
}
358371

359372
error = fprintf(file, "\n");

0 commit comments

Comments
 (0)