This repository was archived by the owner on Jan 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patharg_parser.cpp
More file actions
175 lines (156 loc) · 6.08 KB
/
Copy patharg_parser.cpp
File metadata and controls
175 lines (156 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// BSD 3-Clause License
//
// Copyright (c) 2024, Arm Limited
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <iostream>
#include <codecvt>
#include <locale>
#include <cwchar>
#include <vector>
#include <sstream>
#include "arg_parser.h"
#include "output.h"
#include "exception.h"
namespace ArgParser {
arg_parser::arg_parser() {}
void arg_parser::parse(
_In_ const int argc,
_In_reads_(argc) const wchar_t* argv[]
)
{
wstr_vec raw_args;
for (int i = 1; i < argc; i++)
{
raw_args.push_back(argv[i]);
m_arg_array.push_back(argv[i]);
}
if (raw_args.size() == 0)
throw_invalid_arg(L"", L"warning: No arguments were found!");
#pragma region Command Selector
for (auto& command : m_commands_list)
{
if (command->parse(raw_args)) {
m_command = command->m_command;
raw_args.erase(raw_args.begin());
break;
}
}
if (m_command == COMMAND_CLASS::NO_COMMAND) {
throw_invalid_arg(raw_args.front(), L"warning: command not recognized!");
}
if (m_command == COMMAND_CLASS::HELP) {
return;
}
#pragma endregion
while (raw_args.size() > 0)
{
size_t initial_size = raw_args.size();
for (auto& current_flag : m_flags_list)
{
try
{
if (current_flag->parse(raw_args)) {
raw_args.erase(raw_args.begin(), raw_args.begin() + current_flag->get_arg_count() + 1);
parsed_args.push_back(current_flag);
}
}
catch (const std::exception& err)
{
throw_invalid_arg(raw_args.front(), L"Error: " + std::wstring(err.what(), err.what() + std::strlen(err.what())));
}
}
// if going over all the known arguments doesn't affect the size of the raw_args, then the command is unkown
if (initial_size == raw_args.size())
{
throw_invalid_arg(raw_args.front(), L"Error: Unrecognized command");
}
}
}
void arg_parser::print_help() const
{
m_out.GetOutputStream() << L"NAME:\n"
<< L"\twperf - Performance analysis tools for Windows on Arm\n\n"
<< L"\tUsage: wperf <command> [options]\n\n"
<< L"SYNOPSIS:\n\n";
for (auto& command : m_commands_list)
{
m_out.GetOutputStream() << L"\t" << command->get_all_flags_string() << L"\n" << command->get_usage_text() << L"\n";
}
m_out.GetOutputStream() << L"OPTIONS:\n\n";
for (auto& flag : m_flags_list)
{
m_out.GetOutputStream() << flag->get_help() << L"\n\n";
}
m_out.GetOutputStream() << L"EXAMPLES:\n\n";
for (auto& command : m_commands_list)
{
if (command->get_examples().empty()) continue;
m_out.GetOutputStream() << command->get_examples() << L"\n\n";
}
}
#pragma region error handling
void arg_parser::throw_invalid_arg(const std::wstring& arg, const std::wstring& additional_message) const
{
std::wstring command = L"wperf";
for (int i = 1; i < m_arg_array.size(); ++i) {
if (i > 0) {
command.append(L" ");
}
command.append(m_arg_array.at(i));
}
std::size_t pos = command.find(arg);
if (pos == 0 || pos == std::wstring::npos) {
pos = command.length();
}
std::wstring indicator(pos, L'~');
indicator += L'^';
std::wostringstream error_message;
error_message << L"Invalid argument detected:\n"
<< command << L"\n"
<< indicator << L"\n";
if (!additional_message.empty()) {
error_message << additional_message << L"\n";
}
m_out.GetErrorOutputStream() << error_message.str();
throw fatal_exception("INVALID_ARGUMENT");
}
#pragma endregion
wstring arg_parser_arg_command::get_usage_text() const
{
return arg_parser_add_wstring_behind_multiline_text(arg_parser_format_string_to_length(
m_useage_text + L"\n" + m_description), L"\t ") + L"\n";
}
wstring arg_parser_arg_command::get_examples() const
{
std::wstring example_output;
for (auto& example : m_examples) {
example_output += example + L"\n\n";
}
return arg_parser_add_wstring_behind_multiline_text(arg_parser_format_string_to_length(example_output), L"\t");
}
}