|
|
The end user can make several command-line errors when invoking a program. The first error is specifying an illegal option:
cc -Z foo.c # illegal option!
The second error is failing to specify a value for an option that expects one:
cc -o -- foo.c # missing value!
Notice that if an option taking a value appears in an option block, it is treated as missing a value:
cc -co foo.c # missing value!
This is consistent with the intended (but currently unenforced) behavior of getopt(S).
The third error is a missing option:
cc - foo.c # missing option!
Args uses Objections to signal such errors. In the first case, Args::unexpected_option is raised; in the second case, Args::missing_val is raised; and in the third case, Args::missing_opt is raised. In each case both the default and recovery actions is to (1) print an error message, (2) ignore the offending (or missing) option, and (3) set the ``error state'' of the Args object.
This error state can be inquired by calling error():
main(int argc, const char*const* argv) { Args args(argc, argv, "co:OI:D;p", Args::intermix | Args::plusoptions, keywords); if (args.error()) exit(1); // ... }
The reason Args does not itself exit is to allow programs that wish to continue to do so.