changeset 11140:8aa93f43bae8

use getopt_long correctly
author John W. Eaton <jwe@octave.org>
date Fri, 22 Oct 2010 21:18:00 -0400
parents 0dd95d1d57e6
children 224c80da37c5
files src/ChangeLog src/octave.cc
diffstat 2 files changed, 26 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Fri Oct 22 20:59:48 2010 -0400
+++ b/src/ChangeLog	Fri Oct 22 21:18:00 2010 -0400
@@ -1,3 +1,13 @@
+2010-10-22  John W. Eaton  <jwe@octave.org>
+
+	* octave.cc (usage): Put whitespace before and after usage message.
+	(octave_main): If getopt_long returns '?', it means an
+	unrecognized option was encountered.
+	Panic if default case in option switch statement is reached.
+ 	(verbose_usage, short_opts, usage_string): Remove '?' as an
+	alias for 'h' in the list of possible options.
+	Fixes bug #31423.
+
 2010-10-22  John W. Eaton  <jwe@octave.org>
 
 	* oct-parse.yy (load_fcn_from_file): Also strip directory when
--- a/src/octave.cc	Fri Oct 22 20:59:48 2010 -0400
+++ b/src/octave.cc	Fri Oct 22 21:18:00 2010 -0400
@@ -114,7 +114,7 @@
 
 // Usage message
 static const char *usage_string = 
-  "octave [-?HVdfhiqvx] [--debug] [--echo-commands] [--eval CODE]\n\
+  "octave [-HVdfhiqvx] [--debug] [--echo-commands] [--eval CODE]\n\
        [--exec-path path] [--help] [--image-path path] [--info-file file]\n\
        [--info-program prog] [--interactive] [--line-editing]\n\
        [--no-history] [--no-init-file] [--no-init-path] [--no-line-editing]\n\
@@ -124,7 +124,7 @@
 // This is here so that it's more likely that the usage message and
 // the real set of options will agree.  Note: the `+' must come first
 // to prevent getopt from permuting arguments!
-static const char *short_opts = "+?HVdfhip:qvx";
+static const char *short_opts = "+HVdfhip:qvx";
 
 // The code to evaluate at startup (--eval CODE)
 static std::string code_to_eval;
@@ -476,7 +476,7 @@
   --echo-commands, -x     Echo commands as they are executed.\n\
   --eval CODE             Evaluate CODE.  Exit when done unless --persist.\n\
   --exec-path PATH        Set path for executing subprograms.\n\
-  --help, -h, -?          Print short help message and exit.\n\
+  --help, -h,             Print short help message and exit.\n\
   --image-path PATH       Add PATH to head of image search path.\n\
   --info-file FILE        Use top-level info file FILE.\n\
   --info-program PROGRAM  Use PROGRAM for reading info files.\n\
@@ -513,7 +513,7 @@
 static void
 usage (void)
 {
-  std::cerr << "usage: " << usage_string << "\n";
+  std::cerr << "\nusage: " << usage_string << "\n\n";
   exit (1);
 }
 
@@ -648,6 +648,13 @@
 
       switch (optc)
         {
+        case '?':
+          // Unrecognized option.  getopt_long already printed a
+          // message about that, so we will just print the usage string
+          // and exit.
+          usage ();
+          break;
+
         case 'H':
           read_history_file = false;
           bind_internal_variable ("saving_history", false);
@@ -668,7 +675,6 @@
           break;
 
         case 'h':
-        case '?':
           verbose_usage ();
           break;
 
@@ -764,7 +770,11 @@
           break;
 
         default:
-          usage ();
+          // getopt_long should print a message about unrecognized
+          // options and return '?', which is handled above.  So if we
+          // end up here, it is because there was an option but we
+          // forgot to handle it.  That should be fatal.
+          panic_impossible ();
           break;
         }
     }