-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-template.cpp
114 lines (109 loc) · 2.33 KB
/
main-template.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
/////////////////////////////////////////////////////
// Template for option handling
//
#include <stdio.h>
#include <stdlib.h>
#include "getoptandval.h"
void help(); // Last in this file
//
// Case sensitive option strings
//
const char *g_optTable[] =
{
"file",
"max",
"min",
"w",
"h",
"Verbose", // First letter upper case -> Requires no argument if NOVALUE defined
"help"
};
const int g_optCount = sizeof(g_optTable) / sizeof(g_optTable[0]);
enum
{
INFILE,
MAX,
MIN,
W,
H,
VERBOSE,
HELP,
};
//
// -file charles.txt -min -11.61 -max +12.95 -Verbose -w 100 -h 64
//
int main(int argc, char* argv[])
{
char *optValPtr = NULL;
char* fileName = NULL; // output file name template
double max = 1.1;
double min = 1.1;
bool verbose = false;
int width = 10;
int height = 100;
int optNum;
while (--argc > 0) // First argument is program name
{
optNum = getOptAndVal(argc, argv, optValPtr);
//
// optValPtr is pointing to an actual value if the option requires a value
// OR in case the option needs no argument (first letter upper case), the option itself
//
switch (optNum)
{
case INFILE:
fileName = optValPtr;
break;
case MAX:
max = atof(optValPtr);
break;
case MIN:
min = atof(optValPtr);
break;
case W:
width = atoi(optValPtr);
break;
case H:
height = atoi(optValPtr);
break;
case VERBOSE:
verbose = true;
break;
case MISSINGVALUE: // Option value missing
printf("Missing value for %s\n", optValPtr);
help();
case NOOPTION: // No option given, just the value -> pgm filename
printf("No options given\n");
help();
case NOTFOUND: // -option not found
printf("%s illegal option\n", optValPtr);
help();
} // switch()
} // while
printf("file name: %s\n", fileName);
printf("Max: %g\n", max);
printf("Min: %g\n", min);
printf("Width: %d\n", width);
printf("Height: %d\n", height);
printf("Verbose: %d\n", verbose);
//
// Determine action
//
// if (fileName && verbose)
// ... etc ...
return 0;
}
void help()
{
printf("MyProgram 1.00\n"
" Usage:\n"
" [-file <file_name>]\n"
" [-max <max value>]\n"
" [-min <min value>]\n"
" [-w <width>]\n"
" [-h <height>]\n"
" [-Verbose]\n"
" [-help]\n"
);
exit(0);
}