Skip to content

Commit 85dd8cd

Browse files
committed
status: improve parse error messages
1 parent 5ac6efe commit 85dd8cd

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

adopt.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,27 @@ static int spec_name_fprint(FILE *file, const adopt_spec *spec)
355355

356356
int adopt_status_fprint(
357357
FILE *file,
358+
const char *command,
358359
const adopt_opt *opt)
359360
{
360361
const adopt_spec *choice;
361362
int error;
362363

364+
if (command && (error = fprintf(file, "%s: ", command)) < 0)
365+
return error;
366+
363367
switch (opt->status) {
364368
case ADOPT_STATUS_DONE:
365-
error = fprintf(file, "Finished processing arguments (no error)\n");
369+
error = fprintf(file, "finished processing arguments (no error)\n");
366370
break;
367371
case ADOPT_STATUS_OK:
368-
error = fprintf(file, "No error\n");
372+
error = fprintf(file, "no error\n");
369373
break;
370374
case ADOPT_STATUS_UNKNOWN_OPTION:
371-
error = fprintf(file, "Unknown option: %s\n", opt->arg);
375+
error = fprintf(file, "unknown option: %s\n", opt->arg);
372376
break;
373377
case ADOPT_STATUS_MISSING_VALUE:
374-
if ((error = fprintf(file, "Argument '")) < 0 ||
378+
if ((error = fprintf(file, "argument '")) < 0 ||
375379
(error = spec_name_fprint(file, opt->spec)) < 0 ||
376380
(error = fprintf(file, "' requires a value.\n")) < 0)
377381
;
@@ -380,7 +384,12 @@ int adopt_status_fprint(
380384
if (spec_is_choice(opt->spec)) {
381385
int is_choice = 1;
382386

383-
if ((error = fprintf(file, "One argument of")) < 0)
387+
if (spec_is_choice((opt->spec)+1))
388+
error = fprintf(file, "one of");
389+
else
390+
error = fprintf(file, "either");
391+
392+
if (error < 0)
384393
break;
385394

386395
for (choice = opt->spec; is_choice; ++choice) {
@@ -405,7 +414,7 @@ int adopt_status_fprint(
405414
(error = fprintf(file, " is required.\n")) < 0)
406415
break;
407416
} else {
408-
if ((error = fprintf(file, "Argument '")) < 0 ||
417+
if ((error = fprintf(file, "argument '")) < 0 ||
409418
(error = spec_name_fprint(file, opt->spec)) < 0 ||
410419
(error = fprintf(file, "' is required.\n")) < 0)
411420
break;

adopt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,13 @@ adopt_status_t adopt_parse(
242242
* specified, or when an argument was specified without a value.
243243
*
244244
* @param file The file to print information to
245+
* @param command The name of the command to use when printing (optional)
245246
* @param opt The option that failed to parse
246247
* @return 0 on success, -1 on failure
247248
*/
248249
int adopt_status_fprint(
249250
FILE *file,
251+
const char *command,
250252
const adopt_opt *opt);
251253

252254
/**

examples/loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int main(int argc, char **argv)
6060

6161
while (adopt_parser_next(&opt, &parser)) {
6262
if (opt.status != ADOPT_STATUS_OK) {
63-
adopt_status_fprint(stderr, &opt);
63+
adopt_status_fprint(stderr, argv[0], &opt);
6464
adopt_usage_fprint(stderr, argv[0], opt_specs);
6565
return 129;
6666
}

examples/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int main(int argc, char **argv)
5656
size_t i;
5757

5858
if (adopt_parse(&result, opt_specs, argv + 1, argc - 1) != 0) {
59-
adopt_status_fprint(stderr, &result);
59+
adopt_status_fprint(stderr, argv[0], &result);
6060
adopt_usage_fprint(stderr, argv[0], opt_specs);
6161
return 129;
6262
}

0 commit comments

Comments
 (0)