In this homework assignment, you create your project repository and start out with a simple, multi-file C++ program that looks at command-line arguments.
When you run a program at the command line, you can give the program arguments. For example,
./msdscript --help
will run the program msdscript
in the current directory with the argument --help
. At the C++ level, arguments are passed to a program as the argc
and argv
arguments to main
:
int main(int argc, char* argv[]) {
....
}
The argc
passed to main
is always at least 1, because the first “argument” (at index 0) is the name of the program as it was run on the command line. If argc
is 2, that means the program was given one argument, maybe as
./msdscript --help
and then argv[1]
is that argument, which is "--help"
in this case.
Your job for this assignment is to create a function use_arguments
that main
will call. The use_arguments
function should take argc
and argv
and check the arguments in order:
"--help"
, then use_arguments
should print to std::cout
some help text that describes what arguments are allowed. The exact text is up to you. After printing the help text, it should exit immediately with exit(0)
. No further command-line argument should be inspected."--test"
, then use_arguments
should print "Tests passed"
on its own line and continue, but only if a "--test"
argument has not been seen already. If "--test"
has been seen before, use_arguments
should print an error to std::cerr
and exit with exit(1)
.use_arguments
should print an error message to std::cerr
and exit with exit(1)
.If no arguments trigger an error or exit, then use_arguments
should return. Note that there is no requirement on ordering of argument or that any argument is provided; providing zero arguments is allowed.
Put a declaration of use_arguments
in a header file "cmdline.h" (or a different name that you like), and then "main.cpp" should contain just
#include "cmdline.h"
int main(int argc, char **argv) {
use_arguments(argc, argv);
return 0;
}
except that "cmdline.h" can be a different file name that you pick.
Create a "Makefile" so that make
will compile and link "main.cpp" with "cmdline.cpp" (or some other file name that you choose) to create a program named msdscript
. You can and should use a programming environment to develop your program, but in the end, make
and ./msdscript
should work to build and run.
Add all of your code to your new GitHub repo. In class on Thursday, another student will check out your repo and try to build and run your code using just make
and ./msdscript
.
Meanwhile, “hand in” your work by providing the URL of your GitHub repo (which will stay the same all semester).