aboutsummaryrefslogtreecommitdiff
path: root/tests/default-options
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2020-12-07 22:13:06 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2020-12-08 18:00:41 +0300
commit871273b374f75306a6c79c4ec067f2e4ce338172 (patch)
treed16336ce5aa46a5ee6ab0b51323d625bcbd13caa /tests/default-options
parent819741778f6ad75b5b104d2107a8fd9e30d4d4e5 (diff)
Add proper support for option files option to load_default_options()
Diffstat (limited to 'tests/default-options')
-rw-r--r--tests/default-options/driver.cxx64
-rw-r--r--tests/default-options/testscript33
2 files changed, 68 insertions, 29 deletions
diff --git a/tests/default-options/driver.cxx b/tests/default-options/driver.cxx
index 574e002..b3f66bf 100644
--- a/tests/default-options/driver.cxx
+++ b/tests/default-options/driver.cxx
@@ -35,7 +35,7 @@ using namespace std;
using namespace butl;
// Usage: argv[0] [-f <file>] [-d <start-dir>] [-s <sys-dir>] [-h <home-dir>]
-// [-x <extra-dir>] [-e] [-t] <cmd-options>
+// [-x <extra-dir>] [-a] [-e] [-t] <cmd-options>
//
// Parse default options files, merge them with the command line options, and
// print the resulting options to STDOUT one per line. Note that the options
@@ -78,56 +78,61 @@ main (int argc, const char* argv[])
class scanner
{
public:
- scanner (const string& f): ifs_ (f, fdopen_mode::in, ifdstream::badbit) {}
+ scanner (const string& f, const string& option)
+ : option_ (option) {load (path (f));}
bool
more ()
{
- if (peeked_)
- return true;
-
- if (!eof_)
- eof_ = ifs_.peek () == ifdstream::traits_type::eof ();
-
- return !eof_;
+ return i_ < args_.size ();
}
string
peek ()
{
assert (more ());
-
- if (peeked_)
- return *peeked_;
-
- string s;
- getline (ifs_, s);
-
- peeked_ = move (s);
- return *peeked_;
+ return args_[i_];
}
string
next ()
{
assert (more ());
+ return args_[i_++];
+ }
+
+ private:
+ void
+ load (const path& f)
+ {
+ ifdstream is (f, fdopen_mode::in, ifdstream::badbit);
- string s;
- if (peeked_)
+ for (string l; !eof (getline (is, l)); )
{
- s = move (*peeked_);
- peeked_ = nullopt;
- }
- else
- getline (ifs_, s);
+ if (option_ && *option_ == l)
+ {
+ assert (!eof (getline (is, l)));
- return s;
+ // If the path of the file being parsed is not simple and the path
+ // of the file that needs to be loaded is relative, then complete
+ // the latter using the former as a base.
+ //
+ path p (l);
+
+ if (!f.simple () && p.relative ())
+ p = f.directory () / p;
+
+ load (p);
+ }
+ else
+ args_.push_back (move (l));
+ }
}
private:
- ifdstream ifs_;
- bool eof_ = false;
- optional<string> peeked_;
+ optional<string> option_;
+ vector<string> args_;
+ size_t i_ = 0;
};
enum class unknow_mode
@@ -264,6 +269,7 @@ main (int argc, const char* argv[])
cerr << (overwrite ? "overwriting " : "loading ")
<< (remote ? "remote " : "local ") << f << endl;
},
+ "--options-file",
args);
}
catch (const invalid_argument& e)
diff --git a/tests/default-options/testscript b/tests/default-options/testscript
index b168ca9..09bb2ec 100644
--- a/tests/default-options/testscript
+++ b/tests/default-options/testscript
@@ -345,3 +345,36 @@
EOO
}
}
+
+: options-file
+:
+{
+ d = work/.build2;
+ mkdir -p work/.build2;
+
+ cat <<EOI >=$d/foo;
+ --foo
+ --options-file
+ bar
+ --fox
+ EOI
+
+ cat <<EOI >=$d/bar;
+ --bar
+ --options-file
+ baz
+ --box
+ EOI
+
+ cat <<EOI >=$d/baz;
+ --baz
+ EOI
+
+ $* -d $~/work -f foo >>EOO
+ --foo
+ --bar
+ --baz
+ --box
+ --fox
+ EOO
+}