From 81f1ffa46a07c76f8b5e75e832ab760f52884fb4 Mon Sep 17 00:00:00 2001 From: Jan Darmochwal Date: Thu, 7 Oct 2010 16:50:48 +0200 Subject: Added files to parse command line options command_line_options.{cpp,h} must have slipped --- src/command_line_options.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/command_line_options.h | 23 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/command_line_options.cpp create mode 100644 src/command_line_options.h (limited to 'src') diff --git a/src/command_line_options.cpp b/src/command_line_options.cpp new file mode 100644 index 0000000..4ee6520 --- /dev/null +++ b/src/command_line_options.cpp @@ -0,0 +1,54 @@ +#include "command_line_options.h" +#include + +CommandLineOptions::CommandLineOptions(int argc, char * const argv[]) { + // parse command line arguments + for (;;) { + static const struct option longOptions[] = { + {"config", required_argument, NULL, 'c'}, + {"default", required_argument, NULL, 'd'}, + {"path", required_argument, NULL, 'p'}, + {"xpath", required_argument, NULL, 'x'}, + {"size", required_argument, NULL, 's'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'} + }; + + int opt = getopt_long(argc, argv, "c:d:p:x:s:vhb", longOptions, NULL); + if (opt == -1) break; + + switch (opt) { + case 'c': + options.insert("config", optarg); + break; + case 'd': + options.insert("default", optarg); + break; + case 'p': + options.insert("path", optarg); + break; + case 'x': + options.insert("xpath", optarg); + break; + case 's': + options.insert("size", optarg); + break; + case 'v': + options.insert("version", "version"); + break; + case 'h': + options.insert("usage", "usage"); + break; + case '?': + default: + options.insert("error", "error"); + } + + if (optind == argc - 1) { + options.insert("file", argv[optind]); + } else if (optind < argc - 1) { + options.insert("error", "error"); + } + } +} + diff --git a/src/command_line_options.h b/src/command_line_options.h new file mode 100644 index 0000000..8798ff2 --- /dev/null +++ b/src/command_line_options.h @@ -0,0 +1,23 @@ +#ifndef VMCHOOSER_COMMAND_LINE_OPTIONS_H +#define VMCHOOSER_COMMAND_LINE_OPTIONS_H + +#include +#include + +class CommandLineOptions { + public: + CommandLineOptions(int argc, char * const argv[]); + + bool contains(const QString& key) const { + return options.contains(key); + } + + QString value(const QString& key) const { + return options.value(key); + } + + private: + QMap options; +}; + +#endif // VMCHOOSER_COMMAND_LINE_OPTIONS_H -- cgit v1.2.3-55-g7522