-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLineParser.cpp
190 lines (153 loc) · 3.73 KB
/
CommandLineParser.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 Wolfgang Zukrigl
#include "CommandLineParser.h"
#include <iostream>
#include <exception>
#include <string>
using namespace std;
CommandLineParser::CommandLineParser(int argc, char* argv[])
{
bool modeSet = false;
m_x = 0;
m_y = 0;
m_noVersionInfo = false;
// copy command-line-arguments in vector
for (int i = 0; i < argc; i++)
{
m_argv.push_back(argv[i]);
}
// Strip path from program-name (argv[0])
changeCommandName();
// Process all Arguments
for (size_t i = 1; i < m_argv.size(); i++)
{
size_t len = strlen(m_argv[i]);
if (len < 2 || (m_argv[i][0] != '/' && m_argv[i][0] != '-') )
{
printHelpAndExit(EXIT_FAILURE);
}
switch (m_argv[i][1])
{
case 'X':
case 'x':
m_x = parseXorY(argv[i]);
m_mode = CommandLineParser::Mode::SET_DISPLAY_MODE;
modeSet = true;
break;
case 'Y':
case 'y':
m_y = parseXorY(argv[i]);
m_mode = CommandLineParser::Mode::SET_DISPLAY_MODE;
modeSet = true;
break;
case 'S':
case 's':
m_mode = CommandLineParser::Mode::SHOW_CURRENT_DISPLAY_SETTING;
modeSet = true;
break;
case 'L':
case 'l':
m_mode = CommandLineParser::Mode::LIST_ALL_DISPLAY_MODES;
modeSet = true;
break;
case 'V':
case 'v':
m_noVersionInfo = true;
break;
case '?':
case 'h':
case 'H':
printHelpAndExit(EXIT_SUCCESS);
break;
default:
printHelpAndExit(EXIT_FAILURE);
break;
}
}
if ( ! modeSet || m_mode == Mode::SET_DISPLAY_MODE && (m_x == 0 || m_y == 0) )
{
printHelpAndExit(EXIT_FAILURE);
}
printVersionInfoIfDesired();
}
int CommandLineParser::getX()
{
return m_x;
}
int CommandLineParser::getY()
{
return m_y;
}
CommandLineParser::Mode CommandLineParser::getMode()
{
return m_mode;
}
void CommandLineParser::printHelpAndExit(int exitStatus)
{
printVersionInfoIfDesired();
cout << "Usage: " << endl;
cout << m_argv[0] << " { /X:px /Y:px | /S | /L | /? | /H } [/V]" << endl;
cout << endl;
cout << " /X:nnn Width in pixels." << endl;
cout << " /Y:nnn Height in pixels." << endl;
cout << " /S Show current display settings." << endl;
cout << " /L List all display modes." << endl;
cout << " /V Does NOT display version information." << endl;
cout << " /H or /? Displays usage information." << endl;
cout << endl;
cout << "Ex: '" << m_argv[0] << " /x:640 /y:480' Changes resolution to 640 x 480" << endl << endl;
exit(exitStatus);
}
////////////////////////////////////////////////////////
// Helper functions
////////////////////////////////////////////////////////
/**
* argv[0] is the full path to the executed command (exe-File). Strip the path
* and just leave commandname.exe
*/
void CommandLineParser::changeCommandName()
{
int lastOccurence = -1;
int pos = 0;
while (m_argv[0][pos] != '\0')
{
if (m_argv[0][pos] == '/' || m_argv[0][pos] == '\\')
lastOccurence = pos;
pos++;
}
m_argv[0] = &(m_argv[0][lastOccurence+1]);
}
/**
* arg is one of the command-line-args /X:nnn or /Y:nnn. The number nnn is parsed and returned
*/
int CommandLineParser::parseXorY(char* arg)
{
if (strlen(arg) < 4)
{
printHelpAndExit(EXIT_FAILURE);
}
if (arg[2] != ':' && arg[2] != '=')
{
printHelpAndExit(EXIT_FAILURE);
}
int val = 0;
try
{
string valAsString(&(arg[3]));
val = stoi(valAsString);
}
catch (...)
{
printHelpAndExit(EXIT_FAILURE);
}
return val;
}
void CommandLineParser::printVersionInfoIfDesired()
{
if (!m_noVersionInfo)
{
cout << m_argv[0] << endl;
cout << "Version 1.0" << endl;
cout << endl;
}
}