Command Line Parser In QT 4

We all love GNU getopt, don’t we just wish we had it readily available for our Windows development and/or we didn’t have to tell are boss that we want to link to GPL software for some easy command line parsing?

Here at least is a simple class to ease the pain for QT C++. It allows for command line arguments in the form of /<switch> for simple Boolean switches and /<switch>: for switches that take parameters. For switches that take values, enclose the switch value in double quotes (i.e. /myswitch:"switch value with space"). The switches themselves can be one or more characters, and the same switch can appear multiple times.

Args.h

[source language=”cpp”]
#ifndef ARGS_H
#include <QMap>
#include <QStringList>

/*! \file Args.h
Header file for \sa MyNamespace::Args class.

*/

namespace MyNamespace {

/*!
\brief Command Line argument parsing class.

Simple argument parsing in the form of /<switch> or /<switch>:<value> Where <value> can be in quotes if it has an
embedded space. Multivalued switches, i.e. switches appearing more than once with different values are supported.

\class Args Args.h “Args.h”
*/
class Args
{
private:

QMap<QString, QStringList> _args;

public:

/*!
\brief Constructor

Arguments are populated using a call to QCoreApplication::arguments().
*/
Args();

public:

/*!
\brief Returns \c true if switch preent.

\param switchName Name of switch.
\return bool Returns \c true of the switch is found.
*/
bool Contains(const QString& switchName) const;

/*!
\brief Queries a single valued switch.

\param switchName Name of switch.
\param val Value for switch if found. If there is no value for the switch, \a val is set to an empty string.
\return bool Returns \c true if the switch is found.
*/
bool Get(const QString& switchName, QString& val) const;

/*!
\brief Queries a multivalued switch.

\param switchName Name of switch.
\param val QStringList to populate with switch values. If the switch as no values, then \a val will be an empty list.
\return bool Returns \c true if the switch is found.
*/
bool Get(const QString& switchName, QStringList& val) const;

};

}

#endif // ARGS_H
[/source]

Args.cpp
[source language=”cpp”]
/** \file Args.cpp
* Implementation for MyNamespace::Args class.
* Some more details here.
*/

#include "Args.h"
#include <QCoreApplication>
#include <QStringList>
#include <QDebug>

namespace MyNamespace {

Args::Args()
{
QStringList lst = QCoreApplication::arguments();
QString val;
QString switchVal;
QString switchName;

int idxOf = 0;
for (QStringList::iterator it = lst.begin(); it != lst.end(); ++it)
{
if ((*it)[0] == ‘/’)
{
if ((*it).count() > 1)
{
val = (*it).mid(1);
idxOf = val.indexOf(‘:’);
if (idxOf == -1)
{
switchName = val;
switchVal = QString();
}
else
{
switchName = val.left(idxOf);
switchVal = val.mid(idxOf+1);

}

if (switchName.count()) // far be it for someone to do something like /: on the command line
{
if (!_args.contains(switchName))
{
_args.insert(switchName, QStringList()) ;
}
if (switchVal.length())
{
_args[switchName].append(switchVal);
}
}
}
}
}
}

bool Args::Contains(const QString& switchName) const
{
return _args.contains(switchName);
}

bool Args::Get(const QString& switchVal, QString& val) const
{
if (!_args.contains(switchVal))
return false;

if ((_args[switchVal]).length() == 0)
{
val = QString();
}
else
{
val = _args[switchVal][0];
}

return true;
}

bool Args::Get(const QString& switchVal, QStringList& val) const
{
if (!_args.contains(switchVal))
return false;

val.append(_args[switchVal]);
return true;
}

}
[/source]

This entry was posted in QT 4 and tagged . Bookmark the permalink.

2 Responses to Command Line Parser In QT 4

  1. Aris Green says:

    Pretty stupid of me, fixed it.

  2. Anonym says:

    Hi,

    Your two files are the same but the three #include…

Leave a Reply

Your email address will not be published. Required fields are marked *