Skip to content

Commit

Permalink
added new command line class for handling command arguments
Browse files Browse the repository at this point in the history
the new CommandLine class now handles the Tester instance
the main function contains the instance of the CommandLine class
the CommandLine class handles the version option and usage message
the CommandLine class contains the GPL statement lines
the GPL statement lines are passed to Tester for output
  • Loading branch information
thunder422 committed Nov 27, 2012
1 parent a6cdf7f commit ea8e56d
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 64 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -132,6 +132,7 @@ add_custom_command(OUTPUT test_names.h

# list the include files
set(ibcp_HEADERS
commandline.h
ibcp.h
parser.h
table.h
Expand All @@ -143,6 +144,7 @@ set(ibcp_HEADERS
# list the main program sources
set(ibcp_SOURCES
commandhandlers.cpp
commandline.cpp
main.cpp
parser.cpp
table.cpp
Expand Down
122 changes: 122 additions & 0 deletions commandline.cpp
@@ -0,0 +1,122 @@
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: commandline.n - command line functions source file
// Copyright (C) 2012 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2012-11-25 initial version

#include <QFileInfo>

#include "ibcp_config.h" // for cmake
#include "commandline.h"
#include "test_ibcp.h"


CommandLine::CommandLine(const QStringList &args)
{
// get base file name of program from first argument
m_programName = QFileInfo(args.at(0)).baseName();
m_processed = false;

// parse command line arguments
if (version(args))
{
m_processed = true;
return;
}

// create GPL statement string list
m_gplStatement
.append(QString(QT_TR_NOOP("%1 Copyright (C) 2010-%2 Thunder422"))
.arg(m_programName).arg(ibcp_COPYRIGHT_YEAR));
m_gplStatement
.append(QT_TR_NOOP("This program comes with ABSOLUTELY NO WARRANTY."));
m_gplStatement
.append(QT_TR_NOOP("This is free software, and you are welcome to"));
m_gplStatement
.append(QT_TR_NOOP("redistribute it under certain conditions."));

Tester tester(args);
if (tester.hasError())
{
qWarning("%s", qPrintable(tester.errorMessage()));
m_processed = true;
return;
}
else if (tester.hasOption())
{
if (!tester.run(cout(), m_gplStatement))
{
qWarning("%s", qPrintable(tester.errorMessage()));
}
m_processed = true;
return;
}
// NOTE: leave m_processed set to false to start GUI,
// for now there is no GUI so always processed

// unsupported option (NOTE: other options get checked before this)
QStringList options = tester.options();
options.prepend("-v");
// append any other options here
qWarning("%s: %s %s", qPrintable(tr("usage")), qPrintable(m_programName),
qPrintable(options.join("|")));
m_processed = true;
}


CommandLine::~CommandLine()
{
if (m_cout.device())
{
QIODevice *output = m_cout.device();
m_cout.setDevice(0);
delete output;
}
}


QTextStream &CommandLine::cout(void)
{
if (!m_cout.device())
{
// setup standard output stream first time
QFile *output = new QFile;
output->open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);
m_cout.setDevice(output);
}
return m_cout;
}


// function to print version number
bool CommandLine::version(const QStringList &args)
{
if (args.count() != 2 || args.at(1) != "-v")
{
return false; // not our option or extra/invalid options
}
cout() << tr("%1 version %2").arg(m_programName)
.arg(ibcp_RELEASE_STRING + 7) << endl;
return true;
}


// end: commandline.cpp
54 changes: 54 additions & 0 deletions commandline.h
@@ -0,0 +1,54 @@
// vim:ts=4:sw=4:
//
// Interactive BASIC Compiler Project
// File: commandline.h - command line header file
// Copyright (C) 2012 Thunder422
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// For a copy of the GNU General Public License,
// see <http://www.gnu.org/licenses/>.
//
//
// Change History:
//
// 2012-11-25 initial version

#ifndef COMMANDLINE_H
#define COMMANDLINE_H

#include <QCoreApplication>
#include <QStringList>
#include <QTextStream>

class CommandLine
{
Q_DECLARE_TR_FUNCTIONS(CommandLine)

bool version(const QStringList &args);
QTextStream &cout(void);

QString m_programName;
QStringList m_gplStatement;
bool m_processed;
QTextStream m_cout;
public:
CommandLine(const QStringList &args);
~CommandLine();

bool processed(void) const // if processed then exit program
{
return m_processed;
}
};


#endif // COMMANDLINE_H
64 changes: 9 additions & 55 deletions main.cpp
Expand Up @@ -23,73 +23,27 @@
// 2010-03-13 initial version

#include <QtGui/QApplication>
#include <QFileInfo>
#include <QTextStream>
#include <QTimer>

#include "ibcp_config.h" // for cmake
#include "test_ibcp.h"


void printGplHeader(QTextStream &cout, const QString &name)
{
cout << endl << name << " Copyright (C) 2010-" << ibcp_COPYRIGHT_YEAR
<< " Thunder422" << endl
<< "This program comes with ABSOLUTELY NO WARRANTY." << endl
<< "This is free software, and you are welcome to" << endl
<< "redistribute it under certain conditions." << endl << endl;
}


// function to print version number
bool ibcpVersion(QTextStream &cout, const QString &name, QStringList &args)
{
if (args.count() != 2 || args.at(1) != "-v")
{
return false; // not our option or extra/invalid options
}
cout << name << QObject::tr(" version %1").arg(ibcp_RELEASE_STRING + 7)
<< endl;
return true;
}
#include "commandline.h"


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QStringList args = app.arguments();

// get base file name of program from first argument
QString programName = QFileInfo(args.at(0)).baseName();

// setup standard output stream
QFile output;
output.open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);
QTextStream cout(&output);

if (!ibcpVersion(cout, programName, args))
CommandLine commandLine(app.arguments());
if (commandLine.processed())
{
Tester tester(args);
if (tester.hasError())
{
qWarning("%s", qPrintable(tester.errorMessage()));
}
else if (!tester.hasOption())
{
QStringList options = tester.options();
options.prepend("-v");
qWarning("%s: %s %s", qPrintable(QObject::tr("usage")),
qPrintable(programName), qPrintable(options.join("|")));
}
else if (!tester.run(cout))
{
qWarning("%s", qPrintable(tester.errorMessage()));
}
// force quit once event processing loop is started
QTimer::singleShot(0, &app, SLOT(quit()));
}
else
{
// start GUI here
}

// force quit once event processing loop is started
QTimer::singleShot(0, &app, SLOT(quit()));
return app.exec();
}

Expand Down
17 changes: 10 additions & 7 deletions test_ibcp.cpp
Expand Up @@ -34,12 +34,9 @@
#include "translator.h"


extern void printGplHeader(QTextStream &cout, const QString &name);


// function to process a test input file specified on the command line
// or accept input lines from the user
Tester::Tester(QStringList &args)
Tester::Tester(const QStringList &args)
{
QString name[OptSizeOf];
name[OptParser] = "parser";
Expand Down Expand Up @@ -123,7 +120,7 @@ QStringList Tester::options(void) const
}


bool Tester::run(QTextStream &cout)
bool Tester::run(QTextStream &cout, const QStringList &gplStatement)
{
QFile file;
QTextStream input(&file);
Expand Down Expand Up @@ -158,8 +155,14 @@ bool Tester::run(QTextStream &cout)
}
qFatal("%s", qPrintable(tr("Program aborting!")));
}
printGplHeader(cout, m_programName);
cout << "Table initialization successful." << endl;

cout << endl;
foreach (QString line, gplStatement)
{
cout << line << endl;
}

cout << endl << "Table initialization successful." << endl;

Translator translator(Table::instance());

Expand Down
4 changes: 2 additions & 2 deletions test_ibcp.h
Expand Up @@ -63,11 +63,11 @@ class Tester
QString m_testFileName; // name of test file (OptFile only)
QString m_errorMessage; // message if error occurred
public:
Tester(QStringList &args);
Tester(const QStringList &args);
~Tester(void) {}

QStringList options(void) const;
bool run(QTextStream &cout);
bool run(QTextStream &cout, const QStringList &gplStatement);
bool hasOption(void) const // has a test option been specified?
{
return m_option != OptNone;
Expand Down

0 comments on commit ea8e56d

Please sign in to comment.