浏览代码

better usage messages, showing entire offending argument

Roberto Ierusalimschy 15 年之前
父节点
当前提交
a838b3b496
共有 1 个文件被更改,包括 11 次插入8 次删除
  1. 11 8
      lua.c

+ 11 - 8
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
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** 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,
   fprintf(stderr,
-  "%s: unrecognized option '-%c'\n"
   "usage: %s [options] [script [args]]\n"
   "usage: %s [options] [script [args]]\n"
   "Available options are:\n"
   "Available options are:\n"
   "  -e stat  execute string " LUA_QL("stat") "\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\n"
   "  -        stop handling options and execute stdin\n"
   "  -        stop handling options and execute stdin\n"
   ,
   ,
-  progname, badoption, progname);
+  progname);
   fflush(stderr);
   fflush(stderr);
 }
 }
 
 
@@ -357,11 +360,11 @@ static int collectargs (char **argv, int *pi, int *pv, int *pe) {
       case 'l':
       case 'l':
         if (argv[i][2] == '\0') {
         if (argv[i][2] == '\0') {
           i++;
           i++;
-          if (argv[i] == NULL) return -1;
+          if (argv[i] == NULL) return -(i - 1);
         }
         }
         break;
         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;
   return 0;
@@ -415,7 +418,7 @@ static int pmain (lua_State *L) {
   if (argv[0] && argv[0][0]) progname = argv[0];
   if (argv[0] && argv[0][0]) progname = argv[0];
   script = collectargs(argv, &has_i, &has_v, &has_e);
   script = collectargs(argv, &has_i, &has_v, &has_e);
   if (script < 0) {  /* invalid arg? */
   if (script < 0) {  /* invalid arg? */
-    print_usage(-script);  /* '-script' is the offending argument */
+    print_usage(argv[-script]);
     return 0;
     return 0;
   }
   }
   if (has_v) print_version();
   if (has_v) print_version();