C/C++ program template with one CLI argument
This example program provides an example of a CLI executable in C/C++ that uses one command line argument and exits with a usage message if that argument is not present:
#include <stdio.h>
int main(int argc, char **argv)
{
if(argc <= 1) { // <= (number of expected CLI arguments)
fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
return -1;
}
// TODO Your code goes here!
printf("Input file: %s\n", argv[1]);
return 0;
}
Compile with
g++ -o cli-onearg cli-onearg.cpp
Usage example:
$ ./cli-onearg
Usage: ./cli-onearg <input file>
$ ./cli-onearg my-input-file.txt
Input file: my-input-file.txt