Skip to content

Commit 9d9a07c

Browse files
committed
first load
1 parent dab35db commit 9d9a07c

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

.vscode/launch.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(lldb) Launch",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/a.out",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": true,
17+
"MIMode": "lldb",
18+
"preLaunchTask": "make"
19+
},
20+
{
21+
"name": "(gdb) Launch",
22+
"type": "cppdbg",
23+
"request": "launch",
24+
"program": "${workspaceFolder}/a.out",
25+
"args": [
26+
"-1",
27+
"ls",
28+
"-2",
29+
"wc"
30+
],
31+
"stopAtEntry": false,
32+
"cwd": "${workspaceFolder}",
33+
"environment": [],
34+
"externalConsole": true,
35+
"internalConsoleOptions": "openOnSessionStart",
36+
"MIMode": "gdb"
37+
}
38+
]
39+
}

.vscode/tasks.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "make",
8+
"type": "shell",
9+
"command": "make",
10+
"args": [
11+
"a.out"
12+
],
13+
"group": {
14+
"kind": "build",
15+
"isDefault": true
16+
},
17+
"problemMatcher": "$gcc"
18+
},
19+
{
20+
"label": "clean",
21+
"type": "shell",
22+
"command": "make",
23+
"args": [
24+
"clean"
25+
],
26+
"problemMatcher": "$gcc"
27+
}
28+
]
29+
}

main.cpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* A modest getopt tutorial - example really.
2+
Perry Kivolowitz
3+
Carthage College
4+
*/
5+
6+
#include <iostream>
7+
#include <getopt.h>
8+
#include <string>
9+
10+
using namespace std;
11+
12+
/* This program is a modest tutorial on the use of getopt.
13+
14+
gettopt is a very old C library that facilitates the parsing of command line arguments.
15+
This tutorial (more like an example) is not complete. You can find documentation on getopt
16+
online.
17+
*/
18+
19+
20+
int main(int argc, char * argv[]) {
21+
/* getopt is typically isolated in a function or method specifically intended to process
22+
command line options. Below, I have defined some variables of different types and will
23+
pass these to the getopt managing function. Notice I have initialized these variables with
24+
there default values.
25+
26+
That is, if the corresponding option is not specified on the command line, the default
27+
value is retained.
28+
29+
One of these variables will be *required* - you'll see how this is implemented.
30+
*/
31+
32+
int an_integer = 17;
33+
float a_float = 12.345;
34+
bool a_bool = false;
35+
char * a_c_string = nullptr;
36+
string a_cpp_string_this_will_be_required;
37+
/* Forward references to supporting functions. Of course,
38+
the forward references would not be needed using a c++ class or by reordering these
39+
functions.
40+
*/
41+
void PrintUsage();
42+
bool HandleOptions(int argc, char ** argv, int &, bool &, char **, string &, float &);
43+
44+
if (!HandleOptions(argc, argv, an_integer, a_bool, &a_c_string, a_cpp_string_this_will_be_required, a_float)) {
45+
PrintUsage();
46+
return 1;
47+
}
48+
cout << "An integer: " << an_integer << endl;
49+
cout << "A bool: " << a_bool << endl;
50+
cout << "A c-string: " << ((a_c_string == nullptr) ? "" : a_c_string) << endl;
51+
cout << "A string: " << a_cpp_string_this_will_be_required << endl;
52+
cout << "A float: " << a_float << endl;
53+
return 0;
54+
}
55+
56+
void PrintUsage() {
57+
cerr << "Usage:" << endl;
58+
cerr << "-s string this is requred." << endl;
59+
cerr << "-i integer optional - an integer" << endl;
60+
cerr << "-b optional - if present a boolean is set to true" << endl;
61+
cerr << "-c string optional - a c string" << endl;
62+
cerr << "-s string optional - a c++ string" << endl;
63+
cerr << "-f float optional - a float" << endl;
64+
cerr << "Note that without care, a string is just one \"word\"" << endl;
65+
}
66+
67+
bool HandleOptions(int argc, char ** argv, int & an_int, bool & a_bool, char ** a_c_string, string & a_cpp_string, float & a_float) {
68+
int c;
69+
70+
/* getopt must be passed argc and argv - after all these are how command line arguments are passed to programs.
71+
72+
The third parameter is a format string defining the valid command line options as well as if a specific
73+
option requires a parameter. This is controlled by whether or not a colon (':') comes after the option
74+
character. In the following example, 's' requires a parameter but 'b' does not.
75+
76+
When there are no more members of argv to attempt parsing, getopt return -1.
77+
78+
Notice there is no requirement to keep to any particular order of options.
79+
*/
80+
81+
while ((c = getopt(argc, argv, "hf:i:bc:s:")) != -1) {
82+
switch (c) {
83+
default:
84+
case 'h':
85+
// Short circut forcing the usage to be printed by returning false from this function.
86+
return false;
87+
88+
case 'i':
89+
an_int = atoi(optarg);
90+
break;
91+
92+
case 'b':
93+
a_bool = true;
94+
break;
95+
96+
case 'c':
97+
*a_c_string = optarg;
98+
break;
99+
100+
case 's':
101+
a_cpp_string = string(optarg);
102+
break;
103+
104+
case 'f':
105+
a_float = atof(optarg);
106+
break;
107+
}
108+
}
109+
110+
/* The s option is to be required. If the string variable still has length 0, the s option was not given.
111+
Note that this is just one construction of the test. I can use this construction because there is only
112+
one way HandleOptions can fail.
113+
*/
114+
return a_cpp_string.size() > 0;
115+
}

makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CFLAGS = -Wall -g -std=c++11 -DDEBUG
2+
LFLAGS =
3+
CC = g++
4+
5+
srcs = $(wildcard *.cpp)
6+
objs = $(srcs:.cpp=.o)
7+
deps = $(srcs:.cpp=.d)
8+
9+
a.out: $(objs)
10+
$(CC) $^ -o $@ $(LFLAGS)
11+
12+
%.o: %.cpp
13+
$(CC) -MMD -MP $(CFLAGS) -c $< -o $@
14+
15+
.PHONY: clean
16+
17+
# $(RM) is rm -f by default
18+
clean:
19+
$(RM) $(objs) $(deps) a.out core
20+
21+
-include $(deps)

wkspc.code-workspace

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {
8+
"files.associations": {
9+
"ostream": "cpp"
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)