-
Notifications
You must be signed in to change notification settings - Fork 2
/
lson.cpp
94 lines (69 loc) · 2.51 KB
/
lson.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
//==================================================================================================
// lson.cpp
//
// This is the first draft of an LSON lexer/parser. Eventually it will grow into a C++ library and a
// command-line utility.
//==================================================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string>
struct Parameters
{
bool someFlag { false };
bool printHelp { false };
};
// Function Declarations
bool getParameters (Parameters& params, int argc, wchar_t* argv[]);
//--------------------------------------------------------------------------------------------------
int wmain (int argc, wchar_t* argv[])
{
Parameters params;
if (!getParameters(params, argc, argv)) return -1;
wprintf (L"Someflag = %s\n", params.someFlag ? L"true" : L"false");
if (params.printHelp) {
wprintf (L"Help!\n");
}
return 0;
}
//--------------------------------------------------------------------------------------------------
bool getParameters (Parameters& params, int argc, wchar_t* argv[])
{
// Processes command-line arguments, and loads resulting values into the Parameters structure.
// This function returns true if all parameters are valid.
for (auto i=1; i < argc; ++i) {
auto argptr = argv[i];
// Handle Windows standard "/?" help switch.
if (0 == _wcsicmp(argptr, L"/?")) {
params.printHelp = true;
return true;
}
auto unexpectedArgument = false;
if (argptr[0] != L'-') {
// Non-dash Arguments
unexpectedArgument = true;
} else if (argptr[1] == L'-') {
// Double-Dash Options
argptr += 2;
if (0 == _wcsicmp(argptr, L"help")) {
params.printHelp = true;
return true;
} else {
unexpectedArgument = true;
}
} else {
// Single-Dash Options
argptr += 1;
if (*argptr == L'h' || *argptr == L'H')
params.printHelp = true;
else if (*argptr == L'f')
params.someFlag = true;
else
unexpectedArgument = true;
}
if (unexpectedArgument) {
fwprintf (stderr, L"ERROR: Unexpected argument (%s).\n", argv[i]);
return false;
}
}
return true;
}