gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 08147ce 062/125: All mandatory options not giv


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 08147ce 062/125: All mandatory options not given are listed with error
Date: Sun, 23 Apr 2017 22:36:38 -0400 (EDT)

branch: master
commit 08147ce46be43936ba2dcff1a04198bf53375b67
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    All mandatory options not given are listed with error
    
    Until now, as soon as a mandatory option without a value was found, an
    error would be printed. So if multiple necessary options were not given,
    then multiple runs were necessary to find all of them. But now, the names
    and descriptions of such options are passed to linked lists and after
    parsing all the options, they are all printed together, so in one run the
    user can see all the necessary options. Also, the description of the option
    is printed to help the user know what the problem was.
    
    While doing this, the check for the FITS output name and `tabletype' option
    in the Table program was moved only to when the `tabletype' parameter has
    been set.
    
    Also, it was noticed that in one of the rows, the spacing between the
    string column of the numerical string was not enough. This is now
    corrected.
---
 bin/table/ui.c        | 55 ++++++++++++++++++++++++------------------
 lib/gnuastro/data.h   |  2 ++
 lib/options.c         | 67 +++++++++++++++++++++++++++++++++++++++++++++++----
 lib/options.h         |  9 +++++++
 tests/table/table.txt |  2 +-
 5 files changed, 105 insertions(+), 30 deletions(-)

diff --git a/bin/table/ui.c b/bin/table/ui.c
index 5df9323..163f857 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -59,15 +59,6 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 /**************************************************************/
 /***************       Sanity Check         *******************/
 /**************************************************************/
-static void
-ui_option_is_mandatory(char *name)
-{
-  error(EXIT_FAILURE, 0, "`%s' option is mandatory", name);
-}
-
-
-
-
 
 /* This function is in charge of reading the option values from the arrays
    of the `argp_option' structure and put them into each program's own data
@@ -88,6 +79,7 @@ static void
 ui_read_check_only_options(struct tableparams *p)
 {
   size_t i;
+  struct gal_linkedlist_stll *namell=NULL, *docll=NULL;
 
   /* Put the program's option values into the structure. */
   for(i=0; !gal_options_is_last(&options[i]); ++i)
@@ -99,39 +91,49 @@ ui_read_check_only_options(struct tableparams *p)
           gal_linked_list_copy_stll(options[i].value, &p->columns);
           break;
 
+
         case ARGS_OPTION_SEARCHIN_KEY:
           if(options[i].value)
             p->searchin=gal_table_string_to_searchin(options[i].value);
-          else ui_option_is_mandatory((char *)options[i].name);
+          else
+            gal_options_add_to_not_given(&options[i], &namell, &docll);
           break;
 
+
         case ARGS_OPTION_IGNORECASE_KEY:
           p->ignorecase = *(unsigned char *)options[i].value;
           break;
 
 
+
         /* Output */
         case ARGS_OPTION_TABLETYPE_KEY:
 
-          /* Set the tabletype parameter. */
+          /* If `tabletype' has a value, put it in. */
           if(options[i].value)
-            p->tabletype=gal_table_string_to_type(options[i].value);
+            {
+              /* Set the value into the structure. */
+              p->tabletype=gal_table_string_to_type(options[i].value);
+
+              /* If the output name was set and is a FITS file, make sure
+                 that the type of the table is not a `txt'. */
+              if( p->cp.output && gal_fits_name_is_fits(p->cp.output)
+                  && ( p->tabletype !=GAL_TABLE_TYPE_AFITS
+                       && p->tabletype !=GAL_TABLE_TYPE_BFITS ) )
+                error(EXIT_FAILURE, 0, "desired output file `%s' is a FITS "
+                      "file, but `tabletype' is not a FITS table type. "
+                      "Please set it to `fits-ascii', or `fits-binary'",
+                      p->cp.output);
+            }
+
+          /* In Table, `tabletype' is only mandatory when the output is a
+             FITS file, A text table output only has one possible type. */
           else if( gal_fits_name_is_fits(p->cp.output) )
-            ui_option_is_mandatory((char *)options[i].name);
-
-
-          /* If the output name was set and is a FITS file, make sure that
-             the type of the table is not a `txt'. */
-          if( p->cp.output && gal_fits_name_is_fits(p->cp.output)
-              && ( p->tabletype !=GAL_TABLE_TYPE_AFITS
-                   && p->tabletype !=GAL_TABLE_TYPE_BFITS ) )
-            error(EXIT_FAILURE, 0, "desired output file `%s' is a FITS "
-                  "file, but `tabletype' is not a FITS table type. "
-                  "Please set it to `fits-ascii', or `fits-binary'",
-                  p->cp.output);
+            gal_options_add_to_not_given(&options[i], &namell, &docll);
           break;
 
 
+
         /* Operating mode */
         case ARGS_OPTION_INFORMATION_KEY:
           if(options[i].value)
@@ -143,6 +145,11 @@ ui_read_check_only_options(struct tableparams *p)
           error(EXIT_FAILURE, 0, "option key %d not recognized in "
                 "`fill_params_from_options'", options[i].key);
         }
+
+  /* If any of the mandatory options were not given, then print an error
+     listing them and abort. */
+  if(namell)
+    gal_options_mandatory_error(namell, docll);
 }
 
 
diff --git a/lib/gnuastro/data.h b/lib/gnuastro/data.h
index 17f14ec..7f77034 100644
--- a/lib/gnuastro/data.h
+++ b/lib/gnuastro/data.h
@@ -33,6 +33,8 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 #include <wcslib/wcs.h>
 #include <gsl/gsl_complex.h>
 
