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
|
/*
g++ p.c -o exe
*/
#include <iostream>
#include <getopt.h>
// some global variable
bool verb = 0;
double ab = 0;
// print help on screen
void PrintHelp() {
std::cout << " --verb :: \tVerbose output" << std::endl;
std::cout << " --help :: \tShow help" << std::endl;
return;
}
// process the parameters
void ProcessArgs(int argc, char** argv) {
const char* const short_opts = "hv";
const option long_opts[] = {
{"verb", no_argument, nullptr, 'v'},
{"help", no_argument, nullptr, 'h'},
{nullptr, no_argument, nullptr, 0}
};
while (true) {
const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
if (opt == -1) break;
switch (opt) {
case 'v':
verb = 1;
break;
case 'h':
case '?':
default:
PrintHelp();
break;
}
}
}
int main (int argc, char** argv) {
ProcessArgs(argc, argv);
/* some magic should go here, so that if the user gives 'exe 12.2', then the variable ab is set to 12.2, but if he gives 'exe -v 12.2' than also verbosity is on */
if (verb) {
std::cout << "The value of variable AB is " << ab << std::endl;
}
return 0;
}
|