comparison lwlib/lw_cmdline.c @ 441:b138b4005125

Make missing command line arguments fail properly Actually make lwasm, lwlink, and lwar exit with a nonzero status if command line argument parsing fails due to missing arguments. This required adjustments to lw_cmdline to return error codes in those cases.
author William Astle <lost@l-w.ca>
date Mon, 27 Nov 2017 22:35:53 -0700
parents 8e25147c2aa8
children
comparison
equal deleted inserted replaced
440:fda62f676ed4 441:b138b4005125
17 17
18 You should have received a copy of the GNU General Public License along with 18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>. 19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */ 20 */
21 21
22 #include <errno.h>
22 #include <stdio.h> 23 #include <stdio.h>
23 #include <stdlib.h> 24 #include <stdlib.h>
24 #include <string.h> 25 #include <string.h>
25 #include <ctype.h> 26 #include <ctype.h>
26 27
378 int i, j, r; 379 int i, j, r;
379 int firstarg; 380 int firstarg;
380 int nextarg; 381 int nextarg;
381 char *tstr; 382 char *tstr;
382 int cch; 383 int cch;
383 384
384 /* first, permute the argv array so that all option arguments are at the start */ 385 /* first, permute the argv array so that all option arguments are at the start */
385 for (i = 1, firstarg = 1; i < argc; i++) 386 for (i = 1, firstarg = 1; i < argc; i++)
386 { 387 {
387 if (argv[i][0] == '-' && argv[i][1]) 388 if (argv[i][0] == '-' && argv[i][1])
388 { 389 {
494 { 495 {
495 if (cch) 496 if (cch)
496 fprintf(stderr, "Unknown option '%c'. See %s --usage.\n", argv[i][cch - 1], argv[0]); 497 fprintf(stderr, "Unknown option '%c'. See %s --usage.\n", argv[i][cch - 1], argv[0]);
497 else 498 else
498 fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]); 499 fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]);
499 exit(1); 500 return EINVAL;
500 } 501 }
501 if (parser -> options[j].arg) 502 if (parser -> options[j].arg)
502 { 503 {
503 if (tstr && cch && argv[i][cch] == 0) 504 if (tstr && cch && argv[i][cch] == 0)
504 nextarg++; 505 nextarg++;
514 } 515 }
515 516
516 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0) 517 if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
517 { 518 {
518 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name); 519 fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
519 continue; 520 return EINVAL;
520 } 521 }
521 } 522 }
522 r = (*(parser -> parser))(parser -> options[j].key, tstr, input); 523 r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
523 if (r != 0) 524 if (r != 0)
524 return r; 525 return r;