Adding framework for an option parser.

git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@448 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2005-05-31 19:05:55 +00:00
parent a2248f584f
commit 74930f86b3
15 changed files with 415 additions and 85 deletions
+51 -25
View File
@@ -23,25 +23,37 @@
#include "config.h"
#include <cstdio>
#include <sstream>
#include <stdexcept>
#include <unistd.h>
#include <sigc++/bind.h>
#include <sigc++/hide.h>
#include "option_parser.h"
void
OptionParser::insert_flag(char c, SlotFlag s) {
OptionParser::insert_flag(char c, Slot s) {
m_container[c].m_slot = sigc::hide(s);
m_container[c].m_useOption = false;
}
void
OptionParser::insert_option(char c, SlotOption s) {
OptionParser::insert_option(char c, SlotString s) {
m_container[c].m_slot = s;
m_container[c].m_useOption = true;
}
void
OptionParser::insert_option_list(char c, SlotStringPair s) {
m_container[c].m_slot = sigc::bind<0>(sigc::ptr_fun(&OptionParser::call_option_list), s);
m_container[c].m_useOption = true;
}
void
OptionParser::insert_int_pair(char c, SlotIntPair s) {
m_container[c].m_slot = sigc::bind<0>(sigc::ptr_fun(&OptionParser::call_int_pair), s);
m_container[c].m_useOption = true;
}
int
OptionParser::process(int argc, char** argv) {
int c;
@@ -49,31 +61,15 @@ OptionParser::process(int argc, char** argv) {
opterr = 0;
while ((c = getopt(argc, argv, optString.c_str())) != -1) {
if (c == '?') {
std::stringstream s;
s << "Invalid use of option flag -" << (char)optopt << ". Try rtorrent -h for more information.";
throw std::runtime_error(s.str());
} else {
call_flag(c, optarg ? optarg : "");
}
}
while ((c = getopt(argc, argv, optString.c_str())) != -1)
if (c == '?')
throw std::runtime_error("Invalid/unknown option flag \"-" + std::string(1, (char)optopt) + "\". See rtorrent -h for more information.");
else
call(c, optarg ? optarg : "");
return optind;
}
void
OptionParser::call_int_pair(const std::string& str, SlotIntPair slot) {
int a, b;
if (sscanf(str.c_str(), "%u-%u", &a, &b) != 2)
throw std::runtime_error("Invalid argument \"" + str + "\" should be <a-b>");
slot(a, b);
}
std::string
OptionParser::create_optstring() {
std::string s;
@@ -89,7 +85,7 @@ OptionParser::create_optstring() {
}
void
OptionParser::call_flag(char c, const std::string& arg) {
OptionParser::call(char c, const std::string& arg) {
Container::iterator itr = m_container.find(c);
if (itr == m_container.end())
@@ -97,3 +93,33 @@ OptionParser::call_flag(char c, const std::string& arg) {
itr->second.m_slot(arg);
}
void
OptionParser::call_option_list(SlotStringPair slot, const std::string& arg) {
std::string::const_iterator itr = arg.begin();
while (itr != arg.end()) {
std::string::const_iterator last = std::find(itr, arg.end(), ',');
std::string::const_iterator opt = std::find(itr, last, '=');
if (opt == itr || opt == last)
throw std::logic_error("Invalid argument, \"" + arg + "\" should be \"key1=opt1,key2=opt2,...\"");
slot(std::string(itr, opt), std::string(opt + 1, last));
if (last == arg.end())
break;
itr = ++last;
}
}
void
OptionParser::call_int_pair(SlotIntPair slot, const std::string& arg) {
int a, b;
if (std::sscanf(arg.c_str(), "%u-%u", &a, &b) != 2)
throw std::runtime_error("Invalid argument, \"" + arg + "\" should be \"a-b\"");
slot(a, b);
}