Skip to content

Commit 1059def

Browse files
committed
feat: add abstract arg class
1 parent 78a8b21 commit 1059def

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

parser/arg-parser-arg.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "arg-parser-arg.h"
2+
3+
arg_parser_arg::arg_parser_arg(
4+
const std::wstring name,
5+
const std::vector<std::wstring> alias,
6+
const std::wstring description,
7+
const int arg_count
8+
) : m_name(name), m_aliases(alias), m_description(description), m_arg_count(arg_count) {};
9+
10+
inline bool arg_parser_arg::operator==(const std::wstring& other_arg) const
11+
{
12+
return is_match(other_arg);
13+
}
14+
15+
bool arg_parser_arg::is_match(const std::wstring& other_arg) const
16+
{
17+
return !other_arg.empty() && (other_arg == m_name || std::find(m_aliases.begin(), m_aliases.end(), other_arg) != m_aliases.end());
18+
}
19+
20+
std::wstring arg_parser_arg::get_help() const
21+
{
22+
return m_name + L" or " + get_alias_string() + L"/n " + m_description;
23+
}
24+
25+
std::wstring arg_parser_arg::get_usage_text() const
26+
{
27+
return m_description;
28+
}
29+
30+
arg_parser_arg arg_parser_arg::add_alias(std::wstring new_alias)
31+
{
32+
m_aliases.push_back(new_alias);
33+
return *this;
34+
}
35+
36+
int arg_parser_arg::get_arg_count() const
37+
{
38+
return m_arg_count;
39+
}
40+
41+
std::wstring arg_parser_arg::get_name() const
42+
{
43+
return m_name;
44+
}
45+
46+
std::wstring arg_parser_arg::get_alias_string() const
47+
{
48+
// convert alias vector to wstring
49+
std::wstring alias_string;
50+
for (auto& m_alias : m_aliases) {
51+
alias_string.append(m_alias);
52+
}
53+
return alias_string;
54+
}
55+
56+
arg_parser_arg arg_parser_arg::add_check_func(std::function<bool(const std::wstring&)> check_func)
57+
{
58+
m_check_funcs.push_back(check_func);
59+
return *this;
60+
}
61+
62+
void arg_parser_arg::set_is_parsed()
63+
{
64+
m_is_parsed = true;
65+
}
66+
67+
bool arg_parser_arg::parse(std::vector<std::wstring> arg_vect)
68+
{
69+
if (m_arg_count == -1 && arg_vect.size() > 0) m_arg_count = arg_vect.size() - 1;
70+
71+
if (arg_vect.size() < m_arg_count + 1)
72+
return false;
73+
74+
if (!is_match(arg_vect[0]))
75+
return false;
76+
if (m_arg_count == 0)
77+
{
78+
set_is_parsed();
79+
return true;
80+
}
81+
82+
for (int i = 1; i < m_arg_count + 1; ++i)
83+
{
84+
for (auto& check_func : m_check_funcs)
85+
{
86+
if (!check_func(arg_vect[i]))
87+
return false;
88+
}
89+
m_values.push_back(arg_vect[i]);
90+
91+
}
92+
set_is_parsed();
93+
return true;
94+
}
95+

parser/arg-parser-arg.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include <set>
5+
#include <functional>
6+
7+
struct arg_parser_arg {
8+
const std::wstring m_name;
9+
std::vector<std::wstring> m_aliases{};
10+
const std::wstring m_description;
11+
12+
int m_arg_count; // -1 for variable number of arguments
13+
bool m_is_parsed = false;
14+
std::vector<std::wstring> m_values{};
15+
16+
std::vector <std::function<bool(const std::wstring&)>> m_check_funcs = {};
17+
18+
inline bool operator==(const std::wstring& other) const;
19+
20+
public:
21+
arg_parser_arg(
22+
const std::wstring name,
23+
const std::vector<std::wstring> alias,
24+
const std::wstring description,
25+
const int arg_count
26+
);
27+
bool is_match(const std::wstring& arg) const;
28+
virtual std::wstring get_help() const;
29+
virtual std::wstring get_usage_text() const;
30+
arg_parser_arg add_alias(std::wstring new_alias);
31+
int get_arg_count() const;
32+
std::wstring get_name() const;
33+
std::wstring get_alias_string() const;
34+
arg_parser_arg add_check_func(std::function<bool(const std::wstring&)> check_func);
35+
void set_is_parsed();
36+
bool parse(std::vector<std::wstring> arg_vect);
37+
};
38+
39+
class arg_parser_arg_opt : arg_parser_arg {
40+
arg_parser_arg_opt(
41+
const std::wstring name,
42+
const std::vector<std::wstring> alias,
43+
const std::wstring description,
44+
const int arg_count
45+
) : arg_parser_arg(name, alias, description, 0) {};
46+
};
47+
48+
class arg_parser_arg_pos : arg_parser_arg {
49+
arg_parser_arg_pos(
50+
const std::wstring name,
51+
const std::vector<std::wstring> alias,
52+
const std::wstring description,
53+
const int arg_count
54+
) : arg_parser_arg(name, alias, description, arg_count) {};
55+
};
56+
57+
class arg_parser_arg_command : arg_parser_arg {
58+
arg_parser_arg_command(
59+
const std::wstring name,
60+
const std::vector<std::wstring> alias,
61+
const std::wstring description,
62+
const int arg_count,
63+
const std::wstring examples
64+
) : arg_parser_arg(name, alias, description, 1) {};
65+
const std::wstring m_examples;
66+
};

parser/arg-parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <string>
3838
#include <set>
3939
#include <unordered_map>
40+
#include "arg.h"
4041

4142
using namespace std;
4243

parser/parser.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@
131131
</ItemDefinitionGroup>
132132
<ItemGroup>
133133
<ClCompile Include="arg-parser.cpp" />
134+
<ClCompile Include="arg-parser-arg.cpp" />
134135
<ClCompile Include="main.cpp" />
135136
</ItemGroup>
136137
<ItemGroup>
137138
<ClInclude Include="arg-parser.h" />
139+
<ClInclude Include="arg-parser-arg.h" />
138140
</ItemGroup>
139141
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
140142
<ImportGroup Label="ExtensionTargets">

parser/parser.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
<ClCompile Include="arg-parser.cpp">
2222
<Filter>Source Files</Filter>
2323
</ClCompile>
24+
<ClCompile Include="arg-parser-arg.cpp">
25+
<Filter>Source Files</Filter>
26+
</ClCompile>
2427
</ItemGroup>
2528
<ItemGroup>
2629
<ClInclude Include="arg-parser.h">
2730
<Filter>Header Files</Filter>
2831
</ClInclude>
32+
<ClInclude Include="arg-parser-arg.h">
33+
<Filter>Header Files</Filter>
34+
</ClInclude>
2935
</ItemGroup>
3036
</Project>

0 commit comments

Comments
 (0)