copyright
This commit is contained in:
parent
97d072ae2d
commit
f11af6fb89
@ -1,3 +1,7 @@
|
|||||||
add_executable(cxxopts main.cpp cxxopts.cpp)
|
add_executable(example main.cpp)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
|
||||||
|
|
||||||
|
add_library(cxxopts SHARED cxxopts.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(example cxxopts)
|
||||||
|
|||||||
@ -1,13 +1,41 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jarryd Beck
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#include "cxxopts.hpp"
|
#include "cxxopts.hpp"
|
||||||
|
|
||||||
namespace cxxopts
|
namespace cxxopts
|
||||||
{
|
{
|
||||||
|
|
||||||
std::basic_regex<char> option_matcher
|
namespace
|
||||||
("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)");
|
{
|
||||||
|
|
||||||
std::basic_regex<char> option_specifier
|
std::basic_regex<char> option_matcher
|
||||||
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
|
("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)");
|
||||||
|
|
||||||
|
std::basic_regex<char> option_specifier
|
||||||
|
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
|
||||||
|
}
|
||||||
|
|
||||||
OptionAdder
|
OptionAdder
|
||||||
Options::add_options()
|
Options::add_options()
|
||||||
|
|||||||
@ -1,3 +1,27 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jarryd Beck
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -31,18 +55,16 @@ namespace cxxopts
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool
|
struct value_has_arg
|
||||||
value_has_arg(bool*)
|
|
||||||
{
|
{
|
||||||
return false;
|
static constexpr bool value = true;
|
||||||
}
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <>
|
||||||
bool
|
struct value_has_arg<bool>
|
||||||
value_has_arg(T*)
|
|
||||||
{
|
{
|
||||||
return true;
|
static constexpr bool value = false;
|
||||||
}
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class default_value : public Value
|
class default_value : public Value
|
||||||
@ -68,7 +90,7 @@ namespace cxxopts
|
|||||||
bool
|
bool
|
||||||
has_arg() const
|
has_arg() const
|
||||||
{
|
{
|
||||||
return value_has_arg(m_store);
|
return value_has_arg<T>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const T&
|
const T&
|
||||||
@ -98,13 +120,6 @@ namespace cxxopts
|
|||||||
return std::make_shared<values::default_value<T>>();
|
return std::make_shared<values::default_value<T>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern std::basic_regex<char> option_matcher;
|
|
||||||
|
|
||||||
extern std::basic_regex<char> option_specifier;
|
|
||||||
|
|
||||||
class MessageException
|
|
||||||
;
|
|
||||||
|
|
||||||
class OptionException : public std::exception
|
class OptionException : public std::exception
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
88
src/main.cpp
88
src/main.cpp
@ -1,7 +1,28 @@
|
|||||||
#include <codecvt>
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2014 Jarryd Beck
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <locale>
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "cxxopts.hpp"
|
#include "cxxopts.hpp"
|
||||||
|
|
||||||
@ -10,51 +31,40 @@ int main(int argc, char* argv[])
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
std::match_results<const char*> result;
|
cxxopts::Options options;
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i)
|
options.add_options()
|
||||||
{
|
("a,apple", "an apple")
|
||||||
std::cout << "Argument " << i << std::endl;
|
("b,bob", "Bob")
|
||||||
std::regex_match(argv[i], result, cxxopts::option_matcher);
|
("f,file", "File", cxxopts::value<std::string>())
|
||||||
std::cout << "empty = " << result.empty() << std::endl;
|
;
|
||||||
std::cout << "size = " << result.size() << std::endl;
|
|
||||||
|
|
||||||
std::cout << "matches:" << std::endl;
|
options.parse(argc, argv);
|
||||||
for (int j = 0; j != result.size(); ++j)
|
|
||||||
|
if (options.count("a"))
|
||||||
{
|
{
|
||||||
std::cout << "arg " << j << ": " << result[j] << std::endl;
|
std::cout << "Saw option ‘a’" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
cxxopts::Options options;
|
if (options.count("b"))
|
||||||
|
{
|
||||||
|
std::cout << "Saw option ‘b’" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
options.add_options()
|
if (options.count("f"))
|
||||||
("a,apple", "an apple")
|
{
|
||||||
("b,bob", "Bob")
|
std::cout << "File = " << options["f"].as<std::string>()
|
||||||
("f,file", "File", cxxopts::value<std::string>())
|
<< std::endl;
|
||||||
;
|
}
|
||||||
|
|
||||||
options.parse(argc, argv);
|
if (options.count("help"))
|
||||||
|
{
|
||||||
|
//std::cout << options.print_help();
|
||||||
|
}
|
||||||
|
|
||||||
if (options.count("a"))
|
} catch (const cxxopts::OptionException& e)
|
||||||
{
|
{
|
||||||
std::cout << "Saw option ‘a’" << std::endl;
|
std::cout << "error parsing options: " << e.what() << std::endl;
|
||||||
}
|
|
||||||
|
|
||||||
if (options.count("b"))
|
|
||||||
{
|
|
||||||
std::cout << "Saw option ‘b’" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options.count("f"))
|
|
||||||
{
|
|
||||||
std::cout << "File = " << options["f"].as<std::string>()
|
|
||||||
<< std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (const std::regex_error& e)
|
|
||||||
{
|
|
||||||
std::cout << "regex_error: " << e.what() << std::endl;
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user