From 39c593458d035125582234aee0992b883ec07696 Mon Sep 17 00:00:00 2001 From: spaetz Date: Tue, 20 May 2008 11:58:10 +0000 Subject: [PATCH] add a command line option parsing, using getopt. It currently does -v (version) and -h (usage info). git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@1067 ffa7fe5e-494d-0410-b361-a75ebd5db220 --- navit/navit/main.c | 41 +++++++++++++++++++++++++++++++++++++++-- navit/navit/main.h | 1 + 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/navit/navit/main.c b/navit/navit/main.c index f320ae6..3445763 100644 --- a/navit/navit/main.c +++ b/navit/navit/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -111,6 +112,12 @@ main_remove_navit(struct navit *nav) event_main_loop_quit(); } +void +print_usage(void) +{ + printf(_("navit usage:\nnavit [options] [configfile]\n\t-d : set the debug output level to .\n\t-h: print this usage info.\n\t-v: Print the version and exit.\n")); +} + int main(int argc, char **argv) { GError *error = NULL; @@ -192,8 +199,38 @@ int main(int argc, char **argv) route_init(); navigation_init(); config_file=NULL; - if (argc > 1) - config_file=argv[1]; + int opt; + opterr=0; //don't bomb out on errors. + if (argc > 1) { + while((opt = getopt(argc, argv, ":hvd:")) != -1) { + switch(opt) { + case 'h': + print_usage(); + exit(0); + break; + case 'v': + printf("%s %s\n", "navit", "0.0.4+svn"); + exit(0); + break; + case 'd': + printf("TODO Verbose option is set to `%s'\n", optarg); + break; + case ':': + fprintf(stderr, "navit: Error - Option `%c' needs a value\n", optopt); + print_usage(); + exit(1); + break; + case '?': + fprintf(stderr, "navit: Error - No such option: `%c'\n", optopt); + print_usage(); + exit(1); + } + } + } + // the first non-option argument is the config file + if (optind < argc) + config_file=argv[optind]; + if (! config_file) { config_file=g_strjoin(NULL,get_home_directory(), "/.navit/navit.xml" , NULL); if (!file_exists(config_file)) { diff --git a/navit/navit/main.h b/navit/navit/main.h index 4629888..ba62fe7 100644 --- a/navit/navit/main.h +++ b/navit/navit/main.h @@ -9,6 +9,7 @@ void main_iter_destroy(struct iter *iter); struct navit * main_get_navit(struct iter *iter); void main_add_navit(struct navit *nav); void main_remove_navit(struct navit *nav); +void print_usage(void); int main(int argc, char **argv); /* end of prototypes */ -- 2.7.4