Merge branch 'master' into 'master'

Honor long options as well.

See merge request !1
master
Chris Punches 2017-12-07 06:22:55 +00:00
commit 4d66a0f059
1 changed files with 27 additions and 5 deletions

View File

@ -22,6 +22,7 @@
#include "src/json/json.h" #include "src/json/json.h"
#include "src/loaders/loaders.h" #include "src/loaders/loaders.h"
#include <unistd.h> #include <unistd.h>
#include <getopt.h>
#include <syslog.h> #include <syslog.h>
@ -31,8 +32,7 @@
void print_usage() void print_usage()
{ {
printf("examplar [ -h ] [ -v ] [ -c CONFIG_PATH ]"); printf("examplar [ -h | --help ] [ -v | --verbose ] [ -c | --config CONFIG_PATH ]\n\n");
exit(0);
} }
int main( int argc, char * argv[] ) int main( int argc, char * argv[] )
@ -47,10 +47,28 @@ int main( int argc, char * argv[] )
// -v verbose // -v verbose
// -c CONFIG_FILE_PATH -- defaults to '/etc/Examplar/config.json' // -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': case 'h':
show_help = true; show_help = true;
case 'v': case 'v':
@ -59,6 +77,9 @@ int main( int argc, char * argv[] )
case 'c': case 'c':
config_path = std::string(optarg); config_path = std::string(optarg);
break; break;
case '?':
print_usage();
exit(1);
default: default:
break; break;
} }
@ -67,6 +88,7 @@ int main( int argc, char * argv[] )
if ( show_help == true ) if ( show_help == true )
{ {
print_usage(); print_usage();
exit(0);
} }
setlogmask( LOG_UPTO( LOG_INFO ) ); setlogmask( LOG_UPTO( LOG_INFO ) );
@ -106,4 +128,4 @@ int main( int argc, char * argv[] )
closelog(); closelog();
return 0; return 0;
} }