9#if __DCL_HAVE_THIS_FILE__
20 const wchar_t* _program_version,
21 const wchar_t* _program_bug_address,
22 const wchar_t* _arg_doc,
24 const Option _options[]
25) : __output(_output), __errout(_errout)
27 __program_version = _program_version;
28 __program_bug_address = _program_bug_address;
35static Arguments::Option __options__[] =
37 { L
"help", L
'?',
NULL, 0, L
"Give this help list" },
38 { L
"usage",
KEY_USAGE,
NULL, 0, L
"Give a short usage message" },
39 { L
"version", L
'V',
NULL, 0, L
"Print program version" },
53static void __add_help(StringBuilder& _sb,
const Arguments::Option* _options)
55 while (_options->name || _options->doc) {
57 StringBuilder sb = L
" ";
58 if (iswprint(_options->key)) {
60 sb += (wchar_t)_options->key;
76 if (sb.length() < 28) {
77 String pad(L
' ', 28 - sb.length());
109 __add_help(sb, __options);
111 __add_help(sb, __options__);
113 if (__program_bug_address) {
115 sb += L
"Report bugs to ";
116 sb += __program_bug_address;
123 StringBuilder sb = L
"Usage: ";
125 sb += L
" [OPTION...] ";
134 StringBuilder sb = L
"Try `";
136 sb += L
" --help' or `";
138 sb += L
" --usage' for more information.";
144 StringBuilder sb = __value0;
147 sb += L
"' requires an argument";
154 while (_options->name || _options->doc) {
155 if (iswprint(_options->key)) {
156 sb += (wchar_t)_options->key;
163static void __add_key_arg_options(
165 const Arguments::Option* _options
168 while (_options->name || _options->doc) {
169 if (iswprint(_options->key) && _options->arg) {
170 StringBuilder sb = L
"-";
171 sb += (wchar_t)_options->key;
180static void __add_name_options(
182 const Arguments::Option* _options
185 while (_options->name || _options->doc) {
186 if (_options->name && _options->arg ==
NULL) {
189 sb += _options->name;
196static void __add_name_arg_options(
198 const Arguments::Option* _options
201 while (_options->name || _options->doc) {
202 if (_options->name && _options->arg) {
205 sb += _options->name;
225 __add_key_arg_options(a, __options);
226 __add_name_arg_options(a, __options);
227 __add_name_options(a, __options);
228 __add_name_options(a, __options__);
230 StringBuilder sb = L
"Usage: ";
232 String padding(L
' ', 10);
233 size_t n = sb.length();
234 for (
size_t i = 0; i < a.size(); i++) {
235 const String& s = a[i];
236 if ((n + s.length() + 3) > 80) {
239 n = padding.length();
248 if (n + String::length(__arg_doc) + 1 > 80) {
262 if (__program_version) {
263 sb += __program_version;
269static const Arguments::Option* __find_key(
271 const Arguments::Option* _options
274 if (_s.length() > 2 && _s.startsWith(L
"--")) {
275 String name = _s.substring(2);
276 while (_options->name || _options->doc) {
277 if (_options->name && name.compare(_options->name,
278 String::length(_options->name)) == 0) {
284 else if (_s.length() > 1) {
286 while (_options->name || _options->doc) {
287 if (_options->key == key) {
296bool Arguments::parse(
297 int _argc,
char* _argv[],
bool _argv_required
300 LocaleDecoder decoder;
301 StringArray argv(_argc);
302 for (
int i = 0; i < _argc; i++) {
303 argv[i] = decoder.decode(_argv[i]);
307 argv.erase((
size_t)0);
309 for (
size_t i = 0; i < argv.size(); i++) {
310 const String& s = argv[i];
311 if (s.startsWith(L
"-")) {
312 const Option* option = __find_key(s, __options__);
313 if (option !=
NULL) {
314 switch (option->key) {
329 for (
size_t i = 0; i < argv.size(); ) {
333 const String key = argv[i];
334 if (key.startsWith(L
"-")) {
335 const Option* option = __find_key(key, __options);
336 if (option ==
NULL) {
337 __errout << __value0 + L
": unrecognized option '"
338 << key << L
"'" <<
endl
346 if (key.startsWith(L
"--")) {
347 size_t index = key.indexOf(L
'=');
348 if (index != (
size_t)-1) {
349 arg = key.substring(index + 1);
353 if (key.length() > 2) {
354 arg = key.substring(2);
358 if (i < argv.size()) {
374 catch (Exception* e) {
387 if (!msg.isEmpty()) {
388 __errout << __value0 << L
": " << msg <<
endl
393 catch (Exception* e) {
399 if (_argv_required && argv.isEmpty()) {
String __key_options(const Arguments::Option *_options)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
DCLCVAR const struct __endl endl
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