changeset 103:8b0be0fc42cf

Fixed arg handling for short args in command line option parser - optional args for short opts should now work and options no longer need a space between option character and argument
author lost@l-w.ca
date Sat, 06 Aug 2011 22:35:58 -0600
parents f7a87c3a8e0e
children bc82df7f6bbe
files lwlib/lw_cmdline.c
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/lwlib/lw_cmdline.c	Sat Aug 06 22:16:09 2011 -0600
+++ b/lwlib/lw_cmdline.c	Sat Aug 06 22:35:58 2011 -0600
@@ -395,8 +395,9 @@
 				break;
 		cch++;
 		tstr = argv[i] + cch;
-		if (!*tstr)
+		if (!*tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
 		{
+			/* only consume the next arg if the argument is optional */
 			if (nextarg < argc)
 				tstr = argv[nextarg];
 			else
@@ -438,7 +439,10 @@
 		/* this string or "nextarg" */
 		if (parser -> options[j].name == NULL)
 		{
-			fprintf(stderr, "Unknown option. See %s --usage.\n", argv[0]);
+			if (cch)
+				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);
 		}
 		if (parser -> options[j].arg)
@@ -449,6 +453,10 @@
 			if (!*tstr)
 				tstr = NULL;
 			
+			/* move on to next argument if we have an arg specified */
+			if (tstr && cch && argv[i][cch] != 0)
+				i++;
+			
 			if (!tstr && (parser -> options[j].flags & lw_cmdline_opt_optional) == 0)
 			{
 				fprintf(stderr, "Option %s requires argument.\n", parser -> options[j].name);