diff options
author | Jan Darmochwal | 2010-10-07 16:50:48 +0200 |
---|---|---|
committer | Jan Darmochwal | 2010-10-07 16:50:48 +0200 |
commit | 81f1ffa46a07c76f8b5e75e832ab760f52884fb4 (patch) | |
tree | a77f20f424b492f2d838a8515d3a0d25ae1ec7b7 | |
parent | delete ifaceDBus_ifaceDBus_ in ~Dialog (diff) | |
download | vmchooser-81f1ffa46a07c76f8b5e75e832ab760f52884fb4.tar.gz vmchooser-81f1ffa46a07c76f8b5e75e832ab760f52884fb4.tar.xz vmchooser-81f1ffa46a07c76f8b5e75e832ab760f52884fb4.zip |
Added files to parse command line options
command_line_options.{cpp,h} must have slipped
-rw-r--r-- | src/command_line_options.cpp | 54 | ||||
-rw-r--r-- | src/command_line_options.h | 23 |
2 files changed, 77 insertions, 0 deletions
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 <getopt.h> + +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 <QMap> +#include <QString> + +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<QString, QString> options; +}; + +#endif // VMCHOOSER_COMMAND_LINE_OPTIONS_H |