gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 41302705: Configuration files: --config-prefix


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 41302705: Configuration files: --config-prefix=STR allows optional prefix
Date: Sun, 7 May 2023 19:14:32 -0400 (EDT)

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

    Configuration files: --config-prefix=STR allows optional prefix
    
    Until now, if we had several configuration files for multiple instances of
    the same program, it was not possible to load them all into the shell
    (because same options would overwrite each other's values).
    
    With this commit a new '--config-prefix' common option has been added to
    all the programs. It is given a string that can be in the prefix of options
    in separate configuration files. This solves the problem above: allowing
    complex pipelines to easily load all configuration files into their shell
    for later usage of the options.
    
    This completes task #16365.
---
 NEWS                               |  4 ++++
 doc/gnuastro.texi                  | 35 +++++++++++++++++++++++++++++++++++
 lib/checkset.c                     | 35 +++++++++++++++++++++++++++++++++++
 lib/gnuastro-internal/checkset.h   |  3 +++
 lib/gnuastro-internal/commonopts.h | 13 +++++++++++++
 lib/gnuastro-internal/options.h    |  2 ++
 lib/options.c                      |  3 ++-
 7 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 2c3618f5..04a7dd25 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,10 @@ See the end of the file for license conditions.
     loaded into a shell (with 'source') or Make (with 'include'). Until
     now, only white-space characters were allowed as separators of option
     names and values.
+  --config-prefix=STR: Given string can be a prefix of options in
+    configuration files. Added with the point above, this allows loading
+    the different configuration files of different instances of the same
+    program without overwriting them. See the example in the book.
 
 ** Removed features
 
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index 307975c6..b8e577fd 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -11220,6 +11220,41 @@ Only options that are parsed after this option are 
printed, so to see the parsin
 @cindex Debug
 This is a very good option to confirm where the value of each option is has 
been defined in scenarios where there are multiple configuration files (for 
debugging).
 
+@item --config-prefix=STR
+Accept option names in configuration files that start with the given prefix 
and ignore the prefix when comparing options within the configuration file with 
the program's option names.
+This gives unique features to Gnuastro's configuration files, especially in 
large pipelines.
+
+Since order matters when reading custom configuration files, this option 
should be called @strong{before} the @option{--config} option(s).
+
+Let's demonstrate this with the simple scenario below.
+You have multiple configuration files for different instances of one program 
(let's assume @file{nc-a.conf} and @file{nc-b.conf}).
+At the same time, want to load all the option names/values into your shell as 
environment variables (for example with @code{source nc-*.conf}).
+This happens when you want to use the options value in other parts of the your 
script also.
+
+If you don't use @code{--config-prefix}, the shell will over-write the common 
option values between the configuration files.
+But thanks to @code{--config-prefix}, you can give a different prefix to the 
different instances of the same option in different configuration files.
+
+@example
+$ cat nc-a.conf
+a_tilesize=20,20
+
+$ cat nc-b.conf
+b_tilesize=40,40
+
+## Load configuration files as shell scripts (to define the
+## option name and values as shell variables with values).
+## Just note that 'source' only takes one file at a time.
+$ for c in nc-*.conf; do source $c; done
+
+$ astnoisechisel img.fits \
+                 --config=nc-a.conf --config-prefix=a_
+$ echo "NoiseChisel run with --tilesize=$a_tilesize"
+
+$ astnoisechisel img.fits \
+                 --config=nc-b.conf --config-prefix=b_
+$ echo "NoiseChisel run with --tilesize=$b_tilesize"
+@end example
+
 @item -S
 @itemx --setdirconf
 Update the current directory configuration file for the Gnuastro program and 
quit.
diff --git a/lib/checkset.c b/lib/checkset.c
index 463fbba0..abed6aa1 100644
--- a/lib/checkset.c
+++ b/lib/checkset.c
@@ -446,6 +446,41 @@ gal_checkset_dataset_name(char *filename, char *hdu)
 
 
 
+/* Remove the given prefix from the name, then compare. If the prefix is
+   NULL, then this becomes a basic string comparison '!strcmp(a,b)'. */
+int
+gal_checkset_noprefix_isequal(char *string, char *prefix,
+                              const char *tocompare)
+{
+  size_t plen;
+
+  /* If the input string is empty, then this option has nothing to do
+     (should return false): like NaN in math, two NULLs are not equal in
+     this scenario. */
+  if(string==NULL) return 0;
+  if(tocompare==NULL) return 0;
+
+  /* Do the comparison. */
+  if(prefix)
+    {
+      /* Check if 'string' starts with 'prefix'. When it does start with
+         the prefix, then do the comparison for the string after the
+         prefix. */
+      plen=strlen(prefix);
+      if( !strncmp(string, prefix, plen) )
+        return !strcmp(string+plen, tocompare);
+    }
+
+  /* If a prefix wasn't given, OR the string didn't start with the given
+     prefix, then ignore the prefix and simply compare the full input
+     string and the 'tocompare' string. */
+  return !strcmp(string, tocompare);
+}
+
+
+
+
+
 
 
 
diff --git a/lib/gnuastro-internal/checkset.h b/lib/gnuastro-internal/checkset.h
index 88b4c81a..ba02e213 100644
--- a/lib/gnuastro-internal/checkset.h
+++ b/lib/gnuastro-internal/checkset.h
@@ -97,6 +97,9 @@ gal_checkset_dataset_name(char *filename, char *hdu);
 char *
 gal_checkset_timestamp(char *filename, char *newext);
 
+int
+gal_checkset_noprefix_isequal(char *string, char *prefix,
+                              const char *tocompare);
 
 
 /**************************************************************/
diff --git a/lib/gnuastro-internal/commonopts.h 
b/lib/gnuastro-internal/commonopts.h
index 6bbf7aec..78e6d624 100644
--- a/lib/gnuastro-internal/commonopts.h
+++ b/lib/gnuastro-internal/commonopts.h
@@ -449,6 +449,19 @@ struct argp_option gal_commonopts_options[] =
       GAL_OPTIONS_NOT_SET,
       gal_options_check_config
     },
+    {
+      "config-prefix",
+      GAL_OPTIONS_KEY_CONFIGPREFIX,
+      "STR",
+      0,
+      "Custom prefix of option names config files.",
+      GAL_OPTIONS_GROUP_OPERATING_MODE,
+      &cp->configprefix,
+      GAL_TYPE_STRING,
+      GAL_OPTIONS_RANGE_ANY,
+      GAL_OPTIONS_NOT_MANDATORY,
+      GAL_OPTIONS_NOT_SET,
+    },
     {
       "setdirconf",
       GAL_OPTIONS_KEY_SETDIRCONF,
diff --git a/lib/gnuastro-internal/options.h b/lib/gnuastro-internal/options.h
index 794d32eb..1d4b117e 100644
--- a/lib/gnuastro-internal/options.h
+++ b/lib/gnuastro-internal/options.h
@@ -118,6 +118,7 @@ enum options_common_keys
   GAL_OPTIONS_KEY_SEARCHIN,
   GAL_OPTIONS_KEY_LASTCONFIG,
   GAL_OPTIONS_KEY_CHECKCONFIG,
+  GAL_OPTIONS_KEY_CONFIGPREFIX,
   GAL_OPTIONS_KEY_TABLEFORMAT,
   GAL_OPTIONS_KEY_ONLYVERSION,
   GAL_OPTIONS_KEY_WORKOVERCH,
@@ -215,6 +216,7 @@ struct gal_options_common_params
   uint8_t           setusrconf; /* To write teh user config config file.  */
   uint8_t           lastconfig; /* This is the last configuration file.   */
   uint8_t          checkconfig; /* Check config files and values.         */
+  char           *configprefix; /* Custom prefix in --config files.       */
 
   /* Output files. */
   gal_fits_list_key_t  *okeys;  /* Configuration as FITS keys in output.  */
diff --git a/lib/options.c b/lib/options.c
index edfb22a0..1b0c2ccd 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -2577,7 +2577,8 @@ options_set_from_name(char *name, char *arg,  struct 
argp_option *options,
   for(i=0;1;++i)
     {
       /* Check if the key corresponds to this option. */
-      if( options[i].name && !strcmp(options[i].name, name) )
+      if( gal_checkset_noprefix_isequal(name, cp->configprefix,
+                                        options[i].name) )
         {
           /* Ignore this option and its value. This can happen in several
              situations:



reply via email to

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