simplifying a function

Dramatic simplification of legacy code by separating code and data and applying some later C++.
Function removed from Application.cpp as it was in a anonymous namespace and could not easily be subjected to unit testing.
Added ProgramOptionsUtilities.h
This commit is contained in:
berniev
2023-03-08 08:50:46 +10:00
committed by wwmayer
parent eba80267ff
commit de1acd926e
5 changed files with 125 additions and 50 deletions

View File

@@ -51,6 +51,7 @@
#include <QProcessEnvironment>
#include <QStandardPaths>
#include <LibraryVersions.h>
#include <array>
#include <App/MaterialPy.h>
#include <App/MetadataPy.h>
@@ -107,6 +108,7 @@
#include "Part.h"
#include "PartPy.h"
#include "Placement.h"
#include "ProgramOptionsUtilities.h"
#include "Property.h"
#include "PropertyContainer.h"
#include "PropertyExpressionEngine.h"
@@ -2145,53 +2147,6 @@ void Application::initTypes()
}
namespace {
pair<string, string> customSyntax(const string& s)
{
#if defined(FC_OS_MACOSX)
if (s.find("-psn_") == 0)
return make_pair(string("psn"), s.substr(5));
#endif
if (s.find("-display") == 0)
return make_pair(string("display"), string("null"));
else if (s.find("-style") == 0)
return make_pair(string("style"), string("null"));
else if (s.find("-graphicssystem") == 0)
return make_pair(string("graphicssystem"), string("null"));
else if (s.find("-widgetcount") == 0)
return make_pair(string("widgetcount"), string(""));
else if (s.find("-geometry") == 0)
return make_pair(string("geometry"), string("null"));
else if (s.find("-font") == 0)
return make_pair(string("font"), string("null"));
else if (s.find("-fn") == 0)
return make_pair(string("fn"), string("null"));
else if (s.find("-background") == 0)
return make_pair(string("background"), string("null"));
else if (s.find("-bg") == 0)
return make_pair(string("bg"), string("null"));
else if (s.find("-foreground") == 0)
return make_pair(string("foreground"), string("null"));
else if (s.find("-fg") == 0)
return make_pair(string("fg"), string("null"));
else if (s.find("-button") == 0)
return make_pair(string("button"), string("null"));
else if (s.find("-btn") == 0)
return make_pair(string("btn"), string("null"));
else if (s.find("-name") == 0)
return make_pair(string("name"), string("null"));
else if (s.find("-title") == 0)
return make_pair(string("title"), string("null"));
else if (s.find("-visual") == 0)
return make_pair(string("visual"), string("null"));
// else if (s.find("-ncols") == 0)
// return make_pair(string("ncols"), boost::program_options::value<int>(1));
// else if (s.find("-cmap") == 0)
// return make_pair(string("cmap"), string("null"));
else if ('@' == s[0])
return std::make_pair(string("response-file"), s.substr(1));
else
return make_pair(string(), string());
}
void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& vm)
{
@@ -2311,7 +2266,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v
try {
store( boost::program_options::command_line_parser(args).
options(cmdline_options).positional(p).extra_parser(customSyntax).run(), vm);
options(cmdline_options).positional(p).extra_parser(Util::customSyntax).run(), vm);
std::ifstream ifs("FreeCAD.cfg");
if (ifs)
@@ -2358,7 +2313,7 @@ void parseProgramOptions(int ac, char ** av, const string& exe, variables_map& v
copy(tok.begin(), tok.end(), back_inserter(args));
// Parse the file and store the options
store( boost::program_options::command_line_parser(args).
options(cmdline_options).positional(p).extra_parser(customSyntax).run(), vm);
options(cmdline_options).positional(p).extra_parser(Util::customSyntax).run(), vm);
}
}

View File

@@ -0,0 +1,81 @@
/***************************************************************************
* Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
***************************************************************************/
#ifndef PROGRAMOPTIONSUTILITIES_H
#define PROGRAMOPTIONSUTILITIES_H
#include <string>
namespace App::Util{
std::pair<std::string, std::string> customSyntax(std::string_view strIn)
{
if(strIn.size() < 2) {
return{};
}
char leadChr {strIn[0]};
std::string rest {strIn.substr(1)};
if (leadChr == '@') {
return {"response-file", rest};
}
if (leadChr != '-') {
return {};
}
#if defined(FC_OS_MACOSX)
if (rest.find("psn_") == 0) {
return {"psn", rest.substr(4)};
}
#endif
if(rest == "widgetcount"){
return {rest, ""};
}
constexpr std::array knowns {"display",
"style",
"graphicssystem",
"geometry",
"font",
"fn",
"background",
"bg",
"foreground",
"fg",
"button",
"btn",
"name",
"title",
"visual"};
if(std::find(knowns.begin(), knowns.end(), rest) != knowns.end()) {
return {rest, "null"};
}
return {};
}
} // namespace
#endif// PROGRAMOPTIONSUTILITIES_H

View File

@@ -89,7 +89,7 @@ void PropertyQuantity::setPyObject(PyObject *value)
{
// Set the unit if Unit object supplied, else check the unit
// and set the value
if (PyObject_TypeCheck(value, &(UnitPy::Type))) {
Base::UnitPy *pcObject = static_cast<Base::UnitPy*>(value);
Base::Unit unit = *(pcObject->getUnitPtr());

View File

@@ -0,0 +1,38 @@
#include "gtest/gtest.h"
#define FC_OS_MACOSX 1
#include "App/ProgramOptionsUtilities.h"
using namespace App::Util;
using Spr = std::pair<std::string, std::string>;
TEST(ApplicationTest, fCustomSyntaxLookup){
Spr res {customSyntax("-display")};
Spr exp {"display", "null"};
EXPECT_EQ(res,exp);
};
TEST(ApplicationTest, fCustomSyntaxMac){
Spr res {customSyntax("-psn_stuff")};
Spr exp {"psn", "stuff"};
EXPECT_EQ(res,exp);
};
TEST(ApplicationTest, fCustomSyntaxWidgetCount){
Spr res {customSyntax("-widgetcount")};
Spr exp {"widgetcount", ""};
EXPECT_EQ(res,exp);
}
TEST(ApplicationTest, fCustomSyntaxNotFound){
Spr res {customSyntax("-displayx")};
Spr exp {"", ""};
EXPECT_EQ(res,exp);
};
TEST(ApplicationTest, fCustomSyntaxAmpersand){
Spr res {customSyntax("@freddie")};
Spr exp {"response-file", "freddie"};
EXPECT_EQ(res,exp);
};
TEST(ApplicationTest, fCustomSyntaxEmptyIn){
Spr res {customSyntax("")};
Spr exp {"", ""};
EXPECT_EQ(res,exp);
};

View File

@@ -9,4 +9,5 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/MappedElement.cpp
${CMAKE_CURRENT_SOURCE_DIR}/MappedName.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Metadata.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Application.cpp
)