#include #include #include using namespace std; const char * ARG_STR{"hG:P::"}; int main(int argc, char * argv[]){ char opt; char punct{'!'}; string greeting{"Hello World"}; // this controls error reporting. opterr = 0; while ((opt = getopt(argc, argv, ARG_STR)) != -1) { switch(opt) { case 'h': cout << "h: calling help routine " << endl; cout << endl; break; case 'G': greeting = optarg; cout << "G: The greeting will be " << greeting << endl; cout << endl; break; case 'P': if (optarg != nullptr) { punct = optarg[0]; } else { punct = ' '; } cout << "P: the punctuation is \""<< punct << "\"" << endl; cout << endl; break; default: cout << "Default got " << opt << " which is at position " << optind << endl; cout << "\tOptopt contains " << static_cast(optopt) << endl; cout << "\tBut that is not a valid argument" << endl; cout << endl; } cout << endl; } cout << "optind is " << optind << endl; if (optind < argc) { cout << "That is " << argv[optind] << endl; } else { cout << "That is past the end of the args. " << endl; } cout << endl; cout << greeting << punct << endl; return 0; }