copyright

This commit is contained in:
Jarryd Beck 2014-10-02 21:11:50 +10:00
parent 97d072ae2d
commit f11af6fb89
4 changed files with 118 additions and 61 deletions

View File

@ -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")
add_library(cxxopts SHARED cxxopts.cpp)
target_link_libraries(example cxxopts)

View File

@ -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"
namespace cxxopts
{
std::basic_regex<char> option_matcher
("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)");
namespace
{
std::basic_regex<char> option_specifier
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
std::basic_regex<char> option_matcher
("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)");
std::basic_regex<char> option_specifier
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
}
OptionAdder
Options::add_options()

View File

@ -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 <set>
#include <map>
@ -31,18 +55,16 @@ namespace cxxopts
}
template <typename T>
bool
value_has_arg(bool*)
struct value_has_arg
{
return false;
}
static constexpr bool value = true;
};
template <typename T>
bool
value_has_arg(T*)
template <>
struct value_has_arg<bool>
{
return true;
}
static constexpr bool value = false;
};
template <typename T>
class default_value : public Value
@ -68,7 +90,7 @@ namespace cxxopts
bool
has_arg() const
{
return value_has_arg(m_store);
return value_has_arg<T>::value;
}
const T&
@ -98,13 +120,6 @@ namespace cxxopts
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
{
public:

View File

@ -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 <locale>
#include <sstream>
#include "cxxopts.hpp"
@ -10,51 +31,40 @@ int main(int argc, char* argv[])
try
{
std::match_results<const char*> result;
cxxopts::Options options;
for (int i = 1; i < argc; ++i)
{
std::cout << "Argument " << i << std::endl;
std::regex_match(argv[i], result, cxxopts::option_matcher);
std::cout << "empty = " << result.empty() << std::endl;
std::cout << "size = " << result.size() << std::endl;
options.add_options()
("a,apple", "an apple")
("b,bob", "Bob")
("f,file", "File", cxxopts::value<std::string>())
;
std::cout << "matches:" << std::endl;
for (int j = 0; j != result.size(); ++j)
options.parse(argc, argv);
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()
("a,apple", "an apple")
("b,bob", "Bob")
("f,file", "File", cxxopts::value<std::string>())
;
if (options.count("f"))
{
std::cout << "File = " << options["f"].as<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;
}
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;
std::cout << "error parsing options: " << e.what() << std::endl;
exit(1);
}