From 59a0e24c675862a7bc8cec56f64fe6791fed536c Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Fri, 11 Jan 2013 11:33:17 +0100 Subject: [PATCH] main: basic option processing --- src/main.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 97d5d7d..16df632 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,8 +16,82 @@ * along with Codecrypt. If not, see . */ -int main() +#include + +/* + * main module. Parse options, fire up stuff, pass commands to it. + */ + +#define out(x) std::cout << x << std::endl +#define outeol std::cout << std::endl +#define err(x) std::cerr << x << std::endl +#define erreol std::cerr << std::endl + +void print_version() { + out ("codecrypt " PACKAGE_VERSION); +} + +void print_help (char*pname) +{ + print_version(); + outeol; + out ("Usage: " << pname << " [options]"); + outeol; + out ("Options consist of:"); + out (" -h, --help display this help"); + out (" -V,--version display version information"); +} + +#include + +int main (int argc, char**argv) +{ + bool do_help = false; + bool do_version = false; + bool has_opt = false; + + //process options + int c, option_index; + for (;;) { + static struct option long_opts[] = { + {"help", 0, 0, 'h' }, + {"version", 0, 0, 'V' }, + {0, 0, 0, 0 } + }; + + c = getopt_long (argc, argv, "hV", long_opts, &option_index); + if (c == -1) break; + + has_opt = true; + switch (c) { + case '?': + case ':': + case 'h': + do_help = true; + break; + case 'V': + do_version = true; + break; + default: //which doesn't just happen. + break; + } + } + + if (optind != argc) { + err (argv[0] << ": unmatched non-option parameters"); + do_help = true; + } + + if ( (!has_opt) || do_help) { + print_help (argv[0]); + return 0; + } + + if (do_version) { + print_version(); + return 0; + } return 0; }