| 45 | |
| 46 | |
| 47 | int main(int argc, char** argv) |
| 48 | { |
| 49 | try |
| 50 | { |
| 51 | command_line_parser parser; |
| 52 | |
| 53 | // first I will define the command line options I want. |
| 54 | // Add a -c option and tell the parser what the option is for. |
| 55 | parser.add_option("c","Indicates that we want to compress a file."); |
| 56 | parser.add_option("d","Indicates that we want to decompress a file."); |
| 57 | // add a --in option that takes 1 argument |
| 58 | parser.add_option("in","This option takes one argument which specifies the name of the file we want to compress/decompress.",1); |
| 59 | // add a --out option that takes 1 argument |
| 60 | parser.add_option("out","This option takes one argument which specifies the name of the output file.",1); |
| 61 | // In the code below, we use the parser.print_options() method to print all our |
| 62 | // options to the screen. We can tell it that we would like some options to be |
| 63 | // grouped together by calling set_group_name() before adding those options. In |
| 64 | // general, you can make as many groups as you like by calling set_group_name(). |
| 65 | // However, here we make only one named group. |
| 66 | parser.set_group_name("Miscellaneous Options"); |
| 67 | parser.add_option("h","Display this help message."); |
| 68 | parser.add_option("l","Set the compression level [1-3], 3 is max compression, default is 2.",1); |
| 69 | |
| 70 | |
| 71 | // now I will parse the command line |
| 72 | parser.parse(argc,argv); |
| 73 | |
| 74 | |
| 75 | // Now I will use the parser to validate some things about the command line. |
| 76 | // If any of the following checks fail then an exception will be thrown and it will |
| 77 | // contain a message that tells the user what the problem was. |
| 78 | |
| 79 | // First I want to check that none of the options were given on the command line |
| 80 | // more than once. To do this I define an array that contains the options |
| 81 | // that shouldn't appear more than once and then I just call check_one_time_options() |
| 82 | const char* one_time_opts[] = {"c", "d", "in", "out", "h", "l"}; |
| 83 | parser.check_one_time_options(one_time_opts); |
| 84 | // Here I'm checking that the user didn't pick both the c and d options at the |
| 85 | // same time. |
| 86 | parser.check_incompatible_options("c", "d"); |
| 87 | |
| 88 | // Here I'm checking that the argument to the l option is an integer in the range 1 to 3. |
| 89 | // That is, it should be convertible to an int by dlib::string_assign and be either |
| 90 | // 1, 2, or 3. Note that if you wanted to allow floating point values in the range 1 to |
| 91 | // 3 then you could give a range 1.0 to 3.0 or explicitly supply a type of float or double |
| 92 | // to the template argument of the check_option_arg_range() function. |
| 93 | parser.check_option_arg_range("l", 1, 3); |
| 94 | |
| 95 | // The 'l' option is a sub-option of the 'c' option. That is, you can only select the |
| 96 | // compression level when compressing. This command below checks that the listed |
| 97 | // sub options are always given in the presence of their parent options. |
| 98 | const char* c_sub_opts[] = {"l"}; |
| 99 | parser.check_sub_options("c", c_sub_opts); |
| 100 | |
| 101 | // check if the -h option was given on the command line |
| 102 | if (parser.option("h")) |
| 103 | { |
| 104 | // display all the command line options |
nothing calls this directly
no test coverage detected