Permalink
Browse files

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

  • Loading branch information...
taiyu-len authored and pbeckingham committed Nov 15, 2018
1 parent 8514071 commit d2b1662a3972d7eb7894c5b0df7d746e74dd6945
Showing with 41 additions and 43 deletions.
  1. +39 −41 src/CLI2.cpp
  2. +2 −2 src/CLI2.h
@@ -214,58 +214,56 @@ const std::string A2::dump () const
}

////////////////////////////////////////////////////////////////////////////////
// Static method.
void CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc)
static
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];
if (raw == "--")
return;

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 ();

Context::getContext ().header (format ("Using alternate .taskrc file {1}", rc._data));
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;
}

// Keep looping, because if there are multiple rc:file arguments, the last
// one should dominate.
}
}
////////////////////////////////////////////////////////////////////////////////
// Static method.
bool CLI2::getOverride (int argc, const char** argv, std::string& home, File& rc)
{
const char* value = getValue (argc, argv, "rc");
if (value == nullptr)
return false;
rc = File (value);
if (rc._data.rfind ("/") != std::string::npos)
home = rc.parent ();
else
home = ".";
return true;
}

////////////////////////////////////////////////////////////////////////////////
// Look for CONFIG data.location and initialize a Path object.
// 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");
if (location != "")
data = location;

for (int i = 0; i < argc; ++i)
const char* value = getValue (argc, argv, "rc.data.location");
if (value == nullptr)
{
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.
}
std::string location = Context::getContext ().config.get ("data.location");
if (location != "")
data = location;
return false;
}
data = Directory (value);
return true;
}

////////////////////////////////////////////////////////////////////////////////
@@ -60,8 +60,8 @@ class CLI2
public:
static int minimumMatchLength;

static void getOverride (int, const char**, std::string&, File&);
static void getDataLocation (int, const char**, Path&);
static bool getOverride (int, const char**, std::string&, File&);
static bool getDataLocation (int, const char**, Path&);
static void applyOverrides (int, const char**);

public:

0 comments on commit d2b1662

Please sign in to comment.