From e65b0ba6542b03a76fa6fecabdea56c04c2792b5 Mon Sep 17 00:00:00 2001 From: Mateusz Kazimierczuk Date: Thu, 27 Jul 2023 02:01:14 +0200 Subject: [PATCH] options: add -? as a synonym of -h (--help) The short option '-?' was removed nine years ago in commit 43019189, then restored six years later in 5bd92d4c, and then removed again two months later in 743100fe due to getopt() returning '?' for options that aren't recognized, preventing the use of '-?' as a valid option. However, getopt() provides a way to check for unrecognized options via the 'optopt' variable, which gets set only for invalid options. Signed-off-by: Mateusz Kazimierczuk Signed-off-by: Benno Schulenberg --- doc/nano.1 | 2 +- doc/nano.texi | 1 + doc/rnano.1 | 2 +- src/nano.c | 15 +++++++++------ 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/doc/nano.1 b/doc/nano.1 index 556dedeb..73219b38 100644 --- a/doc/nano.1 +++ b/doc/nano.1 @@ -250,7 +250,7 @@ Make the cursor visible in the file browser (putting it on the highlighted item) and in the help viewer. Useful for braille users and people with poor vision. .TP -.BR \-h ", " \-\-help +.BR \-h ", " \-? ", " \-\-help Show a summary of the available command-line options and exit. .TP .BR \-i ", " \-\-autoindent diff --git a/doc/nano.texi b/doc/nano.texi index df3dcbc0..0fe14e23 100644 --- a/doc/nano.texi +++ b/doc/nano.texi @@ -574,6 +574,7 @@ highlighted item) and in the help viewer. Useful for braille users and people with poor vision. @item -h +@itemx -? @itemx --help Show a summary of command-line options and exit. diff --git a/doc/rnano.1 b/doc/rnano.1 index 5a920287..1892d3e9 100644 --- a/doc/rnano.1 +++ b/doc/rnano.1 @@ -48,7 +48,7 @@ not allow appending or prepending to any file. .SH OPTIONS .TP -.BR \-h ", " \-\-help +.BR \-h ", " \-? ", " \-\-help Show the available command-line options and exit. .P For all existing options, see the \fBnano\fR(1) man page. diff --git a/src/nano.c b/src/nano.c index 8578ee7d..591545ff 100644 --- a/src/nano.c +++ b/src/nano.c @@ -607,7 +607,7 @@ void usage(void) #if defined(ENABLE_BROWSER) || defined(ENABLE_HELP) print_opt("-g", "--showcursor", N_("Show cursor in file browser & help text")); #endif - print_opt("-h", "--help", N_("Show this help text and exit")); + print_opt("-h, -?", "--help", N_("Show this help text and exit")); #ifndef NANO_TINY print_opt("-i", "--autoindent", N_("Automatically indent new lines")); print_opt("-j", "--jumpyscrolling", N_("Scroll per half-screen, not per line")); @@ -1849,7 +1849,7 @@ int main(int argc, char **argv) SET(RESTRICTED); while ((optchr = getopt_long(argc, argv, "ABC:DEFGHIJ:KLMNOPQ:RS$T:UVWX:Y:Z" - "abcdef:ghijklmno:pqr:s:tuvwxy!%_0", long_options, NULL)) != -1) { + "abcdef:gh?ijklmno:pqr:s:tuvwxy!%_0", long_options, NULL)) != -1) { switch (optchr) { #ifndef NANO_TINY case 'A': @@ -1999,9 +1999,6 @@ int main(int argc, char **argv) SET(SHOW_CURSOR); break; #endif - case 'h': - usage(); - exit(0); #ifndef NANO_TINY case 'i': SET(AUTOINDENT); @@ -2098,7 +2095,13 @@ int main(int argc, char **argv) SET(ZERO); break; #endif - default: + case 'h': + case '?': + /* If the option is valid, print the help text and exit. */ + if (!optopt) { + usage(); + exit(0); + } printf(_("Type '%s -h' for a list of available options.\n"), argv[0]); exit(1); }