Simplify CLI2::get* methods. allow rc=...

This commit is contained in:
taiyu 2018-11-15 12:26:32 -08:00 committed by Paul Beckingham
parent 8514071f19
commit d2b1662a39
2 changed files with 41 additions and 43 deletions

View file

@ -214,58 +214,56 @@ const std::string A2::dump () const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Static method. static
void CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc) const char* getValue (int argc, const char** argv, std::string arg)
{ {
for (int i = 0; i < argc; ++i) const auto is_arg = [&] (std::string s)
{ {
std::string raw = argv[i]; return s.size () > arg.size () + 1
if (raw == "--") && (s[arg.size ()] == ':' || s[arg.size ()] == '=')
return; && s.compare (0, arg.size (), arg) == 0;
};
// find last argument before --
auto last = std::make_reverse_iterator (argv);
auto first = std::make_reverse_iterator (
std::find (argv, argv + argc, std::string ("--")));
auto it = std::find_if (first, last, is_arg);
if (it == last)
return nullptr;
// return the string after : or =
return *it + arg.size () + 1;
}
if (raw.length () >= 3 && ////////////////////////////////////////////////////////////////////////////////
raw.substr (0, 3) == "rc:") // Static method.
{ bool CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc)
rc = raw.substr (3); {
const char* value = getValue (argc, argv, "rc");
home = "."; if (value == nullptr)
auto last_slash = rc._data.rfind ("/"); return false;
if (last_slash != std::string::npos) rc = File (value);
home = rc.parent (); if (rc._data.rfind ("/") != std::string::npos)
home = rc.parent ();
Context::getContext ().header (format ("Using alternate .taskrc file {1}", rc._data)); else
home = ".";
// Keep looping, because if there are multiple rc:file arguments, the last return true;
// one should dominate.
}
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Look for CONFIG data.location and initialize a Path object. // Look for CONFIG data.location and initialize a Path object.
// Static method. // Static method.
void CLI2::getDataLocation (int argc, const char** argv, Path& data) bool CLI2::getDataLocation (int argc, const char** argv, Path& data)
{ {
std::string location = Context::getContext ().config.get ("data.location"); const char* value = getValue (argc, argv, "rc.data.location");
if (location != "") if (value == nullptr)
data = location;
for (int i = 0; i < argc; ++i)
{ {
std::string raw = argv[i]; std::string location = Context::getContext ().config.get ("data.location");
if (raw == "--") if (location != "")
break; data = location;
return false;
if (raw.length () > 17 &&
raw.substr (0, 16) == "rc.data.location")
{
data = Directory (raw.substr (17));
Context::getContext ().header (format ("Using alternate data.location {1}", (std::string) data));
// Keep looping, because if there are multiple rc:file arguments, the last
// one should dominate.
}
} }
data = Directory (value);
return true;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -60,8 +60,8 @@ class CLI2
public: public:
static int minimumMatchLength; static int minimumMatchLength;
static void getOverride (int, const char**, std::string&, File&); static bool getOverride (int, const char**, std::string&, File&);
static void getDataLocation (int, const char**, Path&); static bool getDataLocation (int, const char**, Path&);
static void applyOverrides (int, const char**); static void applyOverrides (int, const char**);
public: public: