mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
Cmd line args parser (#21)
* CommandLineArgumetns parser added * fixed CMakeLists.txt * Fix to linux build * Fix linux build for tests
This commit is contained in:
parent
334aa2c967
commit
1ab3543bd1
@ -35,6 +35,8 @@ add_library(oatpp
|
||||
core/base/memory/ObjectPool.hpp
|
||||
core/base/StrBuffer.cpp
|
||||
core/base/StrBuffer.hpp
|
||||
core/base/CommandLineArguments.cpp
|
||||
core/base/CommandLineArguments.hpp
|
||||
core/collection/FastQueue.cpp
|
||||
core/collection/FastQueue.hpp
|
||||
core/collection/LinkedList.cpp
|
||||
@ -245,6 +247,8 @@ if(OATPP_BUILD_TESTS)
|
||||
test/core/base/memory/PerfTest.hpp
|
||||
test/core/base/RegRuleTest.cpp
|
||||
test/core/base/RegRuleTest.hpp
|
||||
test/core/base/CommandLineArgumentsTest.cpp
|
||||
test/core/base/CommandLineArgumentsTest.hpp
|
||||
test/core/data/mapping/type/TypeTest.cpp
|
||||
test/core/data/mapping/type/TypeTest.hpp
|
||||
test/encoding/UnicodeTest.cpp
|
||||
|
64
core/base/CommandLineArguments.cpp
Normal file
64
core/base/CommandLineArguments.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#include "CommandLineArguments.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace oatpp { namespace base {
|
||||
|
||||
bool CommandLineArguments::Parser::hasArgument(int argc, const char * argv[], const char* argName) {
|
||||
return getArgumentIndex(argc, argv, argName) >= 0;
|
||||
}
|
||||
|
||||
v_int32 CommandLineArguments::Parser::getArgumentIndex(int argc, const char * argv[], const char* argName) {
|
||||
for(v_int32 i = 0; i < argc; i ++) {
|
||||
if(std::strcmp(argName, argv[i]) == 0){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* CommandLineArguments::Parser::getArgumentStartingWith(int argc, const char * argv[], const char* argNamePrefix, const char* defaultValue) {
|
||||
for(v_int32 i = 0; i < argc; i ++) {
|
||||
if(std::strncmp(argNamePrefix, argv[i], std::strlen(argNamePrefix)) == 0){
|
||||
return argv[i];
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
const char* CommandLineArguments::Parser::getNamedArgumentValue(int argc, const char * argv[], const char* argName, const char* defaultValue) {
|
||||
for(v_int32 i = 0; i < argc; i ++) {
|
||||
if(std::strcmp(argName, argv[i]) == 0){
|
||||
if(i + 1 < argc) {
|
||||
return argv[i + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
}}
|
103
core/base/CommandLineArguments.hpp
Normal file
103
core/base/CommandLineArguments.hpp
Normal file
@ -0,0 +1,103 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_base_CommandLineArguments_hpp
|
||||
#define oatpp_base_CommandLineArguments_hpp
|
||||
|
||||
#include "./Environment.hpp"
|
||||
|
||||
namespace oatpp { namespace base {
|
||||
|
||||
class CommandLineArguments {
|
||||
public:
|
||||
|
||||
class Parser {
|
||||
public:
|
||||
|
||||
/**
|
||||
* returns true if getArgumentIndex(argName) >= 0
|
||||
*/
|
||||
static bool hasArgument(int argc, const char * argv[], const char* argName);
|
||||
|
||||
/**
|
||||
* get index of the argument with the name == argName
|
||||
*/
|
||||
static v_int32 getArgumentIndex(int argc, const char * argv[], const char* argName);
|
||||
|
||||
/**
|
||||
* return argument wich starts with the prefix
|
||||
* ex:
|
||||
* for cmd = "-k -c 1000 -n 100 'http://127.0.0.1:8000/'"
|
||||
* getArgumentWhichStartsWith("http") == http://127.0.0.1:8000/
|
||||
* if no argument found defaultValue returned
|
||||
*/
|
||||
static const char* getArgumentStartingWith(int argc, const char * argv[], const char* argNamePrefix, const char* defaultValue = nullptr);
|
||||
|
||||
/**
|
||||
* return value preceded by the argument
|
||||
* ex:
|
||||
* for cmd = "-k -c 1000 -n 100"
|
||||
* getNamedArgumentValue("-c") == "1000"
|
||||
* getNamedArgumentValue("-n") == "100"
|
||||
*/
|
||||
static const char* getNamedArgumentValue(int argc, const char * argv[], const char* argName, const char* defaultValue = nullptr);
|
||||
|
||||
};
|
||||
|
||||
private:
|
||||
int m_argc;
|
||||
const char ** m_argv;
|
||||
public:
|
||||
|
||||
CommandLineArguments()
|
||||
: m_argc(0)
|
||||
, m_argv(nullptr)
|
||||
{}
|
||||
|
||||
CommandLineArguments(int argc, const char * argv[])
|
||||
: m_argc(argc)
|
||||
, m_argv(argv)
|
||||
{}
|
||||
|
||||
bool hasArgument(const char* argName) {
|
||||
return Parser::hasArgument(m_argc, m_argv, argName);
|
||||
}
|
||||
|
||||
v_int32 getArgumentIndex(const char* argName) {
|
||||
return Parser::getArgumentIndex(m_argc, m_argv, argName);
|
||||
}
|
||||
|
||||
const char* getArgumentStartingWith(const char* argNamePrefix, const char* defaultValue = nullptr) {
|
||||
return Parser::getArgumentStartingWith(m_argc, m_argv, argNamePrefix, defaultValue);
|
||||
}
|
||||
|
||||
const char* getNamedArgumentValue(const char* argName, const char* defaultValue = nullptr) {
|
||||
return Parser::getNamedArgumentValue(m_argc, m_argv, argName, defaultValue);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif /* oatpp_base_CommandLineArguments_hpp */
|
@ -10,6 +10,8 @@
|
||||
#include "oatpp/test/core/base/collection/LinkedListTest.hpp"
|
||||
#include "oatpp/test/core/base/memory/MemoryPoolTest.hpp"
|
||||
#include "oatpp/test/core/base/memory/PerfTest.hpp"
|
||||
#include "oatpp/test/core/base/CommandLineArgumentsTest.hpp"
|
||||
#include "oatpp/test/core/base/RegRuleTest.hpp"
|
||||
|
||||
#include "oatpp/core/concurrency/SpinLock.hpp"
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
@ -36,6 +38,8 @@ public:
|
||||
};
|
||||
|
||||
void runTests() {
|
||||
OATPP_RUN_TEST(oatpp::test::base::RegRuleTest);
|
||||
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
|
||||
OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest);
|
||||
OATPP_RUN_TEST(oatpp::test::memory::PerfTest);
|
||||
OATPP_RUN_TEST(oatpp::test::collection::LinkedListTest);
|
||||
|
64
test/core/base/CommandLineArgumentsTest.cpp
Normal file
64
test/core/base/CommandLineArgumentsTest.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#include "CommandLineArgumentsTest.hpp"
|
||||
|
||||
#include "oatpp/core/base/CommandLineArguments.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace oatpp { namespace test { namespace base {
|
||||
|
||||
bool CommandLineArgumentsTest::onRun() {
|
||||
|
||||
/* -k -c 100 -n 500000 "http://127.0.0.1:8000/" */
|
||||
int argc = 6;
|
||||
const char * argv[] = {"-k", "-c", "100", "-n", "500000", "http://127.0.0.1:8000/"};
|
||||
|
||||
oatpp::base::CommandLineArguments args(argc, argv);
|
||||
|
||||
OATPP_ASSERT(args.getArgumentIndex("-k") == 0);
|
||||
OATPP_ASSERT(args.getArgumentIndex("-c") == 1);
|
||||
OATPP_ASSERT(args.getArgumentIndex("100") == 2);
|
||||
OATPP_ASSERT(args.getArgumentIndex("-n") == 3);
|
||||
OATPP_ASSERT(args.getArgumentIndex("500000") == 4);
|
||||
OATPP_ASSERT(args.getArgumentIndex("http://127.0.0.1:8000/") == 5);
|
||||
OATPP_ASSERT(args.getArgumentIndex("not-existing-arg") == -1);
|
||||
|
||||
OATPP_ASSERT(args.hasArgument("-k"));
|
||||
OATPP_ASSERT(args.hasArgument("not-existing-arg") == false);
|
||||
|
||||
OATPP_ASSERT(std::strcmp(args.getArgumentStartingWith("http"), "http://127.0.0.1:8000/") == 0);
|
||||
OATPP_ASSERT(std::strcmp(args.getArgumentStartingWith("tcp", "tcp://default/"), "tcp://default/") == 0);
|
||||
|
||||
OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("-c"), "100") == 0);
|
||||
OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("-c", nullptr), "100") == 0);
|
||||
|
||||
OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("-n"), "500000") == 0);
|
||||
OATPP_ASSERT(std::strcmp(args.getNamedArgumentValue("--non-existing", "default"), "default") == 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}}}
|
42
test/core/base/CommandLineArgumentsTest.hpp
Normal file
42
test/core/base/CommandLineArgumentsTest.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_test_base_CommandLineArgumentsTest_hpp
|
||||
#define oatpp_test_base_CommandLineArgumentsTest_hpp
|
||||
|
||||
#include "oatpp/test/UnitTest.hpp"
|
||||
|
||||
namespace oatpp { namespace test { namespace base {
|
||||
|
||||
class CommandLineArgumentsTest : public UnitTest{
|
||||
public:
|
||||
|
||||
CommandLineArgumentsTest():UnitTest("TEST[base::CommandLineArgumentsTest]"){}
|
||||
bool onRun() override;
|
||||
|
||||
};
|
||||
|
||||
}}}
|
||||
|
||||
#endif /* oatpp_test_core_base_CommandLineArgumentsTest_hpp */
|
Loading…
Reference in New Issue
Block a user