Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 58560e0

Browse files
committed
feat: add arg_parser to user_request
1 parent 9b0112c commit 58560e0

File tree

5 files changed

+226
-525
lines changed

5 files changed

+226
-525
lines changed

wperf/arg-parser.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31-
#include "arg-parser.h"
3231
#include <iostream>
3332
#include <codecvt>
3433
#include <locale>
3534
#include <cwchar>
3635
#include <vector>
3736
#include <sstream>
37+
#include "arg-parser.h"
38+
#include "output.h"
39+
#include "exception.h"
3840

3941
namespace ArgParser {
4042
arg_parser::arg_parser() {}
@@ -101,26 +103,26 @@ namespace ArgParser {
101103

102104
void arg_parser::print_help() const
103105
{
104-
std::wcout << L"NAME:\n"
106+
m_out.GetOutputStream() << L"NAME:\n"
105107

106108
<< L"\twperf - Performance analysis tools for Windows on Arm\n\n"
107109
<< L"\tUsage: wperf <command> [options]\n\n"
108110
<< L"SYNOPSIS:\n\n";
109111
for (auto& command : m_commands_list)
110112
{
111-
std::wcout << L"\t" << command->get_all_flags_string() << L"\n" << command->get_usage_text() << L"\n";
113+
m_out.GetOutputStream() << L"\t" << command->get_all_flags_string() << L"\n" << command->get_usage_text() << L"\n";
112114
}
113115

114-
std::wcout << L"OPTIONS:\n\n";
116+
m_out.GetOutputStream() << L"OPTIONS:\n\n";
115117
for (auto& flag : m_flags_list)
116118
{
117-
std::wcout << L" " << flag->get_help() << L"\n";
119+
m_out.GetOutputStream() << L" " << flag->get_help() << L"\n";
118120
}
119-
std::wcout << L"EXAMPLES:\n\n";
121+
m_out.GetOutputStream() << L"EXAMPLES:\n\n";
120122
for (auto& command : m_commands_list)
121123
{
122124
if (command->get_examples().empty()) continue;
123-
std::wcout << L" " << command->get_examples() << L"\n";
125+
m_out.GetOutputStream() << L" " << command->get_examples() << L"\n";
124126
}
125127
}
126128

@@ -155,8 +157,8 @@ namespace ArgParser {
155157
if (!additional_message.empty()) {
156158
error_message << additional_message << L"\n";
157159
}
158-
std::wcerr << error_message.str();
159-
throw std::invalid_argument("INVALID_ARGUMENT");
160+
m_out.GetErrorOutputStream() << error_message.str();
161+
throw fatal_exception("INVALID_ARGUMENT");
160162
}
161163

162164
#pragma endregion

wperf/arg-parser.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace ArgParser {
5858
MAN,
5959
NO_COMMAND
6060
};
61-
class arg_parser_arg_command : public arg_parser_arg_opt {
61+
class arg_parser_arg_command : public arg_parser_arg_pos {
6262

6363
public:
6464
arg_parser_arg_command(
@@ -67,8 +67,9 @@ namespace ArgParser {
6767
const std::wstring description,
6868
const std::wstring useage_text,
6969
const COMMAND_CLASS command,
70-
const wstr_vec examples
71-
) : arg_parser_arg_opt(name, alias, description), m_examples(examples), m_command(command), m_useage_text(useage_text) {};
70+
const wstr_vec examples,
71+
const int arg_count = 0
72+
) : arg_parser_arg_pos(name, alias, description, {}, arg_count), m_examples(examples), m_command(command), m_useage_text(useage_text) {};
7273
const COMMAND_CLASS m_command = COMMAND_CLASS::NO_COMMAND;
7374
const wstr_vec m_examples;
7475
const std::wstring m_useage_text;
@@ -180,7 +181,8 @@ namespace ArgParser {
180181
L"wperf man [--json]",
181182
COMMAND_CLASS::MAN,
182183
{
183-
}
184+
},
185+
1
184186
);
185187

186188
#pragma endregion
@@ -346,7 +348,7 @@ namespace ArgParser {
346348
{}
347349
);
348350
arg_parser_arg_pos iteration_arg = arg_parser_arg_pos::arg_parser_arg_pos(
349-
L"-i",
351+
L"-n",
350352
{},
351353
L"Number of consecutive counts in timeline mode (disabled by default).",
352354
{}

wperf/main.cpp

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "config.h"
5151
#include "perfdata.h"
5252
#include "disassembler.h"
53+
#include "arg-parser.h"
5354

5455
static bool no_ctrl_c = true;
5556

@@ -81,35 +82,27 @@ wmain(
8182
)
8283
{
8384
auto exit_code = EXIT_SUCCESS;
85+
ArgParser::arg_parser arg_parser;
86+
arg_parser.parse(argc, argv);
87+
8488

8589
user_request request;
8690
pmu_device pmu_device;
87-
wstr_vec raw_args;
8891

8992
LLVMDisassembler disassembler;
9093
bool spawned_process = false;
9194
HANDLE process_handle = NULL;
9295
PROCESS_INFORMATION pi;
9396
ZeroMemory(&pi, sizeof(pi));
9497

95-
//* Handle CLI options before we initialize PMU device(s)
96-
for (int i = 1; i < argc; i++)
97-
raw_args.push_back(argv[i]);
98-
99-
pmu_device.do_force_lock = user_request::is_force_lock(raw_args);
100-
101-
if (raw_args.size() == 1 && user_request::is_help(raw_args))
98+
if (arg_parser.m_command == ArgParser::COMMAND_CLASS::HELP)
10299
{
103-
user_request::print_help();
100+
arg_parser.print_help();
104101
goto clean_exit;
105102
}
106103

107-
if (raw_args.empty())
108-
{
109-
user_request::print_help_header();
110-
user_request::print_help_prompt();
111-
goto clean_exit;
112-
}
104+
pmu_device.do_force_lock = arg_parser.force_lock_opt.is_set();
105+
113106
//* Handle CLI options before we initialize PMU device(s)
114107

115108
try {
@@ -162,7 +155,7 @@ wmain(
162155
{
163156
struct pmu_device_cfg pmu_cfg;
164157
pmu_device.get_pmu_device_cfg(pmu_cfg);
165-
request.init(raw_args, pmu_cfg,
158+
request.init(arg_parser, pmu_cfg,
166159
pmu_device.builtin_metrics,
167160
pmu_device.get_product_groups_metrics_names(),
168161
pmu_events::extra_events);
@@ -1340,7 +1333,8 @@ wmain(
13401333
#if defined(ENABLE_ETW_TRACING_APP)
13411334
EventUnregisterWindowsPerf_App();
13421335
#endif
1343-
1336+
std::wstring hellp;
1337+
std::wcin >> hellp;
13441338
if(spawned_process)
13451339
{
13461340
TerminateProcess(pi.hProcess, 0);

0 commit comments

Comments
 (0)