25 #include <QStringList> 26 #include <QCoreApplication> 69 : m_value(QVariant()),
71 m_exitImmediately(
true),
106 AddPriv(QStringLiteral(
"h,help"), QVariant(), QStringLiteral(
"Display full usage information."),
TorcCommandLine::Help,
true);
108 AddPriv(QStringLiteral(
"l,log"), QStringList(QStringLiteral(
"general")), QStringLiteral(
"Set the logging level."),
TorcCommandLine::None);
110 AddPriv(QStringLiteral(
"v,verbose,verbosity"), QStringLiteral(
"info"), QStringLiteral(
"Set the logging type."),
TorcCommandLine::None);
112 AddPriv(QStringLiteral(
"version"), QVariant(), QStringLiteral(
"Display version information."),
TorcCommandLine::Version,
true);
114 AddPriv(QStringLiteral(
"db,database"), QStringLiteral(
""), QStringLiteral(
"Use a custom database location. If the file does not exist, it will be created."));
116 AddPriv(QStringLiteral(
"logfile"), QStringLiteral(
""), QStringLiteral(
"Override the logfile location."));
118 AddPriv(QStringLiteral(
"xsdtest"), QStringLiteral(
""), QStringLiteral(
"Run validation of test configuration XML files found in the given directory."),
TorcCommandLine::XSDTest);
120 AddPriv(QStringLiteral(
"c,config"), QStringLiteral(
""), QStringLiteral(
"Override the configuration directory for XML config file, database etc."));
122 AddPriv(QStringLiteral(
"s,share"), QStringLiteral(
""), QStringLiteral(
"Overrride the shared directory for HTML files etc"));
124 AddPriv(QStringLiteral(
"t,trans"), QStringLiteral(
""), QStringLiteral(
"Override the translations directory"));
134 void TorcCommandLine::Add(
const QString &Keys,
const QVariant &Default,
const QString &HelpText,
bool ExitImmediately)
139 void TorcCommandLine::AddPriv(
const QString &Keys,
const QVariant &Default,
const QString &HelpText, TorcCommandLine::Options Flags ,
bool ExitImmediately )
141 QStringList keys = Keys.split(
',');
147 foreach (
const QString &key, keys)
149 if (key.contains(
'='))
151 std::cout << QStringLiteral(
"Invalid option '%1'").arg(key).toLocal8Bit().constData() << std::endl;
153 else if (first && !m_options.contains(key))
156 m_options.insert(key,
TorcArgument(Default, HelpText, Flags, ExitImmediately));
157 valid.append(
"-" + key);
160 else if (!first && !m_aliases.contains(key) && !m_options.contains(key))
162 m_aliases.insert(key, master);
163 valid.append(
"-" + key);
167 std::cout << QStringLiteral(
"Command line option '%1' already in use - ignoring").arg(key).toLocal8Bit().constData() << std::endl;
171 if (!valid.isEmpty())
173 QString options = valid.join(QStringLiteral(
" OR "));
174 m_help.insert(options, HelpText);
175 if (options.size() > (int)m_maxLength)
176 m_maxLength = options.size() + 2;
189 bool parserror =
false;
191 bool printhelp =
false;
192 bool printversion =
false;
195 for (
int i = 1; i < argc; ++i)
197 QString key = QString::fromLocal8Bit(argv[i]);
200 while (key.startsWith(
'-'))
203 QString value(QStringLiteral(
""));
205 bool simpleformat = key.contains(
'=');
209 QStringList keyval = key.split(
'=');
210 key = keyval.at(0).trimmed();
211 value = keyval.at(1).trimmed();
215 if (!m_options.contains(key))
218 if (!m_aliases.contains(key))
221 error = QStringLiteral(
"Unknown command line option '%1'").arg(key);
226 key = m_aliases.value(key);
233 if (!simpleformat && argument.
m_value.isValid())
238 error = QStringLiteral(
"Insufficient arguments - option '%1' requires a value").arg(key);
242 value = QString::fromLocal8Bit(argv[++i]).trimmed();
244 if (value.startsWith(
'='))
247 error = QStringLiteral(
"Option '%1' expects a value").arg(key);
252 if (!argument.
m_value.isValid())
254 if (!value.isEmpty())
258 error = QStringLiteral(
"Option '%1' does not expect a value ('%2')").arg(key, value);
264 argument.
m_value = QVariant((
bool)
true);
267 else if (argument.
m_value.isValid() && value.isEmpty())
270 error = QStringLiteral(
"Option '%1' expects a value").arg(key).toLocal8Bit();
279 if (!value.isEmpty())
281 switch ((QMetaType::Type)argument.
m_value.type())
283 case QMetaType::QStringList:
284 argument.
m_value = QVariant(QStringList(value));
287 argument.
m_value = QVariant(value);
290 m_options.insert(key, argument);
299 std::cout << error.toLocal8Bit().constData() << std::endl << std::endl;
304 std::cout <<
"Command line options:" << std::endl << std::endl;
306 QHash<QString,QString>::const_iterator it = m_help.constBegin();
307 for ( ; it != m_help.constEnd(); ++it)
309 QByteArray option(it.key().toLocal8Bit().constData());
310 QByteArray padding(m_maxLength - option.size(), 32);
311 std::cout << option.constData() << padding.constData() << it.value().toLocal8Bit().constData() << std::endl;
314 std::cout << std::endl <<
"All options may be preceeded by '-' or '--'" << std::endl;
320 std::cout << std::endl <<
"The following environment variables may be useful:" << std::endl;
324 QByteArray var(variable->
m_variable.toLocal8Bit().constData());
325 QByteArray padding(m_maxLength - var.size(), 32);
326 std::cout << var.constData() << padding.constData() << variable->
m_description.toLocal8Bit().constData() << std::endl;
335 std::cout <<
"QT Version : " << QT_VERSION_STR << std::endl;
348 if (m_options.contains(Key))
349 return m_options.value(Key).m_value;
351 if (m_aliases.contains(Key))
352 return m_options.value(m_aliases.value(Key)).m_value;
#define TORC_EXIT_INVALID_CMDLINE
Torc command line handler.
TorcCommandLine(TorcCommandLine::Options Flags)
void Add(const QString &Keys, const QVariant &Default, const QString &HelpText, bool ExitImmediately)
Implement custom command line options.
static bool RegisterEnvironmentVariable(const QString &Var, const QString &Description)
Register an environment variable for display via the help option.
A simple private linked list to manage registered environment variables.
TorcEnvironmentVariable * nextEnvironmentVariable
TorcCommandLine::Options m_flags
Simple wrapper around a command line argument.
static TorcEnvironmentVariable * gEnvironmentVariable
int Evaluate(int argc, const char *const *argv, bool &Exit)
Evaluate the command line options.
QVariant GetValue(const QString &Key)
Return the value associated with Key or an invalid QVariant if the option is not present.
TorcEnvironmentVariable(const QString &Var, const QString &Description)