]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Make --config-path and --directory affect whole command line
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 10 Aug 2021 13:32:07 +0000 (15:32 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 10 Aug 2021 16:36:39 +0000 (18:36 +0200)
Command line options are processed strictly in order, which means that

    ccache -d dir -s

is not the same as

    ccache -s -d dir

which is not very intuitive. There are also planned features where is
doesn’t make sense to have to put “non-command options” before the
“command options”.

Improve this by processing command line options in two passes: first
non-command options and then command options (still in order).

doc/MANUAL.adoc
src/core/mainoptions.cpp

index 859ee40e094d48bf6704c79401d5bcca5a0702ee..be679864dc0f6c8988292c1dfa3bbd653d8edb90 100644 (file)
@@ -87,17 +87,17 @@ compiler's documentation.
 
 *--config-path* _PATH_::
 
-    Let the subsequent command line options operate on configuration file
-    _PATH_ instead of the default. Using this option has the same effect as
-    setting the environment variable `CCACHE_CONFIGPATH` temporarily.
+    Let the command line options operate on configuration file _PATH_ instead of
+    the default. Using this option has the same effect as setting the
+    environment variable `CCACHE_CONFIGPATH` temporarily.
 
 *-d*, *--directory* _PATH_::
 
-    Let the subsequent command line options operate on cache directory _PATH_
-    instead of the default. For example, to show statistics for a cache
-    directory at `/shared/ccache` you can run `ccache -d /shared/ccache -s`.
-    Using this option has the same effect as setting the environment variable
-    `CCACHE_DIR` temporarily.
+    Let the command line options operate on cache directory _PATH_ instead of
+    the default. For example, to show statistics for a cache directory at
+    `/shared/ccache` you can run `ccache -d /shared/ccache -s`. Using this
+    option has the same effect as setting the environment variable `CCACHE_DIR`
+    temporarily.
 
 *--evict-older-than* _AGE_::
 
index 7f2d0f18ea5d9ac77b71bb94b19f249abfd25351..dc1070a48b9b1ce5e71d639ef81f64daf4ae337d 100644 (file)
@@ -232,6 +232,27 @@ int
 process_main_options(int argc, const char* const* argv)
 {
   int c;
+
+  // First pass: Handle non-command options that affect command options.
+  while ((c = getopt_long(argc,
+                          const_cast<char* const*>(argv),
+                          options_string,
+                          long_options,
+                          nullptr))
+         != -1) {
+    switch (c) {
+    case 'd': // --directory
+      Util::setenv("CCACHE_DIR", optarg);
+      break;
+
+    case CONFIG_PATH:
+      Util::setenv("CCACHE_CONFIGPATH", optarg);
+      break;
+    }
+  }
+
+  // Second pass: Handle command options in order.
+  optind = 1;
   while ((c = getopt_long(argc,
                           const_cast<char* const*>(argv),
                           options_string,
@@ -244,6 +265,12 @@ process_main_options(int argc, const char* const* argv)
     std::string arg = optarg ? optarg : std::string();
 
     switch (c) {
+    case CONFIG_PATH:
+      break; // Already handled in the first pass.
+
+    case 'd': // --directory
+      break;  // Already handled in the first pass.
+
     case CHECKSUM_FILE: {
       Checksum checksum;
       Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY));
@@ -254,10 +281,6 @@ process_main_options(int argc, const char* const* argv)
       break;
     }
 
-    case CONFIG_PATH:
-      Util::setenv("CCACHE_CONFIGPATH", arg);
-      break;
-
     case DUMP_MANIFEST:
       return Manifest::dump(arg, stdout) ? 0 : 1;
 
@@ -338,10 +361,6 @@ process_main_options(int argc, const char* const* argv)
       break;
     }
 
-    case 'd': // --directory
-      Util::setenv("CCACHE_DIR", arg);
-      break;
-
     case 'h': // --help
       PRINT(stdout, USAGE_TEXT, CCACHE_NAME, CCACHE_NAME);
       exit(EXIT_SUCCESS);