diff options
Diffstat (limited to 'project2/cli')
-rw-r--r-- | project2/cli/Jamfile.jam | 16 | ||||
-rw-r--r-- | project2/cli/claOptions.cpp | 87 | ||||
-rw-r--r-- | project2/cli/claOptions.h | 21 |
3 files changed, 124 insertions, 0 deletions
diff --git a/project2/cli/Jamfile.jam b/project2/cli/Jamfile.jam new file mode 100644 index 0000000..28487aa --- /dev/null +++ b/project2/cli/Jamfile.jam @@ -0,0 +1,16 @@ +alias glibmm : : : : + <cflags>"`pkg-config --cflags glibmm-2.4`" + <linkflags>"`pkg-config --libs glibmm-2.4`" + ; + +lib p2cli : + [ glob *.cpp ] + : + <include>. + <include>../../libmisc + <library>glibmm + <library>../common//p2common + : : + <include>. + ; + diff --git a/project2/cli/claOptions.cpp b/project2/cli/claOptions.cpp new file mode 100644 index 0000000..b1e1250 --- /dev/null +++ b/project2/cli/claOptions.cpp @@ -0,0 +1,87 @@ +#include <pch.hpp> +#include <stdio.h> +#include <boost/foreach.hpp> +#include "../common/optionsSource.h" +#include "../common/exceptions.h" +#include "claOptions.h" + +SimpleMessageException(ArgumentRequired); +SimpleMessageException(UnknownOption); + +CommandLineArguments::CommandLineArguments(int c, const char * const * v, const Others & o) : + argc(c), + argv(v), + others(o), + loadedAt(0) +{ +} + +void +CommandLineArguments::loadInto(const ConfigConsumer & consume, const Options::CurrentPlatform & platform) const { + bool moreopts = true; + + for (int x = 1; x < argc; x += 1) { + if (moreopts && strcmp(argv[x], "--") == 0) { + moreopts = false; + } + else if (moreopts && strncmp(argv[x], "--", 2) == 0) { + Glib::ustring plat, name(argv[x] + 2); + Glib::ustring::size_type sl = name.find('/'); + if (sl != (Glib::ustring::size_type)-1) { + plat = name.substr(sl + 1); + name = name.substr(0, sl); + } + const Options::Option * o = consume.get(name); + if (!o) { + throw UnknownOption(name); + } + if (o->paramRequired()) { + x += 1; + if (x >= argc) { + throw ArgumentRequired(name); + } + consume(name, plat, argv[x], platform); + } + else { + consume(name, plat, name, platform); + } + } + else if (moreopts && *(argv[x]) == '-') { + const char * n = argv[x] + 1; + while (*n) { + Glib::ustring name(1, *n++); + const Options::Option * o = consume.get(name); + if (!o) { + throw UnknownOption(name); + } + if (o->paramRequired()) { + if (!*n) { + x += 1; + if (x >= argc) { + throw ArgumentRequired(name); + } + consume(name, Glib::ustring(), argv[x], platform); + } + else { + consume(name, Glib::ustring(), n, platform); + n += 1; + } + } + else { + consume(name, Glib::ustring(), name, platform); + } + } + } + else { + others(argv[x]); + } + } + time(&loadedAt); +} + +bool +CommandLineArguments::needReload() const +{ + return !loadedAt; +} + diff --git a/project2/cli/claOptions.h b/project2/cli/claOptions.h new file mode 100644 index 0000000..c669004 --- /dev/null +++ b/project2/cli/claOptions.h @@ -0,0 +1,21 @@ +#ifndef CLAOPTIONS_H +#define CLAOPTIONS_H + +#include "../common/optionsSource.h" + +class CommandLineArguments : public OptionsSource { + public: + typedef boost::function<void(const char * const)> Others; + CommandLineArguments(int c, const char * const * v, const Others &); + void loadInto(const ConfigConsumer & consume, const Options::CurrentPlatform & platform) const; + bool needReload() const; + + private: + const int argc; + const char * const * argv; + const Others others; + mutable time_t loadedAt; +}; + +#endif + |