24 #include <sys/param.h>
37 #include <QtCore/QDir>
38 #include <QtCore/QFile>
39 #include <QtCore/QHash>
40 #include <QtCore/QTextCodec>
44 #include "kdeversion.h"
75 #define DISPLAY "DISPLAY"
76 #elif defined(Q_WS_QWS)
77 #define DISPLAY "QWS_DISPLAY"
79 #define DISPLAY "NODISPLAY"
86 class KCmdLineParsedOptions :
public QHash<QByteArray,QByteArray>
89 KCmdLineParsedOptions() { }
92 class KCmdLineParsedArgs :
public QList<QByteArray>
95 KCmdLineParsedArgs() { }
99 class KCmdLineArgsList:
public QList<KCmdLineArgs*>
102 KCmdLineArgsList() { }
103 ~KCmdLineArgsList() {
113 class KCmdLineOptionsPrivate {
121 : d(new KCmdLineOptionsPrivate)
130 : d(new KCmdLineOptionsPrivate(*(options.d)))
136 if (
this != &options) {
146 d->names.append(name);
147 d->descriptions.append(description);
148 d->defaults.append(QString::fromUtf8(defaultValue));
154 d->names += other.d->names;
155 d->descriptions += other.d->descriptions;
156 d->defaults += other.d->defaults;
164 class KCmdLineArgsStatic {
167 KCmdLineArgsList *argsList;
174 bool ignoreUnknown : 1;
176 KCmdLineArgs::StdCmdLineArgs mStdargs;
181 KCmdLineArgsStatic ();
183 ~KCmdLineArgsStatic ();
194 static QString decodeInput(
const QByteArray &rawstr);
203 static QByteArray encodeOutput(
const QString &str);
209 void printQ(
const QString &msg);
225 QByteArray &opt_name,
QString &def,
bool &enabled);
232 static void findOption(
const QByteArray &optv,
const QByteArray &_opt,
233 int &i,
bool _enabled,
bool &moreOptions);
241 static void parseAllArgs();
250 static void removeArgs(
const QByteArray &
id);
255 KCmdLineArgsStatic::KCmdLineArgsStatic () {
264 ignoreUnknown =
false;
268 codec = QTextCodec::codecForLocale();
273 qt_options.add(
"display <displayname>",
ki18n(
"Use the X-server display 'displayname'"));
274 #elif defined(Q_WS_QWS)
275 qt_options.add(
"display <displayname>",
ki18n(
"Use the QWS display 'displayname'"));
278 qt_options.add(
"session <sessionId>",
ki18n(
"Restore the application for the given 'sessionId'"));
279 qt_options.add(
"cmap",
ki18n(
"Causes the application to install a private color\nmap on an 8-bit display"));
280 qt_options.add(
"ncols <count>",
ki18n(
"Limits the number of colors allocated in the color\ncube on an 8-bit display, if the application is\nusing the QApplication::ManyColor color\nspecification"));
281 qt_options.add(
"nograb",
ki18n(
"tells Qt to never grab the mouse or the keyboard"));
282 qt_options.add(
"dograb",
ki18n(
"running under a debugger can cause an implicit\n-nograb, use -dograb to override"));
283 qt_options.add(
"sync",
ki18n(
"switches to synchronous mode for debugging"));
284 qt_options.add(
"fn");
285 qt_options.add(
"font <fontname>",
ki18n(
"defines the application font"));
286 qt_options.add(
"bg");
287 qt_options.add(
"background <color>",
ki18n(
"sets the default background color and an\napplication palette (light and dark shades are\ncalculated)"));
288 qt_options.add(
"fg");
289 qt_options.add(
"foreground <color>",
ki18n(
"sets the default foreground color"));
290 qt_options.add(
"btn");
291 qt_options.add(
"button <color>",
ki18n(
"sets the default button color"));
292 qt_options.add(
"name <name>",
ki18n(
"sets the application name"));
293 qt_options.add(
"title <title>",
ki18n(
"sets the application title (caption)"));
294 qt_options.add(
"testability",
ki18n(
"load the testability framework"));
296 qt_options.add(
"visual TrueColor",
ki18n(
"forces the application to use a TrueColor visual on\nan 8-bit display"));
297 qt_options.add(
"inputstyle <inputstyle>",
ki18n(
"sets XIM (X Input Method) input style. Possible\nvalues are onthespot, overthespot, offthespot and\nroot"));
298 qt_options.add(
"im <XIM server>",
ki18n(
"set XIM server"));
299 qt_options.add(
"noxim",
ki18n(
"disable XIM"));
302 qt_options.add(
"qws",
ki18n(
"forces the application to run as QWS Server"));
304 qt_options.add(
"reverse",
ki18n(
"mirrors the whole layout of widgets"));
305 qt_options.add(
"stylesheet <file.qss>",
ki18n(
"applies the Qt stylesheet to the application widgets"));
306 qt_options.add(
"graphicssystem <system>",
ki18n(
"use a different graphics system instead of the default one, options are raster and opengl (experimental)"));
307 qt_options.add(
"qmljsdebugger <port>",
ki18n(
"QML JS debugger information. Application must be\nbuilt with -DQT_DECLARATIVE_DEBUG for the debugger to be\nenabled"));
309 kde_options.add(
"caption <caption>",
ki18n(
"Use 'caption' as name in the titlebar"));
310 kde_options.add(
"icon <icon>",
ki18n(
"Use 'icon' as the application icon"));
311 kde_options.add(
"config <filename>",
ki18n(
"Use alternative configuration file"));
312 kde_options.add(
"nocrashhandler",
ki18n(
"Disable crash handler, to get core dumps"));
314 kde_options.add(
"waitforwm",
ki18n(
"Waits for a WM_NET compatible windowmanager"));
316 kde_options.add(
"style <style>",
ki18n(
"sets the application GUI style"));
317 kde_options.add(
"geometry <geometry>",
ki18n(
"sets the client geometry of the main widget - see man X for the argument format (usually WidthxHeight+XPos+YPos)"));
319 kde_options.add(
"smkey <sessionKey>");
323 KCmdLineArgsStatic::~KCmdLineArgsStatic ()
334 class KCmdLineArgsPrivate
336 friend class KCmdLineArgsStatic;
342 , parsedOptionList(0)
347 ~KCmdLineArgsPrivate()
349 delete parsedOptionList;
350 delete parsedArgList;
355 KCmdLineParsedOptions *parsedOptionList;
356 KCmdLineParsedArgs *parsedArgList;
364 void setOption(
const QByteArray &option,
bool enabled);
371 void setOption(
const QByteArray &option,
const QByteArray &value);
378 void addArgument(
const QByteArray &argument);
385 void save( QDataStream &)
const;
392 void load( QDataStream &);
400 KCmdLineArgsStatic::decodeInput(
const QByteArray &rawstr)
402 return s->codec->toUnicode(rawstr);
406 KCmdLineArgsStatic::encodeOutput(
const QString &str)
408 return s->codec->fromUnicode(str);
412 KCmdLineArgsStatic::printQ(
const QString &msg)
414 fprintf(stdout,
"%s", encodeOutput(msg).data());
419 const QByteArray &_appname,
420 const QByteArray &_catalog,
422 const QByteArray &_version,
424 StdCmdLineArgs stdargs)
427 new KAboutData(_appname, _catalog, _programName, _version, _description),
432 KCmdLineArgs::initIgnore(
int _argc,
char **_argv,
const QByteArray &_appname )
436 s->ignoreUnknown =
true;
442 char **_argv = (
char **) malloc(
sizeof(
char *));
443 _argv[0] = (
char *) s->encodeOutput(ab->
appName()).data();
456 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
457 fprintf(stderr,
"Passing null-pointer to 'argv' is not allowed.\n\n");
465 char *p = strrchr(s->all_argv[0], QDir::separator().toLatin1());
469 s->appName = s->all_argv[0];
474 s->mCwd = QDir::currentPath().toLocal8Bit();
480 return QString::fromLocal8Bit(s->mCwd);
485 if (!s->appName)
return QString();
486 return s->decodeInput(s->appName);
499 s->mStdargs = stdargs;
504 const QByteArray &
id,
const QByteArray &afterId)
509 int pos = s->argsList->count();
511 if (pos > 0 && !
id.isEmpty() && s->argsList->last()->d->name.isEmpty())
514 KCmdLineArgsList::Iterator args;
516 for(args = s->argsList->begin(); args != s->argsList->end(); ++args, i++)
518 if (
id == (*args)->d->id) {
524 if (!afterId.isEmpty() && afterId == (*args)->d->id)
528 Q_ASSERT( s->parsed ==
false );
530 s->argsList->insert(pos,
new KCmdLineArgs(options, name,
id));
541 s->removeArgs(
"kde");
542 s->removeArgs(
"kuniqueapp");
546 uint
count = s->argsList ? s->argsList->count() : 0;
551 KCmdLineArgsList::Iterator args;
552 for(args = s->argsList->begin(); args != s->argsList->end(); ++args)
554 ds << (*args)->d->id;
555 (*args)->d->save(ds);
566 s->removeArgs(
"kde");
567 s->removeArgs(
"kuniqueapp");
569 KCmdLineArgsList::Iterator args;
571 for(args = s->argsList->begin(); args != s->argsList->end(); ++args)
592 Q_ASSERT( s->argsList );
594 for(args = s->argsList->begin(); args != s->argsList->end(); ++args)
596 if ((*args)->d->id ==
id)
598 (*args)->d->load(ds);
604 kWarning() <<
"Argument definitions for" <<
id <<
"not found!";
615 KCmdLineArgsList::Iterator args = s->argsList->begin();
616 while(args != s->argsList->end())
618 if ((*args)->d->id ==
id)
630 void KCmdLineArgsStatic::removeArgs(
const QByteArray &
id)
634 KCmdLineArgsList::Iterator args = s->argsList->begin();
635 while(args != s->argsList->end())
637 if ((*args)->d->id ==
id)
646 if (args != s->argsList->end()) {
648 s->argsList->erase(args);
654 KCmdLineArgsStatic::findOption(
const KCmdLineOptions &options, QByteArray &opt,
655 QByteArray &opt_name,
QString &def,
bool &enabled)
660 for (
int i = 0; i < options.d->names.size(); i++)
664 opt_name = options.d->names[i];
665 if (opt_name.startsWith(
':') || opt_name.isEmpty())
669 if (opt_name.startsWith(
'!'))
671 opt_name = opt_name.mid(1);
674 if (opt_name.startsWith(
"no") && !opt_name.contains(
'<'))
676 opt_name = opt_name.mid(2);
680 int len = opt.length();
681 if (opt == opt_name.left(len))
683 opt_name = opt_name.mid(len);
684 if (opt_name.isEmpty())
689 if (options.d->descriptions[i].isEmpty())
692 if (i >= options.d->names.size())
694 QByteArray nextOption = options.d->names[i];
695 int p = nextOption.indexOf(
' ');
697 nextOption = nextOption.left(p);
698 if (nextOption.startsWith(
'!'))
699 nextOption = nextOption.mid(1);
700 if (nextOption.startsWith(
"no") && !nextOption.contains(
'<'))
702 nextOption = nextOption.mid(2);
705 result = findOption(options, nextOption, opt_name, def, enabled);
713 if (opt_name.startsWith(
' '))
715 opt_name = opt_name.mid(1);
716 def = options.d->defaults[i];
725 KCmdLineArgsStatic::findOption(
const QByteArray &optv,
const QByteArray &_opt,
726 int &i,
bool _enabled,
bool &moreOptions)
728 KCmdLineArgsList::Iterator args = s->argsList->begin();
729 QByteArray opt = _opt;
733 int j = opt.indexOf(
'=');
736 argument = opt.mid(j+1);
742 while (args != s->argsList->end())
745 result = findOption((*args)->d->options, opt, opt_name, def, enabled);
749 if ((args == s->argsList->end()) &&
750 (optv.startsWith(
'-') && !optv.startsWith(
"--")))
757 QByteArray singleCharOption =
" ";
758 singleCharOption[0] = optv[p];
759 args = s->argsList->begin();
760 while (args != s->argsList->end())
763 result = findOption((*args)->d->options, singleCharOption,
764 opt_name, def, enabled);
768 if (args == s->argsList->end())
774 (*args)->d->setOption(singleCharOption, enabled);
775 if (p < optv.length())
780 else if (result == 3)
782 if (argument.isEmpty())
784 argument = optv.mid(p);
786 (*args)->d->setOption(singleCharOption, argument);
791 args = s->argsList->end();
795 if (args == s->argsList->end() || !result)
797 if (s->ignoreUnknown)
803 if ((result & 4) != 0)
813 if (s->ignoreUnknown)
818 if (argument.isEmpty())
821 if (i >= s->all_argc)
826 argument = s->all_argv[i];
828 (*args)->d->setOption(opt, argument);
832 (*args)->d->setOption(opt, enabled);
837 KCmdLineArgsStatic::parseAllArgs()
839 bool allowArgs =
false;
840 bool inOptions =
true;
841 bool everythingAfterArgIsArgs =
false;
843 if (appOptions->d->id.isEmpty())
845 foreach(
const QByteArray& name, appOptions->d->options.d->names)
847 everythingAfterArgIsArgs = everythingAfterArgIsArgs || name.startsWith(
"!+");
848 allowArgs = allowArgs || name.startsWith(
'+') || everythingAfterArgIsArgs;
851 for(
int i = 1; i < s->all_argc; i++)
856 if ((s->all_argv[i][0] ==
'-') && s->all_argv[i][1] && inOptions)
859 QByteArray orig = s->all_argv[i];
860 QByteArray option = orig.mid(1);
861 if (option.startsWith(
'-'))
863 option = option.mid(1);
864 if (option.isEmpty())
870 if (option ==
"help")
874 else if (option.startsWith(
"help-"))
880 else if (option.startsWith(
"psn_"))
885 else if ((option ==
"version") || (option ==
"v"))
888 s->printQ(
i18nc(
"@info:shell message on appcmd --version; do not translate 'Development Platform'"
889 "%3 application name, other %n version strings",
891 "KDE Development Platform: %2\n"
893 QString::fromLatin1(qVersion()),
895 s->about->programName(), s->about->version()));
897 }
else if (option ==
"license")
900 s->printQ(s->about->license());
901 s->printQ(QString::fromLatin1(
"\n"));
903 }
else if (option ==
"author") {
907 if ( !authors.isEmpty() ) {
911 if ( !(*it).emailAddress().isEmpty() )
912 email = QString::fromLatin1(
" <") + (*it).emailAddress() + QLatin1String(
">");
913 authorlist += QString::fromLatin1(
" ") + (*it).name() + email + QLatin1Char(
'\n');
915 s->printQ(
i18nc(
"the 2nd argument is a list of name+address, one on each line",
"%1 was written by\n%2",
QString(s->about->programName()) , authorlist ) );
918 s->printQ(
i18n(
"This application was written by somebody who wants to remain anonymous.") );
922 if (!s->about->customAuthorTextEnabled ())
924 if (s->about->bugAddress().isEmpty() || s->about->bugAddress() == QLatin1String(
"submit@bugs.kde.org") )
925 s->printQ(
i18n(
"Please use http://bugs.kde.org to report bugs.\n" ) );
927 s->printQ(
i18n(
"Please report bugs to %1.\n" , s->about->bugAddress()) );
931 s->printQ(s->about->customAuthorPlainText()+QLatin1Char(
'\n'));
936 if (option.startsWith(
"no"))
938 bool noHasParameter=
false;
939 foreach(
const QByteArray& name, appOptions->d->options.d->names)
941 if (name.contains(option + QByteArray(
" ")) && name.contains(
'<'))
949 option = option.mid(2);
953 s->findOption(orig, option, i, enabled, inOptions);
961 if (s->ignoreUnknown)
968 appOptions->d->addArgument(s->all_argv[i]);
969 if (everythingAfterArgIsArgs)
982 static int qt_argc = -1;
996 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
997 fprintf(stderr,
"Application has not called KCmdLineArgs::init(...).\n\n");
1003 Q_ASSERT(s->all_argc >= (args->
count()+1));
1004 qt_argc = args->
count() +1;
1016 if( s_qt_argv != NULL )
1021 s_qt_argv =
new char*[2];
1022 s_qt_argv[0] = qstrdup(s->all_argc?s->all_argv[0]:
"");
1031 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
1032 fprintf(stderr,
"The \"qt\" options have not be added to KCmdLineArgs!\n\n");
1039 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
1040 fprintf(stderr,
"Application has not called KCmdLineArgs::init(...).\n\n");
1047 s_qt_argv =
new char*[ count + 2 ];
1048 s_qt_argv[0] = qstrdup(s->all_argc?s->all_argv[0]:
"");
1050 for(; i <
count; i++)
1052 s_qt_argv[i+1] = qstrdup(args->d->parsedArgList->at(i));
1074 mainComponentData.
config();
1083 QByteArray localError = s->encodeOutput(error);
1084 if (localError.endsWith(
'\n'))
1086 fprintf(stderr,
"%s: %s\n", s->appName, localError.data());
1088 QString tmp =
i18n(
"Use --help to get a list of available command line options.");
1089 localError = s->encodeOutput(tmp);
1090 fprintf(stderr,
"%s: %s\n", s->appName, localError.data());
1098 Q_ASSERT(s->argsList != 0);
1101 QString optionFormatString = QString::fromLatin1(
" %1 %2\n");
1102 QString optionFormatStringDef = QString::fromLatin1(
" %1 %2 [%3]\n");
1106 KCmdLineArgsList::Iterator args = --(s->argsList->end());
1108 if ((*args)->d->id.isEmpty() && ((*args)->d->options.d->names.size() > 0) &&
1109 !(*args)->d->options.d->names[0].startsWith(
'+'))
1116 if (!(*args)->d->name.isEmpty())
1118 usage =
i18n(
"[%1-options]", (*args)->d->name.toString())+QLatin1Char(
' ')+
usage;
1120 if (args == s->argsList->begin())
1126 if (appOptions->d->id.isEmpty())
1129 for (
int i = 0; i < option.d->names.size(); i++)
1131 QByteArray opt_name = option.d->names[i];
1132 if (opt_name.startsWith(
'+'))
1133 usage += QString::fromLatin1(opt_name.mid(1)) + QLatin1Char(
' ');
1134 else if ( opt_name.startsWith(
"!+") )
1135 usage += QString::fromLatin1(opt_name.mid(2)) + QLatin1Char(
' ');
1140 s->printQ(QLatin1Char(
'\n')+s->about->shortDescription()+QLatin1Char(
'\n'));
1142 s->printQ(
i18n(
"\nGeneric options:\n"));
1143 s->printQ(optionFormatString.arg(QString::fromLatin1(
"--help"), -25)
1144 .arg(
i18n(
"Show help about options")));
1146 args = s->argsList->begin();
1147 while(args != s->argsList->end())
1149 if (!(*args)->d->name.isEmpty() && !(*args)->d->id.isEmpty())
1151 QString option = QString::fromLatin1(
"--help-%1").arg(QString::fromLatin1((*args)->d->id));
1152 QString desc =
i18n(
"Show %1 specific options", (*args)->d->name.toString());
1154 s->printQ(optionFormatString.arg(option, -25).arg(desc));
1159 s->printQ(optionFormatString.arg(QString::fromLatin1(
"--help-all"),-25).arg(
i18n(
"Show all options")));
1160 s->printQ(optionFormatString.arg(QString::fromLatin1(
"--author"),-25).arg(
i18n(
"Show author information")));
1161 s->printQ(optionFormatString.arg(QString::fromLatin1(
"-v, --version"),-25).arg(
i18n(
"Show version information")));
1162 s->printQ(optionFormatString.arg(QString::fromLatin1(
"--license"),-25).arg(
i18n(
"Show license information")));
1163 s->printQ(optionFormatString.arg(QString::fromLatin1(
"--"), -25).arg(
i18n(
"End of options")));
1165 args = s->argsList->begin();
1167 bool showAll = (
id ==
"all");
1171 while(args != s->argsList->end())
1173 if (
id == (*args)->d->id)
break;
1178 while(args != s->argsList->end())
1180 bool hasArgs =
false;
1181 bool hasOptions =
false;
1183 if (!(*args)->d->name.isEmpty())
1184 optionsHeader =
i18n(
"\n%1 options:\n", (*args)->d->name.toString());
1186 optionsHeader =
i18n(
"\nOptions:\n");
1188 while (args != s->argsList->end())
1193 for (
int i = 0; i < option.d->names.size(); i++)
1199 if (!option.d->descriptions[i].isEmpty()) {
1200 descriptionFull = option.d->descriptions[i].toString();
1204 if (option.d->names[i].startsWith(
':'))
1206 if (!descriptionFull.isEmpty())
1208 optionsHeader = QLatin1Char(
'\n')+descriptionFull;
1209 if (!optionsHeader.endsWith(QLatin1Char(
'\n')))
1210 optionsHeader.append(QLatin1Char(
'\n'));
1217 if (option.d->names[i].isEmpty())
1219 if (!descriptionFull.isEmpty())
1221 tmp = QLatin1Char(
'\n')+descriptionFull;
1222 if (!tmp.endsWith(QLatin1Char(
'\n')))
1223 tmp.append(QLatin1Char(
'\n'));
1230 if (!descriptionFull.isEmpty())
1232 dl = descriptionFull.split(QLatin1Char(
'\n'), QString::KeepEmptyParts);
1233 description = dl.first();
1234 dl.erase( dl.begin() );
1236 QByteArray name = option.d->names[i];
1237 if (name.startsWith(
'!'))
1240 if (name.startsWith(
'+'))
1244 s->printQ(
i18n(
"\nArguments:\n"));
1249 if (name.startsWith(
'[') && name.endsWith(
']'))
1250 name = name.mid(1, name.length()-2);
1251 s->printQ(optionFormatString.arg(QString::fromLocal8Bit(name), -25).arg(description));
1257 s->printQ(optionsHeader);
1261 if ((name.length() == 1) || (name[1] ==
' '))
1265 if (descriptionFull.isEmpty())
1272 if (option.d->defaults[i].isEmpty())
1274 s->printQ(optionFormatString.arg(QString::fromLatin1(opt), -25).arg(description));
1278 s->printQ(optionFormatStringDef.arg(QString::fromLatin1(opt), -25)
1279 .arg(description, option.d->defaults[i]));
1284 for(QStringList::Iterator it = dl.begin();
1288 s->printQ(optionFormatString.arg(
QString(), -25).arg(*it));
1293 if (args == s->argsList->end() || !(*args)->d->name.isEmpty() || (*args)->d->id.isEmpty())
1296 if (!showAll)
break;
1313 const QByteArray &_id)
1314 : d(new KCmdLineArgsPrivate(_options, _name, _id))
1323 if (!s.isDestroyed() && s->argsList)
1324 s->argsList->removeAll(
this);
1337 delete d->parsedArgList; d->parsedArgList = 0;
1338 delete d->parsedOptionList; d->parsedOptionList = 0;
1344 delete s->argsList; s->argsList = 0;
1349 KCmdLineArgsPrivate::save( QDataStream &ds)
const
1351 if (parsedOptionList)
1352 ds << (*(parsedOptionList));
1357 ds << (*(parsedArgList));
1363 KCmdLineArgsPrivate::load( QDataStream &ds)
1365 if (!parsedOptionList) parsedOptionList =
new KCmdLineParsedOptions;
1366 if (!parsedArgList) parsedArgList =
new KCmdLineParsedArgs;
1368 ds >> (*(parsedOptionList));
1369 ds >> (*(parsedArgList));
1371 if (parsedOptionList->count() == 0)
1373 delete parsedOptionList; parsedOptionList = 0;
1375 if (parsedArgList->count() == 0)
1377 delete parsedArgList; parsedArgList = 0;
1382 KCmdLineArgsPrivate::setOption(
const QByteArray &opt,
bool enabled)
1387 QByteArray argString =
"-";
1391 addArgument(argString);
1393 if (!parsedOptionList) {
1394 parsedOptionList =
new KCmdLineParsedOptions;
1398 parsedOptionList->insert( opt,
"t" );
1400 parsedOptionList->insert( opt,
"f" );
1404 KCmdLineArgsPrivate::setOption(
const QByteArray &opt,
const QByteArray &value)
1409 QByteArray argString =
"-";
1411 if (opt ==
"qmljsdebugger") {
1416 addArgument(argString +
"=" + value);
1418 addArgument(argString);
1422 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
1424 if (argString ==
"-display")
1426 setenv(
DISPLAY, value.data(),
true);
1430 if (!parsedOptionList) {
1431 parsedOptionList =
new KCmdLineParsedOptions;
1434 parsedOptionList->insertMulti( opt, value );
1440 QByteArray opt = _opt;
1442 if (d->parsedOptionList)
1444 value = d->parsedOptionList->value(opt);
1446 if (!value.isEmpty())
1447 return QString::fromLocal8Bit(value);
1450 QByteArray opt_name;
1453 int result = s->findOption( d->options, opt, opt_name, def, dummy) & ~4;
1457 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
1458 fprintf(stderr,
"Application requests for getOption(\"%s\") but the \"%s\" option\n",
1459 opt.data(), opt.data());
1460 fprintf(stderr,
"has never been specified via addCmdLineOptions( ... )\n\n");
1472 if (!d->parsedOptionList)
1477 QByteArray value = d->parsedOptionList->take(opt);
1478 if (value.isEmpty())
1480 result.prepend(QString::fromLocal8Bit(value));
1488 Q_FOREACH(
const QString &str, result)
1490 d->parsedOptionList->insertMulti(opt, str.toLocal8Bit());
1499 QByteArray opt = _opt;
1500 QByteArray opt_name;
1503 KCmdLineArgsList::Iterator args = s->argsList->begin();
1504 while (args != s->argsList->end())
1507 result = s->findOption((*args)->d->options, opt, opt_name, def, dummy) & ~4;
1514 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs):\n");
1515 fprintf(stderr,
"Application requests for isSet(\"%s\") but the \"%s\" option\n",
1516 opt.data(), opt.data());
1517 fprintf(stderr,
"has never been specified via addCmdLineOptions( ... )\n\n");
1524 if (d->parsedOptionList)
1526 value = d->parsedOptionList->value(opt);
1529 if (!value.isEmpty())
1534 return (value.at(0) ==
't');
1542 return (result == 2);
1548 return d->parsedArgList?d->parsedArgList->count():0;
1554 if (!d->parsedArgList || (n >= (
int) d->parsedArgList->count()))
1556 fprintf(stderr,
"\n\nFAILURE (KCmdLineArgs): Argument out of bounds\n");
1557 fprintf(stderr,
"Application requests for arg(%d) without checking count() first.\n",
1564 return QString::fromLocal8Bit(d->parsedArgList->at(n));
1575 const QString urlArg = QString::fromUtf8(_urlArg);
1576 QFileInfo fileInfo(urlArg);
1577 if (!fileInfo.isRelative()) {
1579 result.
setPath(QDir::fromNativeSeparators(urlArg));
1585 result.
setPath(
cwd()+QLatin1Char(
'/')+urlArg);
1590 return KUrl(urlArg);
1594 KCmdLineArgsPrivate::addArgument(
const QByteArray &argument)
1597 parsedArgList =
new KCmdLineParsedArgs;
1599 parsedArgList->append(argument);
1606 tmpopt.
add(
"tempfile",
ki18n(
"The files/URLs opened by the application will be deleted after use") );
1613 return args && args->
isSet(
"tempfile" );
1620 for(
int i = 0; i < s->all_argc; i++) {
1621 char*
arg = s->all_argv[i];
1624 lst.append(QString::fromLocal8Bit(arg));
QString i18n(const char *text)
Returns a localized version of a string.
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
Add options to your application.
static QStringList allArguments()
Returns the list of command-line arguments.
QStringList getOptionList(const QByteArray &option) const
Read out all occurrences of a string option.
static bool isTempFileSet()
static int & qtArgc()
Returns the number of arguments returned by qtArgv()
QString appName() const
Returns the application's internal name.
static KUrl makeURL(const QByteArray &urlArg)
Used by url().
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
Add command line option, by providing its name, description, and possibly a default value...
~KCmdLineArgs()
Destructor.
~KCmdLineOptions()
Destructor.
static const KAboutData * aboutData()
Returns the KAboutData for consumption by KComponentData.
static QString escape(const QString &text)
Convert &, ", ', <, > characters into XML entities &, <, >, ', ", respectively.
KLocalizedString ki18n(const char *msg)
Creates localized string from a given message.
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
static bool isRelativeUrl(const QString &_url)
Convenience function.
#define K_GLOBAL_STATIC(TYPE, NAME)
This macro makes it easy to use non-POD types as global statics.
static QString cwd()
Get the CWD (Current Working Directory) associated with the current command line arguments.
A class for command-line argument handling.
void cleanPath(const CleanPathOption &options=SimplifyDirSeparators)
Resolves "." and ".." components in path.
KUrl url(int n) const
Read out an argument representing a URL.
QString arg(int n) const
Read out an argument.
static char ** qtArgv()
Returns command line options for consumption by Qt after parsing them in a way that is consistent wit...
#define KDE_VERSION_STRING
Version of KDE as string, at compile time.
static void addTempFileOption()
Add standard option –tempfile.
static void usageError(const QString &error)
Print an error to stderr and the usage help to stdout and exit.
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
Represents and parses a URL.
QString i18nc(const char *ctxt, const char *text)
Returns a localized version of a string and a context.
int count() const
Read the number of arguments that aren't options (but, for example, filenames).
void setPath(const QString &path)
KCmdLineArgs(const KCmdLineOptions &_options, const KLocalizedString &_name, const QByteArray &_id)
Constructor.
static void loadAppArgs(QDataStream &)
Load arguments from a stream.
static void reset()
Reset all option definitions, i.e.
const KSharedConfig::Ptr & config() const
Returns the general config object ("appnamerc").
KCmdLineOptions()
Constructor.
static QString appName()
Get the appname according to argv[0].
This class is used to store information about a program.
friend class KCmdLineArgsList
static void usage(const QByteArray &id=QByteArray())
Print the usage help to stdout and exit.
static void setCwd(const QByteArray &cwd)
Made public for apps that don't use KCmdLineArgs To be done before makeURL, to set the current workin...
static void saveAppArgs(QDataStream &)
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
Initialize class.
QString getOption(const QByteArray &option) const
Read out a string option.
static void addStdCmdLineOptions(StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
add standard Qt/KDE command-line args
Class that holds command line options.
Class for producing and handling localized messages.
void clear()
Clear all options and arguments.
static void enable_i18n()
Enable i18n to be able to print a translated error message.
QString defaultValue(const QString &t)
KCmdLineOptions & operator=(const KCmdLineOptions &options)
Assignment operator.