OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
CommandLineArgs.cpp
Go to the documentation of this file.
1#include "CommandLineArgs.h"
2#include "CommandLineArgs.h"
3#include "CommandLineArgs.h"
4#include "CommandLineArgs.h"
5#include "CommandLineArgs.h"
6#include "CommandLineArgs.h"
7#include "CommandLineArgs.h"
8#include "CommandLineArgs.h"
9#include "CommandLineArgs.h"
10#include "CommandLineArgs.h"
11#include "CommandLineArgs.h"
12#include <exception>
13#include <stdexcept>
14#include "str_utils.h"
15#include <sstream>
16#include <iostream>
17#include <cstring>
18
19
20namespace Utils
21{
22 CommandLineArgs::CommandLineArgs(int argc, char* argv[])
23 {
24 // save arguments in std::string vector
25 std::copy(argv, argv + argc, std::back_inserter(m_vecArguments));
26 parse();
27 }
28
29 CommandLineArgs::CommandLineArgs(const std::vector<std::string>& vecArgv)
30 : m_vecArguments(vecArgv)
31 {
32 parse();
33 }
34
35 bool CommandLineArgs::boolArg(const std::string& name, bool defaultValue) const
36 {
37 auto strB = getArgValue(name);
38 if (strB.length() > 0)
39 {
40 bool b;
41 std::istringstream(strB) >> std::boolalpha >> b;
42 return b;
43 }
44
45 return defaultValue;
46 }
47
48 std::string CommandLineArgs::stringArg(const std::string& name, const std::string& defaultValue) const
49 {
50 auto str = getArgValue(name);
51 if (str.length() > 0)
52 {
53 return str;
54 }
55 return defaultValue;
56 }
57
58 int CommandLineArgs::intArg(const std::string& name, int defaultValue) const
59 {
60 auto strI = getArgValue(name);
61 if (strI.length() > 0)
62 {
63 return std::stoi(strI);
64 }
65 return defaultValue;
66 }
67
68 double CommandLineArgs::doubleArg(const std::string& name, double defaultValue) const
69 {
70 if (const auto strD = getArgValue(name); !strD.empty())
71 {
72 return std::stod(strD);
73 }
74 return defaultValue;
75 }
76
77 std::filesystem::path CommandLineArgs::executable() const
78 {
79 return std::filesystem::path(getArgValue(EXECUTABLE_ARG_NAME));
80 }
81
82 std::filesystem::path CommandLineArgs::executableDirectory() const
83 {
84 return executable().parent_path();
85 }
86
88 {
89 return executable().filename().string();
90 }
91
93 {
94 auto pos = executableName().find(EXE_EXTENSION);
95 return (pos != std::string::npos &&
96 pos == executableName().length()-strlen(EXE_EXTENSION));
97 }
98
100 {
101 return !isWindows();
102 }
103
104 std::string CommandLineArgs::getArgValue(const std::string& name) const
105 {
106 auto findIt = m_mapArguments.find(name);
107 if (findIt == m_mapArguments.end()) return "";
108 return findIt->second;
109 }
110
112 {
113 for (std::size_t i = 0; i < m_vecArguments.size(); i++)
114 {
115 auto& arg = m_vecArguments[i];
116 if (arg.length() > 0)
117 {
118 if (i == 0)
119 {
120 m_mapArguments["executable"] = arg;
121 }
122 else if (arg[0] == '-' || arg[0] == '/')
123 {
124 Utils::str_ltrim(arg, '-');
125 Utils::str_ltrim(arg, '/');
126
127 std::string nextArg;
128 if (i + 1 < m_vecArguments.size())
129 {
130 nextArg = m_vecArguments[i + 1];
131 }
132
133 if (nextArg.length() > 0 &&
134 nextArg[0] != '-' &&
135 nextArg[0] != '/')
136 {
137 m_mapArguments[arg] = nextArg;
138 i++;
139 }
140 else
141 {
142 m_mapArguments[arg] = "true";
143 }
144
145 //if (i + 1 < m_vecArguments.size() &&
146 // m_vecArguments[i + 1].length() > 0 &&
147 // (m_vecArguments[i + 1][0] != '-' || m_vecArguments[i + 1][0] != '/'))
148 //{
149 //
150 // m_mapArguments[arg] = m_vecArguments[i+++1];
151 //}
152 //else
153 //{
154 // m_mapArguments[arg] = "true";
155 //}
156 }
157 }
158 }
159 }
161 {
162 std::cout << getUsageString();
163 }
164}
std::string executableName() const
bool boolArg(const std::string &name, bool defaultValue) const
double doubleArg(const std::string &name, double defaultValue) const
std::filesystem::path executableDirectory() const
std::vector< std::string > m_vecArguments
std::string getArgValue(const std::string &name) const
const char * EXECUTABLE_ARG_NAME
int intArg(const std::string &name, int defaultValue) const
CommandLineArgs(int argc, char *argv[])
std::filesystem::path executable() const
std::string stringArg(const std::string &name, const std::string &defaultValue) const
std::map< std::string, std::string > m_mapArguments
std::string & str_ltrim(std::string &s)
Definition str_utils.cpp:16