gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 6e94c13 6/7: All programs: empty string argume


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 6e94c13 6/7: All programs: empty string arguments are now ignored
Date: Sun, 16 May 2021 15:39:29 -0400 (EDT)

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

    All programs: empty string arguments are now ignored
    
    Until now, when an empty shell variable was given to any of the programs,
    they would crash by saying a file called '' (empty string) doesn't
    exist. This can happen a lot in a shell script, but this error isn't clear
    and even confusing! A more natural error is that "no input file
    specified". I noticed this most recently while working on the final changes
    in the radial profile script.
    
    With this commit, before writing the argument inside any of the programs,
    they will first check to see if its an empty string or not. If it is indeed
    empty, they will ignore the argument, thus letting the program abort with a
    "no input" error.
---
 bin/TEMPLATE/ui.c    |  7 ++++---
 bin/arithmetic/ui.c  |  7 ++++---
 bin/buildprog/ui.c   |  7 ++++---
 bin/convertt/ui.c    |  7 ++++---
 bin/convolve/ui.c    |  6 ++++--
 bin/cosmiccal/ui.c   |  9 ++++++---
 bin/crop/ui.c        | 11 ++++++++---
 bin/fits/ui.c        | 18 +++++++++++-------
 bin/match/ui.c       | 14 ++++++++------
 bin/mkcatalog/ui.c   |  7 ++++---
 bin/mknoise/ui.c     |  7 ++++---
 bin/mkprof/ui.c      |  7 ++++---
 bin/noisechisel/ui.c |  7 ++++---
 bin/query/ui.c       |  7 ++++---
 bin/segment/ui.c     |  7 ++++---
 bin/statistics/ui.c  |  7 ++++---
 bin/table/ui.c       |  7 ++++---
 bin/warp/ui.c        |  7 ++++---
 18 files changed, 89 insertions(+), 60 deletions(-)

diff --git a/bin/TEMPLATE/ui.c b/bin/TEMPLATE/ui.c
index 9117a1e..e69df1d 100644
--- a/bin/TEMPLATE/ui.c
+++ b/bin/TEMPLATE/ui.c
@@ -161,16 +161,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/arithmetic/ui.c b/bin/arithmetic/ui.c
index 5722558..80d2874 100644
--- a/bin/arithmetic/ui.c
+++ b/bin/arithmetic/ui.c
@@ -192,13 +192,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      gal_list_str_add(&p->tokens, arg, 0);
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0') gal_list_str_add(&p->tokens, arg, 0);
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/buildprog/ui.c b/bin/buildprog/ui.c
index 3172968..5521e9c 100644
--- a/bin/buildprog/ui.c
+++ b/bin/buildprog/ui.c
@@ -182,13 +182,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      gal_list_str_add(&p->sourceargs, arg, 0);
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0') gal_list_str_add(&p->sourceargs, arg, 0);
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/convertt/ui.c b/bin/convertt/ui.c
index 98fe093..8be4123 100644
--- a/bin/convertt/ui.c
+++ b/bin/convertt/ui.c
@@ -190,13 +190,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      gal_list_str_add(&p->inputnames, arg, 0);
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0') gal_list_str_add(&p->inputnames, arg, 0);
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/convolve/ui.c b/bin/convolve/ui.c
index 44e8ed0..99367da 100644
--- a/bin/convolve/ui.c
+++ b/bin/convolve/ui.c
@@ -165,13 +165,15 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->filename)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->filename=arg;
+        if(arg[0]!='\0') p->filename=arg;
       break;
 
 
diff --git a/bin/cosmiccal/ui.c b/bin/cosmiccal/ui.c
index 641dc98..fb1cdf6 100644
--- a/bin/cosmiccal/ui.c
+++ b/bin/cosmiccal/ui.c
@@ -177,11 +177,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      argp_error(state, "currently %s doesn't take any arguments",
-                 PROGRAM_NAME);
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0')
+        argp_error(state, "currently %s doesn't take any arguments",
+                   PROGRAM_NAME);
       break;
 
 
diff --git a/bin/crop/ui.c b/bin/crop/ui.c
index c4142ea..58a8a2f 100644
--- a/bin/crop/ui.c
+++ b/bin/crop/ui.c
@@ -191,11 +191,16 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      gal_list_str_add(&p->inputs, arg, 0);
-      ++p->numin;
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0')
+        {
+          gal_list_str_add(&p->inputs, arg, 0);
+          ++p->numin;
+        }
       break;
 
 
