Skip to content

Commit

Permalink
options: Handle command line option errors more gracefully
Browse files Browse the repository at this point in the history
Stop printing the "BUG: unprocessed option id -2" message when user
passes wrong arguments.

Before this change:

    $ ./build/release/ponyc -V
    ./build/release/ponyc: '-V' option requires an argument!
    BUG: unprocessed option id -2
    $ echo $?
    255

After this change:

    $ ./build/release/ponyc -V
    ./build/release/ponyc: '-V' option requires an argument!
    $ echo $?
    255
  • Loading branch information
felipecrv authored and SeanTAllen committed Jan 7, 2020
1 parent 5d5ae76 commit 764110c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/libponyc/options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ ponyc_opt_process_t ponyc_opt_process(opt_state_t* s, pass_opt_t* opt,
}
break;

case -2:
// ponyint_opt_next already took care of printing the error message
return EXIT_255;

default:
printf("BUG: unprocessed option id %d\n", id);
return EXIT_255;
Expand Down
2 changes: 1 addition & 1 deletion src/libponyrt/options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,5 @@ int ponyint_opt_next(opt_state_t* s)

strip_accepted_opts(s);

return m->id;
return (int)m->id;
}
7 changes: 7 additions & 0 deletions src/libponyrt/options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ typedef struct opt_state_t
void ponyint_opt_init(const opt_arg_t* args, opt_state_t* s, int* argc,
char** argv);

/** Find the unsigned identifier of the next option.
*
* Special return values:
*
* -1 when there are no more options to process.
* -2 when an error is detected and an error message is printed.
*/
int ponyint_opt_next(opt_state_t* s);

#endif
9 changes: 8 additions & 1 deletion src/libponyrt/sched/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ static int parse_opts(int argc, char** argv, options_t* opt)
case OPT_VERSION: opt->version = true; break;
case OPT_PONYHELP: opt->ponyhelp = true; break;

default: exit(-1);
case -2:
// an error message has been printed by ponyint_opt_next
exit(-1);
break;

default:
exit(-1);
break;
}
}

Expand Down

0 comments on commit 764110c

Please sign in to comment.