From a838b3b4968ec627513b7cad434497c749ba2bcd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Feb 2010 09:58:57 -0200 Subject: better usage messages, showing entire offending argument --- lua.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lua.c b/lua.c index cec63efe..1c352e27 100644 --- a/lua.c +++ b/lua.c @@ -1,5 +1,5 @@ /* -** $Id: lua.c,v 1.183 2010/01/21 16:31:06 roberto Exp roberto $ +** $Id: lua.c,v 1.184 2010/01/21 16:49:21 roberto Exp roberto $ ** Lua stand-alone interpreter ** See Copyright Notice in lua.h */ @@ -102,9 +102,12 @@ static void laction (int i) { } -static void print_usage (char badoption) { +static void print_usage (const char *badoption) { + if (badoption[1] == 'e' || badoption[1] == 'l') + fprintf(stderr, "%s: '%s' needs argument\n", progname, badoption); + else + fprintf(stderr, "%s: unrecognized option '%s'\n", progname, badoption); fprintf(stderr, - "%s: unrecognized option '-%c'\n" "usage: %s [options] [script [args]]\n" "Available options are:\n" " -e stat execute string " LUA_QL("stat") "\n" @@ -114,7 +117,7 @@ static void print_usage (char badoption) { " -- stop handling options\n" " - stop handling options and execute stdin\n" , - progname, badoption, progname); + progname); fflush(stderr); } @@ -357,11 +360,11 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) { case 'l': if (argv[i][2] == '\0') { i++; - if (argv[i] == NULL) return -1; + if (argv[i] == NULL) return -(i - 1); } break; - default: /* invalid option; return the offendind character as a... */ - return -(unsigned char)argv[i][1]; /* ...negative value */ + default: /* invalid option; return its index... */ + return -i; /* ...as a negative value */ } } return 0; @@ -415,7 +418,7 @@ static int pmain (lua_State *L) { if (argv[0] && argv[0][0]) progname = argv[0]; script = collectargs(argv, &has_i, &has_v, &has_e); if (script < 0) { /* invalid arg? */ - print_usage(-script); /* '-script' is the offending argument */ + print_usage(argv[-script]); return 0; } if (has_v) print_version(); -- cgit v1.2.3-55-g6feb