File: options.cpp

package info (click to toggle)
libwibble 0.1.9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 500 kB
  • ctags: 1,183
  • sloc: cpp: 5,760; sh: 113; makefile: 71
file content (159 lines) | stat: -rw-r--r-- 3,578 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <wibble/config.h>
#include <wibble/commandline/options.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <set>
#include <sstream>

using namespace std;

namespace wibble {
namespace commandline {

int Option::intValue() const
{
	return strtoul(stringValue().c_str(), NULL, 10);
}

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";
}

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;
}
	
ArgList::iterator StringOption::parse(ArgList& list, ArgList::iterator begin)
{
	if (begin == list.end())
		throw exception::BadOption("no string argument found");
	m_value = *begin;
	// Remove the parsed element
	return list.eraseAndAdvance(begin);
}
bool StringOption::parse(const std::string& param)
{
	m_value = param;
	return true;
}

std::string IntOption::stringValue() const
{
	stringstream str;
	str << m_value;
	return str.str();
}

ArgList::iterator IntOption::parse(ArgList& list, ArgList::iterator begin)
{
	if (begin == list.end())
		throw exception::BadOption("no numeric argument found");
	// Ensure that we're all numeric
	for (string::const_iterator s = begin->begin(); s != begin->end(); ++s)
		if (!isdigit(*s))
			throw exception::BadOption("value " + *begin + " must be numeric");
	m_value = strtoul(begin->c_str(), 0, 10);
	// Remove the parsed element
	return list.eraseAndAdvance(begin);
}
bool IntOption::parse(const std::string& param)
{
	m_value = strtoul(param.c_str(), 0, 10);
	return true;
}


ArgList::iterator ExistingFileOption::parse(ArgList& list, ArgList::iterator begin)
{
	if (begin == list.end())
		throw exception::BadOption("no file argument found");
	if (access(begin->c_str(), F_OK) == -1)
		throw exception::BadOption("file " + *begin + " must exist");
	m_value = *begin;

	// Remove the parsed element
	return list.eraseAndAdvance(begin);
}
bool ExistingFileOption::parse(const std::string& param)
{
	if (param.empty())
		throw exception::BadOption("no file argument found");
	if (access(param.c_str(), F_OK) == -1)
		throw exception::BadOption("file " + param + " must exist");
	m_value = param;
	return true;
}

}
}

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