File: options.cpp

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (137 lines) | stat: -rw-r--r-- 3,396 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <wibble/commandline/options.h>
#include <wibble/string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <cstdlib>
#include <set>
#include <cstdlib>
#include <sstream>

using namespace std;

namespace wibble {
namespace commandline {

bool Bool::parse(const std::string& val)
{
	if (val == "true" || val == "t" || val == "1" || val == "yes" || val == "y")
		return true;
	if (val == "false" || val == "f" || val == "0" || val == "no" || val == "n")
		return false;
	throw exception::BadOption("invalid true/false value: \"" + val + "\"");
}
bool Bool::toBool(const bool& val) { return val; }
int Bool::toInt(const value_type& val) { return val ? 1 : 0; }
std::string Bool::toString(const value_type& val) { return val ? "true" : "false"; }

int Int::parse(const std::string& val)
{
	// Ensure that we're all numeric
	for (string::const_iterator s = val.begin(); s != val.end(); ++s)
		if (!isdigit(*s))
			throw exception::BadOption("value " + val + " must be numeric");
	return strtoul(val.c_str(), NULL, 10);
}
bool Int::toBool(const int& val) { return (bool)val; }
int Int::toInt(const int& val) { return val; }
std::string Int::toString(const int& val) { return str::fmt(val); }

std::string String::parse(const std::string& val)
{
	return val;
}
bool String::toBool(const std::string& val) { return !val.empty(); }
int String::toInt(const std::string& val) { return strtoul(val.c_str(), NULL, 10); }
std::string String::toString(const std::string& val) { return val; }

std::string ExistingFile::parse(const std::string& val)
{
	if (access(val.c_str(), F_OK) == -1)
		throw exception::BadOption("file " + val + " must exist");
	return val;
}
std::string ExistingFile::toString(const std::string& val) { return val; }

static string fmtshort(char c, const std::string& usage)
{
	if (usage.empty())
		return string("-") + c;
	else
		return string("-") + c + " " + usage;
}

static string fmtlong(const std::string& c, const std::string& usage)
{
	if (usage.empty())
		return string("--") + c;
	else
		return string("--") + c + "=" + usage;
}

static string manfmtshort(char c, const std::string& usage)
{
	if (usage.empty())
		return string("\\-") + c;
	else
		return string("\\-") + c + " \\fI" + usage + "\\fP";
}

static string manfmtlong(const std::string& c, const std::string& usage)
{
	if (usage.empty())
		return string("\\-\\-") + c;
	else
		return string("\\-\\-") + c + "=\\fI" + usage + "\\fP";
}

Option::Option() : hidden(false) {}

const std::string& Option::fullUsage() const
{
	if (m_fullUsage.empty())
	{
		for (vector<char>::const_iterator i = shortNames.begin();
				i != shortNames.end(); i++)
		{
			if (!m_fullUsage.empty())
				m_fullUsage += ", ";
			m_fullUsage += fmtshort(*i, usage);
		}

		for (vector<string>::const_iterator i = longNames.begin();
				i != longNames.end(); i++)
		{
			if (!m_fullUsage.empty())
				m_fullUsage += ", ";
			m_fullUsage += fmtlong(*i, usage);
		}
	}
	return m_fullUsage;
}
	
std::string Option::fullUsageForMan() const
{
	string res;

	for (vector<char>::const_iterator i = shortNames.begin();
			i != shortNames.end(); i++)
	{
		if (!res.empty()) res += ", ";
		res += manfmtshort(*i, usage);
	}

	for (vector<string>::const_iterator i = longNames.begin();
			i != longNames.end(); i++)
	{
		if (!res.empty()) res += ", ";
		res += manfmtlong(*i, usage);
	}

	return res;
}

}
}

// vim:set ts=4 sw=4: