|
| 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 | +} |
0 commit comments