diff --git a/bin/fits/ui.c b/bin/fits/ui.c
index a6ba964..697145e 100644
--- a/bin/fits/ui.c
+++ b/bin/fits/ui.c
@@ -172,14 +172,18 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
-    /* Read the non-option tokens (arguments): */
+    /* Read the non-option tokens (arguments). */
     case ARGP_KEY_ARG:
-      /* Only FITS files are acceptable. */
-      if( gal_fits_file_recognized(arg) )
-        gal_list_str_add(&p->input, arg, 1);
-      else
-        argp_error(state, "%s is not a recognized FITS file", arg);
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0')
+        {
+          if( gal_fits_file_recognized(arg) )
+            gal_list_str_add(&p->input, arg, 1);
+          else
+            argp_error(state, "%s is not a recognized FITS file", arg);
+        }
       break;
 
 
diff --git a/bin/match/ui.c b/bin/match/ui.c
index 40c57a9..01b6ca4 100644
--- a/bin/match/ui.c
+++ b/bin/match/ui.c
@@ -163,19 +163,21 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
-    /* Read the non-option tokens (arguments): */
+    /* Read the non-option tokens (arguments). */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->input1name)
         {
           if(p->input2name)
-            argp_error(state, "only two arguments (input files) should be "
-                       "given, not any more");
+            argp_error(state, "only two arguments (input files) "
+                       "should be given, not any more");
           else
-            p->input2name=arg;
+            { if(arg[0]!='\0') p->input2name=arg; }
         }
       else
-        p->input1name=arg;
+        { if(arg[0]!='\0') p->input1name=arg; }
       break;
 
 
diff --git a/bin/mkcatalog/ui.c b/bin/mkcatalog/ui.c
index c2c753c..4f4cc23 100644
--- a/bin/mkcatalog/ui.c
+++ b/bin/mkcatalog/ui.c
@@ -187,16 +187,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->objectsfile)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->objectsfile=arg;
+        if(arg[0]!='\0') p->objectsfile=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/mknoise/ui.c b/bin/mknoise/ui.c
index 7a0fc95..0a7661a 100644
--- a/bin/mknoise/ui.c
+++ b/bin/mknoise/ui.c
@@ -177,16 +177,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/mkprof/ui.c b/bin/mkprof/ui.c
index 93b04a6..d12ede4 100644
--- a/bin/mkprof/ui.c
+++ b/bin/mkprof/ui.c
@@ -256,16 +256,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->catname)
         argp_error(state, "only one argument (input catalog) may be given");
       else
-        p->catname=arg;
+        if(arg[0]!='\0') p->catname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/noisechisel/ui.c b/bin/noisechisel/ui.c
index 96e1419..f303215 100644
--- a/bin/noisechisel/ui.c
+++ b/bin/noisechisel/ui.c
@@ -178,16 +178,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/query/ui.c b/bin/query/ui.c
index 204fa8e..e03fe4f 100644
--- a/bin/query/ui.c
+++ b/bin/query/ui.c
@@ -184,13 +184,14 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
-      p->databasestr=arg;
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
+      if(arg[0]!='\0') p->databasestr=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/segment/ui.c b/bin/segment/ui.c
index b72a15a..c4549d3 100644
--- a/bin/segment/ui.c
+++ b/bin/segment/ui.c
@@ -184,16 +184,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/statistics/ui.c b/bin/statistics/ui.c
index 38c36a7..fc771e9 100644
--- a/bin/statistics/ui.c
+++ b/bin/statistics/ui.c
@@ -186,16 +186,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 483336a..7f7d7f1 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -180,16 +180,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->filename)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->filename=arg;
+        if(arg[0]!='\0') p->filename=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
diff --git a/bin/warp/ui.c b/bin/warp/ui.c
index f846516..710c393 100644
--- a/bin/warp/ui.c
+++ b/bin/warp/ui.c
@@ -172,16 +172,17 @@ parse_opt(int key, char *arg, struct argp_state *state)
   /* Set the key to this option. */
   switch(key)
     {
-
     /* Read the non-option tokens (arguments): */
     case ARGP_KEY_ARG:
+      /* The user may give a shell variable that is empty! In that case
+         'arg' will be an empty string! We don't want to account for such
+         cases (and give a clear error that no input has been given). */
       if(p->inputname)
         argp_error(state, "only one argument (input file) should be given");
       else
-        p->inputname=arg;
+        if(arg[0]!='\0') p->inputname=arg;
       break;
 
-
     /* This is an option, set its value. */
     default:
       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);



reply via email to

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