changeset 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 fda62f676ed4
children 61580fc48f98
files lwar/main.c lwasm/main.c lwlib/lw_cmdline.c lwlink/main.c
diffstat 4 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lwar/main.c	Mon Nov 27 22:21:42 2017 -0700
+++ b/lwar/main.c	Mon Nov 27 22:35:53 2017 -0700
@@ -143,7 +143,10 @@
 int main(int argc, char **argv)
 {
 	program_name = argv[0];
-	lw_cmdline_parse(&argparser, argc, argv, 0, 0, NULL);
+	if (lw_cmdline_parse(&argparser, argc, argv, 0, 0, NULL) != 0)
+	{
+		exit(1);
+	}
 	if (archive_file == NULL)
 	{
 		fprintf(stderr, "You must specify an archive file.\n");
--- a/lwasm/main.c	Mon Nov 27 22:21:42 2017 -0700
+++ b/lwasm/main.c	Mon Nov 27 22:35:53 2017 -0700
@@ -312,7 +312,10 @@
 	asmstate.pragmas = PRAGMA_FORWARDREFMAX;
 	
 	/* parse command line arguments */	
-	lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, &asmstate);
+	if (lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, &asmstate) != 0)
+	{
+		exit(1);
+	}
 
 	if (!asmstate.output_file)
 	{
--- a/lwlib/lw_cmdline.c	Mon Nov 27 22:21:42 2017 -0700
+++ b/lwlib/lw_cmdline.c	Mon Nov 27 22:35:53 2017 -0700
@@ -19,6 +19,7 @@
 this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -380,7 +381,7 @@
 	int nextarg;
 	char *tstr;
 	int cch;
-	
+		
 	/* first, permute the argv array so that all option arguments are at the start */
 	for (i = 1, firstarg = 1; i < argc; i++)
 	{
@@ -496,7 +497,7 @@
 				fprintf(stderr, "Unknown option '%c'. See %s --usage.\n", argv[i][cch - 1], argv[0]);
 			else
 				fprintf(stderr, "Unknown option '%s'. See %s --usage.\n", argv[i - 1], argv[0]);
-			exit(1);
+			return EINVAL;
 		}
 		if (parser -> options[j].arg)
 		{
@@ -516,7 +517,7 @@
 			if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
 			{
 				fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);
-				continue;
+				return EINVAL;
 			}
 		}
 		r = (*(parser -> parser))(parser -> options[j].key, tstr, input);
--- a/lwlink/main.c	Mon Nov 27 22:21:42 2017 -0700
+++ b/lwlink/main.c	Mon Nov 27 22:35:53 2017 -0700
@@ -185,7 +185,11 @@
 {
 	program_name = argv[0];
 
-	lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL);
+	if (lw_cmdline_parse(&cmdline_parser, argc, argv, 0, 0, NULL) != 0)
+	{
+		// bail if parsing failed
+		exit(1);
+	}
 	if (ninputfiles == 0)
 	{
 		fprintf(stderr, "No input files\n");