flatten 20260225
This commit is contained in:
42
tjp/core/args/Args.cpp
Normal file
42
tjp/core/args/Args.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#ifdef TJP_CORE_HEADER_ONLY
|
||||
#pragma once
|
||||
#endif
|
||||
#include <tjp/core/header_only/compile.h>
|
||||
|
||||
#include "Args.h"
|
||||
#include <tjp/core/string/starts_with.hpp>
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
std::map<std::string, std::string> mapArgs(int argc, const char *argv[])
|
||||
{
|
||||
std::map<std::string, std::string> mapped;
|
||||
static const std::string prefix = "--";
|
||||
for (int i=1; i<argc; ++i)
|
||||
{
|
||||
std::string s = argv[i];
|
||||
if (starts_with(s, prefix))
|
||||
{
|
||||
bool hasValue = (i+1)<argc && !starts_with(std::string(argv[i+1]), prefix);
|
||||
std::string key = s.substr(2);
|
||||
if (hasValue)
|
||||
{
|
||||
std::string value = argv[i+1];
|
||||
mapped[key] = value;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
mapped[key] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mapped;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
67
tjp/core/args/Args.h
Normal file
67
tjp/core/args/Args.h
Normal file
@@ -0,0 +1,67 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tjp/core/string/from_string.hpp>
|
||||
#include <tjp/core/string/cmp_case.hpp>
|
||||
|
||||
#include <tjp/core/containers/Optional.hpp>
|
||||
#include <map>
|
||||
|
||||
namespace tjp {
|
||||
namespace core {
|
||||
|
||||
typedef std::map<std::string, std::string> Args;
|
||||
|
||||
std::map<std::string, std::string> mapArgs(int argc, const char *argv[]);
|
||||
|
||||
template<typename T>
|
||||
inline T argOrDefault(const std::map<std::string, std::string> &args, const std::string &key, const T &default_)
|
||||
{
|
||||
auto i = args.find(key);
|
||||
if (i == args.end())
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
|
||||
return core::from_string<T>(i->second);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool argOrDefault(const std::map<std::string, std::string> &args, const std::string &key, const bool &default_)
|
||||
{
|
||||
auto i = args.find(key);
|
||||
if (i == args.end())
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
|
||||
return equal_case(i->second, "true");
|
||||
}
|
||||
|
||||
inline std::string argOrDefault_(const std::map<std::string, std::string> &args, const std::string &key, const char *default_)
|
||||
{
|
||||
auto i = args.find(key);
|
||||
if (i == args.end())
|
||||
{
|
||||
return default_;
|
||||
}
|
||||
|
||||
return i->second;
|
||||
}
|
||||
|
||||
inline Optional<std::string> argOptional(const std::map<std::string, std::string> &args, const std::string &key)
|
||||
{
|
||||
auto i = args.find(key);
|
||||
if (i != args.end())
|
||||
return i->second;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
#ifdef TJP_CORE_HEADER_ONLY
|
||||
#include "Args.cpp"
|
||||
#endif
|
||||
60
tjp/core/args/ArgsFile.cpp
Normal file
60
tjp/core/args/ArgsFile.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#ifdef TJP_CORE_HEADER_ONLY
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <tjp/core/header_only/compile.h>
|
||||
|
||||
#include "ArgsFile.h"
|
||||
#include <tjp/core/exception/Exception.hpp>
|
||||
|
||||
#include <tjp/core/string/String.hpp>
|
||||
#include <tjp/core/string/StringView.hpp>
|
||||
#include <tjp/core/string/trim.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
Optional<Args> readArgsFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename);
|
||||
if (!file)
|
||||
throw Exception("Failed to open file");
|
||||
|
||||
Args result;
|
||||
String line_;
|
||||
|
||||
while (std::getline(file, line_))
|
||||
{
|
||||
auto line = trim_copy(StringView(line_));
|
||||
// Skip empty lines and comments
|
||||
if (line.empty() || line[0] == '#')
|
||||
continue;
|
||||
|
||||
auto pos = line.find('=');
|
||||
if (pos == line.npos)
|
||||
continue;
|
||||
|
||||
auto key = line.substr(0, pos);
|
||||
auto value = line.substr(pos + 1);
|
||||
|
||||
result[String(key)] = String(value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
TJP_CORE_HEADER_ONLY_INLINE
|
||||
void mergeArgs(Args &args, Args &rhs)
|
||||
{
|
||||
args.merge(rhs);
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
16
tjp/core/args/ArgsFile.h
Normal file
16
tjp/core/args/ArgsFile.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Args.h"
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
Optional<Args> readArgsFile(const String &fileName);
|
||||
void mergeArgs(Args &args, Args &rhs);
|
||||
|
||||
} // namespace
|
||||
|
||||
#ifdef TJP_CORE_HEADER_ONLY
|
||||
#include "ArgsFile.cpp"
|
||||
#endif
|
||||
Reference in New Issue
Block a user