2014-10-02 13:11:50 +02:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2014-09-10 08:47:22 +02:00
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
2014-10-27 05:20:12 +01:00
|
|
|
|
#define CXXOPTS_USE_UNICODE
|
|
|
|
|
|
|
2014-09-10 08:47:22 +02:00
|
|
|
|
#include "cxxopts.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2014-10-17 02:59:40 +02:00
|
|
|
|
cxxopts::Options options(argv[0], " - example command line options");
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
2014-10-14 07:59:25 +02:00
|
|
|
|
bool apple = false;
|
|
|
|
|
|
|
2014-10-02 13:11:50 +02:00
|
|
|
|
options.add_options()
|
2014-10-14 07:59:25 +02:00
|
|
|
|
("a,apple", "an apple", cxxopts::value<bool>(apple))
|
2014-10-02 13:11:50 +02:00
|
|
|
|
("b,bob", "Bob")
|
2014-10-09 05:09:02 +02:00
|
|
|
|
("f,file", "File", cxxopts::value<std::vector<std::string>>())
|
2014-10-13 05:30:56 +02:00
|
|
|
|
("positional",
|
|
|
|
|
|
"Positional arguments: these are the arguments that are entered "
|
|
|
|
|
|
"without an option", cxxopts::value<std::string>())
|
2014-10-13 06:58:17 +02:00
|
|
|
|
("long-description",
|
|
|
|
|
|
"thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")
|
2014-10-10 09:03:18 +02:00
|
|
|
|
("help", "Print help")
|
2014-10-13 11:52:06 +02:00
|
|
|
|
("int", "An integer", cxxopts::value<int>())
|
2014-10-14 01:39:03 +02:00
|
|
|
|
("option_that_is_too_long_for_the_help", "A very long option")
|
2014-10-27 06:47:21 +01:00
|
|
|
|
("unicode", u8"A help option with non-ascii: à. Here the length of the"
|
|
|
|
|
|
" string should be correct")
|
2014-10-02 13:11:50 +02:00
|
|
|
|
;
|
|
|
|
|
|
|
2014-10-14 07:59:54 +02:00
|
|
|
|
options.add_options("Group")
|
|
|
|
|
|
("c,compile", "compile")
|
|
|
|
|
|
("d,drop", "drop", cxxopts::value<std::vector<std::string>>());
|
|
|
|
|
|
|
2014-10-09 02:08:36 +02:00
|
|
|
|
options.parse_positional("positional");
|
|
|
|
|
|
|
2014-10-02 13:11:50 +02:00
|
|
|
|
options.parse(argc, argv);
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
2014-10-14 07:59:54 +02:00
|
|
|
|
if (options.count("help"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << options.help({"", "Group"}) << std::endl;
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-14 07:59:25 +02:00
|
|
|
|
if (apple)
|
2014-09-10 08:47:22 +02:00
|
|
|
|
{
|
2014-10-02 13:11:50 +02:00
|
|
|
|
std::cout << "Saw option ‘a’" << std::endl;
|
2014-09-10 08:47:22 +02:00
|
|
|
|
}
|
2014-09-27 14:04:53 +02:00
|
|
|
|
|
2014-10-02 13:11:50 +02:00
|
|
|
|
if (options.count("b"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "Saw option ‘b’" << std::endl;
|
|
|
|
|
|
}
|
2014-10-01 09:52:34 +02:00
|
|
|
|
|
2014-10-02 13:11:50 +02:00
|
|
|
|
if (options.count("f"))
|
|
|
|
|
|
{
|
2014-10-09 05:09:02 +02:00
|
|
|
|
auto& ff = options["f"].as<std::vector<std::string>>();
|
|
|
|
|
|
std::cout << "Files" << std::endl;
|
|
|
|
|
|
for (const auto& f : ff)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << f << std::endl;
|
|
|
|
|
|
}
|
2014-10-02 13:11:50 +02:00
|
|
|
|
}
|
2014-10-01 09:52:34 +02:00
|
|
|
|
|
2014-10-09 02:08:36 +02:00
|
|
|
|
if (options.count("positional"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "Positional = " << options["positional"].as<std::string>()
|
|
|
|
|
|
<< std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-13 11:52:06 +02:00
|
|
|
|
if (options.count("int"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "int = " << options["int"].as<int>() << std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-13 00:20:04 +02:00
|
|
|
|
std::cout << "Arguments remain = " << argc << std::endl;
|
2014-10-09 02:16:34 +02:00
|
|
|
|
|
2014-10-02 13:11:50 +02:00
|
|
|
|
} catch (const cxxopts::OptionException& e)
|
2014-09-10 08:47:22 +02:00
|
|
|
|
{
|
2014-10-02 13:11:50 +02:00
|
|
|
|
std::cout << "error parsing options: " << e.what() << std::endl;
|
2014-09-10 08:47:22 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|