-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.cpp
152 lines (126 loc) · 3.48 KB
/
commandline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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"
// create GPL statement string list
const char *CommandLine::s_gplStatement[] = {
QT_TR_NOOP("%1 Copyright (C) 2010-%2 Thunder422"),
QT_TR_NOOP("This program comes with ABSOLUTELY NO WARRANTY."),
QT_TR_NOOP("This is free software, and you are welcome to"),
QT_TR_NOOP("redistribute it under certain conditions."),
NULL
};
CommandLine::CommandLine(const QStringList &args)
{
// get base file name of program from first argument
m_programName = QFileInfo(args.at(0)).baseName();
m_returnCode = -1;
// NOTE: leave m_returnCode set to -1 to start GUI,
// parse command line arguments
if (isVersionOption(args))
{
m_returnCode = 0;
return;
}
Tester tester(args);
if (tester.hasError())
{
cout(stderr) << tester.errorMessage() << endl;
m_returnCode = 1;
return;
}
else if (tester.hasOption())
{
if (tester.run(cout(), this))
{
m_returnCode = 0;
}
else
{
coutClose(); // close stdout
cout(stderr) << tester.errorMessage() << endl;
m_returnCode = 1;
}
return;
}
// unsupported option (NOTE: other options get checked before this)
QStringList options = tester.options();
// append any other options here
options.prepend("-h|-?|-v");
m_returnCode = isHelpOption(args) ? 0 : 1; // error if not help option
cout(m_returnCode == 0 ? stdout : stderr) << tr("usage: ") << m_programName
<< options.join("|") << endl;
}
CommandLine::~CommandLine()
{
coutClose();
}
QTextStream &CommandLine::cout(FILE *stream)
{
if (!m_cout.device())
{
// setup standard output stream first time
QFile *output = new QFile;
output->open(stream, QIODevice::WriteOnly | QIODevice::Unbuffered);
m_cout.setDevice(output);
}
return m_cout;
}
void CommandLine::coutClose(void)
{
if (m_cout.device())
{
QIODevice *output = m_cout.device();
m_cout.setDevice(0);
delete output;
}
}
// function to check if version option was specified and to process it
bool CommandLine::isVersionOption(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(version()) << endl;
return true;
}
// function to check for help options
bool CommandLine::isHelpOption(const QStringList &args) const
{
return args.count() == 2 && (args.at(1) == "-?" || args.at(1) == "-h");
}
// function to return the copyright year value
int CommandLine::copyrightYear(void) const
{
return ibcp_COPYRIGHT_YEAR;
}
// function to return the version number string
QString CommandLine::version(void) const
{
return QString(ibcp_RELEASE_STRING + 7);
}
// end: commandline.cpp