20 const wchar_t* _program_version,
21 const wchar_t* _program_bug_address,
22 const wchar_t* _arg_doc,
24 const Option _options[]
26 : __output(_output), __errout(_errout)
28 __program_version = _program_version;
29 __program_bug_address = _program_bug_address;
36static Arguments::Option __options__[] =
38 { L
"help", L
'?',
NULL, 0, L
"Give this help list" },
39 { L
"usage",
KEY_USAGE,
NULL, 0, L
"Give a short usage message" },
40 { L
"version", L
'V',
NULL, 0, L
"Print program version" },
54static void __add_help(StringBuilder& _sb,
const Arguments::Option* _options)
56 while (_options->name || _options->doc) {
58 StringBuilder sb = L
" ";
59 if (iswprint(_options->key)) {
61 sb += (wchar_t)_options->key;
77 if (sb.length() < 28) {
78 String pad(L
' ', 28 - sb.length());
110 __add_help(sb, __options);
112 __add_help(sb, __options__);
114 if (__program_bug_address) {
116 sb += L
"Report bugs to ";
117 sb += __program_bug_address;
124 StringBuilder sb = L
"Usage: ";
126 sb += L
" [OPTION...] ";
135 StringBuilder sb = L
"Try `";
137 sb += L
" --help' or `";
139 sb += L
" --usage' for more information.";
145 StringBuilder sb = __value0;
148 sb += L
"' requires an argument";
155 while (_options->name || _options->doc) {
156 if (iswprint(_options->key)) {
157 sb += (wchar_t)_options->key;
164static void __add_key_arg_options(
166 const Arguments::Option* _options
169 while (_options->name || _options->doc) {
170 if (iswprint(_options->key) && _options->arg) {
171 StringBuilder sb = L
"-";
172 sb += (wchar_t)_options->key;
181static void __add_name_options(
183 const Arguments::Option* _options
186 while (_options->name || _options->doc) {
187 if (_options->name && _options->arg ==
NULL) {
190 sb += _options->name;
197static void __add_name_arg_options(
199 const Arguments::Option* _options
202 while (_options->name || _options->doc) {
203 if (_options->name && _options->arg) {
206 sb += _options->name;
226 __add_key_arg_options(a, __options);
227 __add_name_arg_options(a, __options);
228 __add_name_options(a, __options);
229 __add_name_options(a, __options__);
231 StringBuilder sb = L
"Usage: ";
233 String padding(L
' ', 10);
234 size_t n = sb.length();
235 for (
size_t i = 0; i < a.size(); i++) {
236 const String& s = a[i];
237 if ((
n + s.length() + 3) > 80) {
240 n = padding.length();
249 if (
n + String::length(__arg_doc) + 1 > 80) {
263 if (__program_version) {
264 sb += __program_version;
270static const Arguments::Option* __find_key(
272 const Arguments::Option* _options
275 if (_s.length() > 2 && _s.startsWith(L
"--")) {
276 String name = _s.substring(2);
277 while (_options->name || _options->doc) {
278 if (_options->name && name.compare(_options->name,
279 String::length(_options->name)) == 0) {
285 else if (_s.length() > 1) {
287 while (_options->name || _options->doc) {
288 if (_options->key == key) {
297bool Arguments::parse(
298 int _argc,
char* _argv[],
bool _argv_required
301 LocaleDecoder decoder;
302 StringArray argv(_argc);
303 for (
int i = 0; i < _argc; i++) {
304 argv[i] = decoder.decode(_argv[i]);
308 argv.erase((
size_t)0);
310 for (
size_t i = 0; i < argv.size(); i++) {
311 const String& s = argv[i];
312 if (s.startsWith(L
"-")) {
313 const Option* option = __find_key(s, __options__);
314 if (option !=
NULL) {
315 switch (option->key) {
330 for (
size_t i = 0; i < argv.size(); ) {
334 const String key = argv[i];
335 if (key.startsWith(L
"-")) {
336 const Option* option = __find_key(key, __options);
337 if (option ==
NULL) {
338 __errout << __value0 + L
": unrecognized option '"
339 << key << L
"'" <<
endl
347 if (key.startsWith(L
"--")) {
348 size_t index = key.indexOf(L
'=');
349 if (index != (
size_t)-1) {
350 arg = key.substring(index + 1);
354 if (key.length() > 2) {
355 arg = key.substring(2);
359 if (i < argv.size()) {
375 catch (Exception* e) {
388 if (!msg.isEmpty()) {
389 __errout << __value0 << L
": " << msg <<
endl
394 catch (Exception* e) {
400 if (_argv_required && argv.isEmpty()) {
String __key_options(const Arguments::Option *_options)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
void CharsetConvertException *size_t n
String argRequired(const String &_option) const
virtual String onValidate()
String shortUsage() const
virtual void onOption(int _key, const String &_arg) __DCL_THROWS1(Exception *)
String toStringAll() const