diff --git a/src/App/Application.cpp b/src/App/Application.cpp index d9f20d6ae8..590234674f 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include @@ -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 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(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); } } diff --git a/src/App/ProgramOptionsUtilities.h b/src/App/ProgramOptionsUtilities.h new file mode 100644 index 0000000000..a4a93df859 --- /dev/null +++ b/src/App/ProgramOptionsUtilities.h @@ -0,0 +1,81 @@ +/*************************************************************************** +* Copyright (c) 2002 Jürgen Riegel * +* * +* 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 + +namespace App::Util{ + +std::pair 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 diff --git a/src/App/PropertyUnits.cpp b/src/App/PropertyUnits.cpp index 66b2149200..f7106d1061 100644 --- a/src/App/PropertyUnits.cpp +++ b/src/App/PropertyUnits.cpp @@ -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(value); Base::Unit unit = *(pcObject->getUnitPtr()); diff --git a/tests/src/App/Application.cpp b/tests/src/App/Application.cpp new file mode 100644 index 0000000000..d89fd08e8a --- /dev/null +++ b/tests/src/App/Application.cpp @@ -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; + +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); +}; diff --git a/tests/src/App/CMakeLists.txt b/tests/src/App/CMakeLists.txt index e159b934e5..b6b1782bdc 100644 --- a/tests/src/App/CMakeLists.txt +++ b/tests/src/App/CMakeLists.txt @@ -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 )