+#include <gnuastro/linkedlist.h>
+
 
 /* C++ Preparations */
 #undef __BEGIN_C_DECLS
diff --git a/lib/options.c b/lib/options.c
index fc70873..c2bc18d 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -65,6 +65,63 @@ gal_options_is_category_title(struct argp_option *option)
 
 
 
+
+void
+gal_options_add_to_not_given(struct argp_option *option,
+                             struct gal_linkedlist_stll **namell,
+                             struct gal_linkedlist_stll **docll)
+{
+  gal_linkedlist_add_to_stll(docll, (char *)option->doc, 0);
+  gal_linkedlist_add_to_stll(namell, (char *)option->name, 0);
+}
+
+
+
+
+
+void
+gal_options_mandatory_error(struct gal_linkedlist_stll *namell,
+                            struct gal_linkedlist_stll *docll)
+{
+  int namewidth=0;
+  char info[2000], *name, *doc;
+  struct gal_linkedlist_stll *tmp;
+
+  /* Get the maximum width of the given names: */
+  for(tmp=namell; tmp!=NULL; tmp=tmp->next)
+    if( strlen(tmp->v) > namewidth ) namewidth=strlen(tmp->v);
+
+  /* Print the introductory information. */
+  sprintf(info, "to continue, the following options need a value:\n");
+  sprintf(info+strlen(info), "(parenthesis after option name contain its "
+          "description)\n\n");
+
+  /* Print the list of options along with their description. */
+  while(namell!=NULL)
+    {
+      gal_linkedlist_pop_from_stll(&namell, &name);
+      gal_linkedlist_pop_from_stll(&docll, &doc);
+      sprintf(info+strlen(info), "  %-*s (%s\b)\n", namewidth+4, name, doc);
+    }
+  sprintf(info+strlen(info), "\n");
+
+  /* Print suggestions, way to solve it. */
+  sprintf(info+strlen(info), "Use the command-line or a configuration file "
+          "to set value(s).\n\nFor a complete description of command-line "
+          "options and configuration files, please see the \"Options\" and "
+          "\"Configuration files\" section of the Gnuastro book "
+          "respectively. You can read them on the command-line by running "
+          "the following commands (type `SPACE' to flip through pages, type "
+          "`Q' to return to the command-line):\n\n"
+          "  info gnuastro Options\n"
+          "  info gnuastro \"Configuration files\"\n");
+
+  error(EXIT_FAILURE, 0, "%s", info);
+}
+
+
+
+
 /* The modified `argp_option' structure contains a void pointer, depending
    on the type of value inside it, you need to the pointer differently. */
 void
@@ -1034,10 +1091,10 @@ options_print_all_in_group(struct argp_option *options, 
int groupint,
           for(tmp=options[i].value; tmp!=NULL; tmp=tmp->next)
             {
               fprintf(fp, " %-*s ", namewidth, options[i].name);
-                options_print_any_type(tmp->v, GAL_DATA_TYPE_STRING,
-                                       valuewidth, fp);
-                fprintf(fp, "# %s\n", options[i].doc);
-              }
+              options_print_any_type(tmp->v, GAL_DATA_TYPE_STRING,
+                                     valuewidth, fp);
+              fprintf(fp, "(%s\b)\n", options[i].doc);
+            }
 
         /* Normal types. */
         else
@@ -1045,7 +1102,7 @@ options_print_all_in_group(struct argp_option *options, 
int groupint,
             fprintf(fp, " %-*s ", namewidth, options[i].name);
             options_print_any_type(options[i].value, options[i].type,
                                    valuewidth, fp);
-            fprintf(fp, "# %s\n", options[i].doc);
+            fprintf(fp, "(%s\b)\n", options[i].doc);
           }
       }
 }
diff --git a/lib/options.h b/lib/options.h
index a1a01c5..21f813f 100644
--- a/lib/options.h
+++ b/lib/options.h
@@ -114,6 +114,15 @@ int
 gal_options_is_category_title(struct argp_option *option);
 
 void
+gal_options_add_to_not_given(struct argp_option *option,
+                             struct gal_linkedlist_stll **namell,
+                             struct gal_linkedlist_stll **docll);
+
+void
+gal_options_mandatory_error(struct gal_linkedlist_stll *namell,
+                            struct gal_linkedlist_stll *docll);
+
+void
 gal_options_free(struct argp_option *options);
 
 
diff --git a/tests/table/table.txt b/tests/table/table.txt
index 5c5f670..a2ad869 100644
--- a/tests/table/table.txt
+++ b/tests/table/table.txt
@@ -39,7 +39,7 @@
 5    -5    |string column.     |  3469       -3469  5  -5   8923  -99       
-7.32384e4        5
 6    -6    |characters beyond  |  20821  -20821  6  -6   9823  -99     nan     
          6
 7    -7    the last one will be   20822  -20822  7  -7   7232  9999   
8.3343e-5         7
-8    -8    read as a number.   20823  -20823  8  -8   921   2.3     
934892734.82      8
+8    -8    read as a number.     20823  -20823  8  -8   921   2.3     
934892734.82      8
 9    -9    address@hidden@    60983  -25722  9  -9   8127  -99          
3474924.489252    9
 10  -10    Characters%^&*()~           62182  -30100 10  -10  8287  7.3e-4     
  -3467.3432e5      10
 11  -12 no data                65500  -32700 11  -11  999    8.73E2       nan  
             11



reply via email to

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