From 0c70f7ee3605ade69047d3b3ee96091a1cd2123c Mon Sep 17 00:00:00 2001 From: DJ Lucas Date: Tue, 5 Dec 2017 22:09:33 -0600 Subject: [PATCH 1/2] Honor long options as well. --- examplar.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/examplar.cpp b/examplar.cpp index 5d868b7..58d7ba8 100644 --- a/examplar.cpp +++ b/examplar.cpp @@ -22,6 +22,7 @@ #include "src/json/json.h" #include "src/loaders/loaders.h" #include +#include #include @@ -31,7 +32,7 @@ void print_usage() { - printf("examplar [ -h ] [ -v ] [ -c CONFIG_PATH ]"); + printf("examplar [ -h/--help ] [ -v/--verbose ] [ -c/--config CONFIG_PATH ]\n\n"); exit(0); } @@ -47,10 +48,28 @@ int main( int argc, char * argv[] ) // -v verbose // -c CONFIG_FILE_PATH -- defaults to '/etc/Examplar/config.json' - while ( ( opt = getopt( argc, argv, "hvc:" ) ) != -1 ) + while (1) { - switch(opt) + static struct option long_options[] = { + {"verbose", no_argument, 0, 'v'}, + {"help", no_argument, 0, 'h'}, + {"config", required_argument, 0, 'c'}, + {0, 0} + }; + int option_index = 0; + + opt = getopt_long (argc, argv, "vhc:", + long_options, &option_index); + + if (opt == -1) + break; + + switch (opt) + { + case 0: + if (long_options[option_index].flag !=0) + break; case 'h': show_help = true; case 'v': @@ -106,4 +125,4 @@ int main( int argc, char * argv[] ) closelog(); return 0; -} \ No newline at end of file +} From 221edee07c2cc3c2f8f366215fb5c38b5c9fd27d Mon Sep 17 00:00:00 2001 From: DJ Lucas Date: Tue, 5 Dec 2017 22:40:57 -0600 Subject: [PATCH 2/2] Forgot print_usage() on invalid option. --- examplar.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examplar.cpp b/examplar.cpp index 58d7ba8..654eb01 100644 --- a/examplar.cpp +++ b/examplar.cpp @@ -32,8 +32,7 @@ void print_usage() { - printf("examplar [ -h/--help ] [ -v/--verbose ] [ -c/--config CONFIG_PATH ]\n\n"); - exit(0); + printf("examplar [ -h | --help ] [ -v | --verbose ] [ -c | --config CONFIG_PATH ]\n\n"); } int main( int argc, char * argv[] ) @@ -78,6 +77,9 @@ int main( int argc, char * argv[] ) case 'c': config_path = std::string(optarg); break; + case '?': + print_usage(); + exit(1); default: break; } @@ -86,6 +88,7 @@ int main( int argc, char * argv[] ) if ( show_help == true ) { print_usage(); + exit(0); } setlogmask( LOG_UPTO( LOG_INFO ) );