Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new command line class for handling command arguments
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
1 parent
a6cdf7f
commit ea8e56d
Showing
6 changed files
with
199 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters