Test mode for Valentina.exe.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2015-10-03 20:27:27 +03:00
parent 693dd7a06e
commit f310c6a479
4 changed files with 84 additions and 56 deletions

View file

@ -499,31 +499,27 @@ void MApplication::RetranslateTables()
void MApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments) void MApplication::ParseCommandLine(const SocketConnection &connection, const QStringList &arguments)
{ {
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "Valentina's measurements editor.")); parser.setApplicationDescription(tr("Valentina's measurements editor."));
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("filename", QCoreApplication::translate("main", "The measurement file.")); parser.addPositionalArgument("filename", tr("The measurement file."));
//----- //-----
QCommandLineOption heightOption(QStringList() << "e" << "height", QCommandLineOption heightOption(QStringList() << "e" << "height",
QCoreApplication::translate("main", "Open with the base height: 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, " tr("Open with the base height: 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, 164, 170, 176, 182 or "
"152, 158, 164, 170, 176, 182 or 188 cm."), "188 cm."), tr("The base height"));
QCoreApplication::translate("main", "The base height"));
parser.addOption(heightOption); parser.addOption(heightOption);
//----- //-----
QCommandLineOption sizeOption(QStringList() << "s" << "size", QCommandLineOption sizeOption(QStringList() << "s" << "size",
QCoreApplication::translate("main", "Open with the base size: 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, " tr("Open with the base size: 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54 or 56 cm."),
"48, 50, 52, 54 or 56 cm."), tr("The base size"));
QCoreApplication::translate("main", "The base size"));
parser.addOption(sizeOption); parser.addOption(sizeOption);
//----- //-----
QCommandLineOption unitOption(QStringList() << "u" << "unit", QCommandLineOption unitOption(QStringList() << "u" << "unit",
QCoreApplication::translate("main", "Set pattern file unit: cm, mm, inch."), tr("Set pattern file unit: cm, mm, inch."), tr("The pattern unit"));
QCoreApplication::translate("main", "The pattern unit"));
parser.addOption(unitOption); parser.addOption(unitOption);
//----- //-----
QCommandLineOption testOption(QStringList() << "test", QCommandLineOption testOption(QStringList() << "test",
QCoreApplication::translate("main", "Use for unit testing. Run the program and open a file without showing a " tr("Use for unit testing. Run the program and open a file without showing a window."));
"window."));
parser.addOption(testOption); parser.addOption(testOption);
//----- //-----
parser.process(arguments); parser.process(arguments);
@ -548,9 +544,9 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
} }
else else
{ {
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", qCCritical(mApp, "%s\n",
"Error: Invalid base height argument. Must be 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158, " qPrintable(tr("Invalid base height argument. Must be 92, 98, 104, 110, 116, 122, 128, 134, "
"164, 170, 176, 182 or 188 cm."))); "140, 146, 152, 158, 164, 170, 176, 182 or 188 cm.")));
parser.showHelp(V_EX_USAGE); parser.showHelp(V_EX_USAGE);
} }
} }
@ -568,9 +564,9 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
} }
else else
{ {
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", qCCritical(mApp, "%s\n",
"Error: Invalid base size argument. Must be 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, " qPrintable(tr("Invalid base size argument. Must be 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, "
"52, 54 or 56 cm."))); "42, 44, 46, 48, 50, 52, 54 or 56 cm.")));
parser.showHelp(V_EX_USAGE); parser.showHelp(V_EX_USAGE);
} }
} }
@ -591,8 +587,7 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
} }
else else
{ {
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", qCCritical(mApp, "%s\n", qPrintable(tr("Invalid base size argument. Must be cm, mm or inch.")));
"Error: Invalid base size argument. Must be cm, mm or inch.")));
parser.showHelp(V_EX_USAGE); parser.showHelp(V_EX_USAGE);
} }
} }
@ -628,8 +623,8 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
QLocalServer::removeServer(serverName); QLocalServer::removeServer(serverName);
if (not localServer->listen(serverName)) if (not localServer->listen(serverName))
{ {
qCWarning(mApp, "Can't begin to listen for incoming connections on name '%s'", qCWarning(mApp, "%s",
qUtf8Printable(serverName)); qUtf8Printable(tr("Can't begin to listen for incoming connections on name '%1'").arg(serverName)));
} }
} }
} }
@ -642,8 +637,7 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
{ {
if (testMode && args.count() > 1) if (testMode && args.count() > 1)
{ {
fprintf(stderr, "%s\n", qPrintable(QCoreApplication::translate("main", qCCritical(mApp, "%s\n", qPrintable(tr("Test mode doesn't support openning several files.")));
"Error: Test mode doesn't support openning several files.")));
parser.showHelp(V_EX_USAGE); parser.showHelp(V_EX_USAGE);
} }
@ -674,6 +668,11 @@ void MApplication::ParseCommandLine(const SocketConnection &connection, const QS
{ {
NewMainWindow(); NewMainWindow();
} }
else
{
qCCritical(mApp, "%s\n", qPrintable(tr("Please, provide one input file.")));
parser.showHelp(V_EX_USAGE);
}
} }
} }

View file

@ -50,8 +50,8 @@ const static auto SINGLE_OPTION_GAPWIDTH = QStringLiteral("G");
const static auto LONG_OPTION_GROUPPING = QStringLiteral("groups"); const static auto LONG_OPTION_GROUPPING = QStringLiteral("groups");
const static auto SINGLE_OPTION_GROUPPING = QStringLiteral("g"); const static auto SINGLE_OPTION_GROUPPING = QStringLiteral("g");
#define tr(A) QCoreApplication::translate("VCommandLine", (A)) const static auto LONG_OPTION_TEST = QStringLiteral("test");
//--------------------------------------------------------------------------------------------------------------------- const static auto SINGLE_OPTION_TEST = QStringLiteral("t");
//such a tricky initialization is used, because it uses static functions which relay on static variables and order of //such a tricky initialization is used, because it uses static functions which relay on static variables and order of
//initialization is not defined between compiled units. i.e. - segv is possible (I hit it when //initialization is not defined between compiled units. i.e. - segv is possible (I hit it when
@ -139,14 +139,20 @@ VCommandLine::VCommandLine() : parser()
{LONG_OPTION_GROUPPING, {LONG_OPTION_GROUPPING,
new QCommandLineOption(QStringList() << SINGLE_OPTION_GROUPPING << LONG_OPTION_GROUPPING, new QCommandLineOption(QStringList() << SINGLE_OPTION_GROUPPING << LONG_OPTION_GROUPPING,
tr("Sets layout groupping (export mode): ") tr("Sets layout groupping (export mode): ")
+ DialogLayoutSettings::MakeGroupsHelp(), tr("Grouping type"), "2")} + DialogLayoutSettings::MakeGroupsHelp(), tr("Grouping type"), "2")},
{LONG_OPTION_TEST,
new QCommandLineOption(QStringList() << SINGLE_OPTION_TEST << LONG_OPTION_TEST,
tr("Run the program in a test mode. The program this mode load a single pattern "
"file and silently quit without showing the main window. The key have priority "
"before key '%1'.").arg(LONG_OPTION_OUTFILE))}
}), }),
isGuiEnabled(false) isGuiEnabled(false)
{ {
parser.setApplicationDescription(QCoreApplication::translate("main", "Pattern making program.")); parser.setApplicationDescription(tr("Pattern making program."));
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
parser.addPositionalArgument("filename", QCoreApplication::translate("main", "Pattern file.")); parser.addPositionalArgument("filename", tr("Pattern file."));
QMap<QString, QCommandLineOption *>::const_iterator i = optionsUsed.constBegin(); QMap<QString, QCommandLineOption *>::const_iterator i = optionsUsed.constBegin();
while (i != optionsUsed.constEnd()) while (i != optionsUsed.constEnd())
@ -188,12 +194,14 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
if ((a || b || c) && x) if ((a || b || c) && x)
{ {
Error(tr("Cannot use pageformat and page explicit size/units together.")); qCritical() << tr("Cannot use pageformat and page explicit size/units together.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
if ((a || b || c) && !(a && b && c)) if ((a || b || c) && !(a && b && c))
{ {
Error(tr("Page height, width, units must be used all 3 at once.")); qCritical() << tr("Page height, width, units must be used all 3 at once.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
} }
@ -207,7 +215,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
// if ((a || b) && !(a && b)) // if ((a || b) && !(a && b))
// { // {
// Error(tr("Shift length must be used together with shift units.")); // qCritical() << tr("Shift length must be used together with shift units.") << "\n";
// const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
// } // }
// } // }
@ -218,14 +227,16 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
{ {
if (!diag.SetIncrease(rotateDegree)) if (!diag.SetIncrease(rotateDegree))
{ {
Error(tr("Invalid rotation value. That must be one of predefined values.")); qCritical() << tr("Invalid rotation value. That must be one of predefined values.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
} }
// if present units MUST be set before any other to keep conversions correct // if present units MUST be set before any other to keep conversions correct
if (!diag.SelectTemplate(OptPaperSize())) if (!diag.SelectTemplate(OptPaperSize()))
{ {
Error(tr("Unknown page templated selected.")); qCritical() << tr("Unknown page templated selected.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
if (parser.isSet(*optionsUsed.value(LONG_OPTION_PAGEH))) //at this point we already sure 3 are set or none if (parser.isSet(*optionsUsed.value(LONG_OPTION_PAGEH))) //at this point we already sure 3 are set or none
@ -233,7 +244,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
if (!diag.SelectPaperUnit(parser.value(*optionsUsed.value(LONG_OPTION_PAGEUNITS)))) if (!diag.SelectPaperUnit(parser.value(*optionsUsed.value(LONG_OPTION_PAGEUNITS))))
{ {
Error(tr("Unsupported paper units.")); qCritical() << tr("Unsupported paper units.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
diag.SetPaperHeight (Pg2Px(parser.value(*optionsUsed.value(LONG_OPTION_PAGEH)), diag)); diag.SetPaperHeight (Pg2Px(parser.value(*optionsUsed.value(LONG_OPTION_PAGEH)), diag));
@ -244,7 +256,8 @@ VLayoutGeneratorPtr VCommandLine::DefaultGenerator() const
{ {
if (!diag.SelectLayoutUnit(parser.value(*optionsUsed.value(LONG_OPTION_SHIFTUNITS)))) if (!diag.SelectLayoutUnit(parser.value(*optionsUsed.value(LONG_OPTION_SHIFTUNITS))))
{ {
Error(tr("Unsupported layout units.")); qCritical() << tr("Unsupported layout units.") << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
} }
@ -281,7 +294,7 @@ VCommandLinePtr VCommandLine::Get(const QCoreApplication& app)
instance->parser.process(app); instance->parser.process(app);
//fixme: in case of additional options/modes which will need to disable GUI - add it here too //fixme: in case of additional options/modes which will need to disable GUI - add it here too
instance->isGuiEnabled = !instance->IsExportEnabled(); instance->isGuiEnabled = not (instance->IsExportEnabled() || instance->IsTestModeEnabled());
return instance; return instance;
} }
@ -293,26 +306,32 @@ VCommandLine::~VCommandLine()
optionsUsed.clear(); optionsUsed.clear();
} }
//------------------------------------------------------------------------------------------------------
Q_NORETURN void VCommandLine::Error(const QString &text) const
{
vStdErr() << text << "\n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
//------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------
void VCommandLine::Reset() void VCommandLine::Reset()
{ {
instance.reset(); instance.reset();
} }
//---------------------------------------------------------------------------------------------------------------------
bool VCommandLine::IsTestModeEnabled() const
{
const bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_TEST));
if (r && parser.positionalArguments().size() != 1)
{
qCritical() << tr("Test option can be used with single input file only.") << "/n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
}
return r;
}
//------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------
bool VCommandLine::IsExportEnabled() const bool VCommandLine::IsExportEnabled() const
{ {
bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_OUTFILE)); const bool r = parser.isSet(*optionsUsed.value(LONG_OPTION_OUTFILE));
if (r && parser.positionalArguments().size() != 1) if (r && parser.positionalArguments().size() != 1)
{ {
Error(tr("Export options can be used with single input file only.")); qCritical() << tr("Export options can be used with single input file only.") << "/n";
const_cast<VCommandLine*>(this)->parser.showHelp(V_EX_USAGE);
} }
return r; return r;
} }
@ -399,5 +418,3 @@ bool VCommandLine::IsGuiEnabled() const
{ {
return isGuiEnabled; return isGuiEnabled;
} }
#undef tr

View file

@ -20,6 +20,7 @@ typedef std::shared_ptr<VCommandLine> VCommandLinePtr;
//QCommandLineParser* object must exists until this object alive //QCommandLineParser* object must exists until this object alive
class VCommandLine class VCommandLine
{ {
Q_DECLARE_TR_FUNCTIONS(VCommandLine)
private: private:
static VCommandLinePtr instance; static VCommandLinePtr instance;
QCommandLineParser parser; QCommandLineParser parser;
@ -41,9 +42,6 @@ protected:
Cases OptGroup() const; Cases OptGroup() const;
//@brief convinient shortcut to show error and help and exit
Q_NORETURN void Error(const QString& text) const;
//@brief: called in destructor of application, so instance destroyed and new maybe created (never happen scenario though) //@brief: called in destructor of application, so instance destroyed and new maybe created (never happen scenario though)
static void Reset(); static void Reset();
@ -56,6 +54,9 @@ public:
//@brief creates object and applies export related options to parser //@brief creates object and applies export related options to parser
//@brief tests if user enabled test mode from cmd, throws exception if not exactly 1 input VAL file supplied in case test mode enabled
bool IsTestModeEnabled() const;
//@brief tests if user enabled export from cmd, throws exception if not exactly 1 input VAL file supplied in case export enabled //@brief tests if user enabled export from cmd, throws exception if not exactly 1 input VAL file supplied in case export enabled
bool IsExportEnabled() const; bool IsExportEnabled() const;

View file

@ -66,17 +66,28 @@ int main(int argc, char *argv[])
w.show(); w.show();
w.ReopenFilesAfterCrash(args); w.ReopenFilesAfterCrash(args);
} }
else
{
if (args.size() != 1)
{
qCritical() << QCoreApplication::translate("vmain", "Please, provide one input file.");
std::exit(V_EX_NOINPUT);
}
}
for (size_t i=0, sz = args.size(); i < sz;++i) for (int i=0, sz = args.size(); i < sz; ++i)
{ {
const bool loaded = w.LoadPattern(args.at(static_cast<int>(i)), app.CommandLine()->OptMeasurePath()); const bool loaded = w.LoadPattern(args.at(static_cast<int>(i)), app.CommandLine()->OptMeasurePath());
if (app.CommandLine()->IsExportEnabled()) if (not app.CommandLine()->IsTestModeEnabled())
{ {
if (loaded) if (app.CommandLine()->IsExportEnabled())
{ {
w.DoExport(app.CommandLine()); if (loaded)
{
w.DoExport(app.CommandLine());
}
break;
} }
break;
} }
} }