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

@@ -213,59 +213,57 @@ const std::string A2::dump () const
return output + ' ' + atts + tags; return output + ' ' + atts + tags;
} }
////////////////////////////////////////////////////////////////////////////////
static
const char* getValue (int argc, const char** argv, std::string arg)
{
const auto is_arg = [&] (std::string s)
{
return s.size () > arg.size () + 1
&& (s[arg.size ()] == ':' || s[arg.size ()] == '=')
&& 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;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Static method. // Static method.
void CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc) bool CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc)
{ {
for (int i = 0; i < argc; ++i) const char* value = getValue (argc, argv, "rc");
{ if (value == nullptr)
std::string raw = argv[i]; return false;
if (raw == "--") rc = File (value);
return; if (rc._data.rfind ("/") != std::string::npos)
if (raw.length () >= 3 &&
raw.substr (0, 3) == "rc:")
{
rc = raw.substr (3);
home = ".";
auto last_slash = rc._data.rfind ("/");
if (last_slash != std::string::npos)
home = rc.parent (); home = rc.parent ();
else
Context::getContext ().header (format ("Using alternate .taskrc file {1}", rc._data)); home = ".";
return true;
// Keep looping, because if there are multiple rc:file arguments, the last
// 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)
{
const char* value = getValue (argc, argv, "rc.data.location");
if (value == nullptr)
{ {
std::string location = Context::getContext ().config.get ("data.location"); std::string location = Context::getContext ().config.get ("data.location");
if (location != "") if (location != "")
data = location; data = location;
return false;
for (int i = 0; i < argc; ++i)
{
std::string raw = argv[i];
if (raw == "--")
break;
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: