gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master da5158b0: Options: warning printed when option


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master da5158b0: Options: warning printed when option value is empty string
Date: Tue, 23 Aug 2022 11:17:13 -0400 (EDT)

branch: master
commit da5158b0bd4fdadb1eefd520a6ef3536860c91ee
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Options: warning printed when option value is empty string
    
    Until now, when the user gave a value to a long option using the '=' sign
    (for example '--hdu=1'), the value should immediately come after the '='
    sign. However, it can happen that the user mistakenly puts a space after
    the equal sign (for example '--hdu= 1'). In such situations, the program
    will usually crash with a confusing error message, or even worse, produce
    unreasonable outputs. So Gnuastro's program should print a warning in such
    cases to let the user know how to fix the problem.
    
    With this commit, such a warning is now printed! When the value of an
    option is an empty string, a complete error message is printed that will
    let the user know about a potential problem.
    
    This bug was reported by Faezeh Bijarchian.
    
    This fixes bug #62944.
---
 NEWS          |  2 ++
 lib/options.c | 23 ++++++++++++++++++++++-
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 7400b86d..820595df 100644
--- a/NEWS
+++ b/NEWS
@@ -136,6 +136,8 @@ See the end of the file for license conditions.
               Infante-Sainz.
   bug #62937: psf-scale-factor not being quiet, when requested. Found and
               fixed by Sepideh Eskandarlou.
+  bug #62944: No warning when the option value isn't immediately after the
+              equal sign in long format. Found by Faezeh Bijarchian.
 
 
 
diff --git a/lib/options.c b/lib/options.c
index 1dbf0579..d8ad5094 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -2111,8 +2111,29 @@ gal_options_set_from_key(int key, char *arg, struct 
argp_option *options,
       /* Check if the key corresponds to this option. */
       if( options[i].key==key )
         {
+          /* It may happen that the user puts a space after the '=' sign of
+             a long option (for example '--hdu= 1'). In this case, Argp
+             will give an empty string to the option and associate the
+             value as a new argument, leading to a confusing error message
+             that only one argument should be given (by programs that need
+             a single argument). So we should check for this condition
+             here. */
+          if(arg && arg[0]=='\0' && cp->quiet==0)
+            error(EXIT_SUCCESS, 0, "WARNING: no value given to the "
+                  "'--%s' option. In other words, its value is an "
+                  "empty string. This may result in undefined behavior "
+                  "(usually a crash in an un-expected part of the "
+                  "program). It can happen when you use an undefined "
+                  "shell variable or if there is an empty space after "
+                  "the '=' sign of long options (for example '--hdu= 1', "
+                  "note the space between the '=' and the '1'; the "
+                  "correct format in such cases is either '--hdu=1' "
+                  "or '--hdu 1'). To supress this warning, please use "
+                  "the '--quiet' (or '-q') option before this option",
+                  options[i].name);
+
           /* When options are read from keys (by this function), they are
-             read from the command-line. On the commandline, the last
+             read from the command-line. On the command-line, the last
              invokation of the option is important. Especially in contexts
              like scripts, this is important because you can change a given
              command-line option (that is not a linked list) by calling it



reply via email to

[Prev in Thread] Current Thread [Next in Thread]