From: Joel Rosdahl Date: Tue, 10 Aug 2021 13:32:07 +0000 (+0200) Subject: feat: Make --config-path and --directory affect whole command line X-Git-Tag: v4.4~29 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=513c83faaafc3a9848cb4492961104e4f4768247;p=thirdparty%2Fccache.git feat: Make --config-path and --directory affect whole command line 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). --- diff --git a/doc/MANUAL.adoc b/doc/MANUAL.adoc index 859ee40e0..be679864d 100644 --- a/doc/MANUAL.adoc +++ b/doc/MANUAL.adoc @@ -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_:: diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 7f2d0f18e..dc1070a48 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -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(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(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);