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.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
2022-07-11 09:02:22 +02:00
|
|
|
|
#include "cxxopts.hpp"
|
2014-10-02 13:11:50 +02:00
|
|
|
|
|
2014-09-10 08:47:22 +02:00
|
|
|
|
#include <iostream>
|
2022-07-11 09:02:22 +02:00
|
|
|
|
#include <memory>
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
2022-07-11 09:02:22 +02:00
|
|
|
|
int
|
2020-08-11 00:01:29 +02:00
|
|
|
|
parse(int argc, const char* argv[])
|
2014-09-10 08:47:22 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-07-11 09:02:22 +02:00
|
|
|
|
std::unique_ptr<cxxopts::Options> allocated(new cxxopts::Options(argv[0], " - example command line options"));
|
|
|
|
|
|
auto& options = *allocated;
|
2017-11-13 08:47:12 +01:00
|
|
|
|
options
|
|
|
|
|
|
.positional_help("[optional args]")
|
|
|
|
|
|
.show_positional_help();
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
2014-10-14 07:59:25 +02:00
|
|
|
|
bool apple = false;
|
|
|
|
|
|
|
2018-04-12 00:15:07 +02:00
|
|
|
|
options
|
2021-02-14 22:33:56 +01:00
|
|
|
|
.set_width(70)
|
|
|
|
|
|
.set_tab_expansion()
|
2018-04-12 00:15:07 +02:00
|
|
|
|
.allow_unrecognised_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")
|
2019-08-23 00:26:16 +02:00
|
|
|
|
("char", "A character", cxxopts::value<char>())
|
2018-03-07 22:53:26 +01:00
|
|
|
|
("t,true", "True", cxxopts::value<bool>()->default_value("true"))
|
2017-07-16 07:11:03 +02:00
|
|
|
|
("f, file", "File", cxxopts::value<std::vector<std::string>>(), "FILE")
|
2015-09-25 08:49:06 +02:00
|
|
|
|
("i,input", "Input", cxxopts::value<std::string>())
|
2014-10-26 22:05:24 +01:00
|
|
|
|
("o,output", "Output file", cxxopts::value<std::string>()
|
2014-10-29 16:59:57 +01:00
|
|
|
|
->default_value("a.out")->implicit_value("b.def"), "BIN")
|
2020-06-04 05:21:51 +02:00
|
|
|
|
("x", "A short-only option", cxxopts::value<std::string>())
|
2016-01-22 07:44:00 +01:00
|
|
|
|
("positional",
|
2014-10-13 05:30:56 +02:00
|
|
|
|
"Positional arguments: these are the arguments that are entered "
|
2015-09-25 08:49:06 +02:00
|
|
|
|
"without an option", cxxopts::value<std::vector<std::string>>())
|
2014-10-13 06:58:17 +02:00
|
|
|
|
("long-description",
|
|
|
|
|
|
"thisisareallylongwordthattakesupthewholelineandcannotbebrokenataspace")
|
2014-10-10 09:03:18 +02:00
|
|
|
|
("help", "Print help")
|
2021-02-14 22:33:56 +01:00
|
|
|
|
("tab-expansion", "Tab\texpansion")
|
2014-10-29 02:35:05 +01:00
|
|
|
|
("int", "An integer", cxxopts::value<int>(), "N")
|
2017-08-16 09:55:06 +02:00
|
|
|
|
("float", "A floating point number", cxxopts::value<float>())
|
2019-06-18 00:14:18 +02:00
|
|
|
|
("vector", "A list of doubles", cxxopts::value<std::vector<double>>())
|
2014-10-14 01:39:03 +02:00
|
|
|
|
("option_that_is_too_long_for_the_help", "A very long option")
|
2021-11-09 08:49:58 +01:00
|
|
|
|
("l,list", "List all parsed arguments (including default values)")
|
|
|
|
|
|
("range", "Use range-for to list arguments")
|
2014-10-27 12:06:24 +01:00
|
|
|
|
#ifdef CXXOPTS_USE_UNICODE
|
|
|
|
|
|
("unicode", u8"A help option with non-ascii: à. Here the size of the"
|
2014-10-27 06:47:21 +01:00
|
|
|
|
" string should be correct")
|
2014-10-27 12:06:24 +01:00
|
|
|
|
#endif
|
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>>());
|
|
|
|
|
|
|
2015-09-25 08:49:06 +02:00
|
|
|
|
options.parse_positional({"input", "output", "positional"});
|
2014-10-09 02:08:36 +02:00
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
auto result = options.parse(argc, argv);
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("help"))
|
2014-10-14 07:59:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
std::cout << options.help({"", "Group"}) << std::endl;
|
2022-07-11 09:02:22 +02:00
|
|
|
|
return true;
|
2014-10-14 07:59:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-09 08:49:58 +01:00
|
|
|
|
if(result.count("list"))
|
|
|
|
|
|
{
|
|
|
|
|
|
if(result.count("range"))
|
|
|
|
|
|
{
|
|
|
|
|
|
for(const auto &kv: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << kv.key() << " = " << kv.value() << std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << result.arguments_string() << std::endl;
|
|
|
|
|
|
}
|
2022-07-11 09:02:22 +02:00
|
|
|
|
return true;
|
2021-11-09 08:49:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-10-14 07:59:25 +02:00
|
|
|
|
if (apple)
|
2014-09-10 08:47:22 +02:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
std::cout << "Saw option ‘a’ " << result.count("a") << " times " <<
|
2016-01-22 07:44:00 +01:00
|
|
|
|
std::endl;
|
2014-09-10 08:47:22 +02:00
|
|
|
|
}
|
2014-09-27 14:04:53 +02:00
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("b"))
|
2014-10-02 13:11:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
std::cout << "Saw option ‘b’" << std::endl;
|
|
|
|
|
|
}
|
2014-10-01 09:52:34 +02:00
|
|
|
|
|
2019-08-23 00:26:16 +02:00
|
|
|
|
if (result.count("char"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "Saw a character ‘" << result["char"].as<char>() << "’" << std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("f"))
|
2014-10-02 13:11:50 +02:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
auto& ff = result["f"].as<std::vector<std::string>>();
|
2014-10-09 05:09:02 +02:00
|
|
|
|
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
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("input"))
|
2014-10-09 02:08:36 +02:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
std::cout << "Input = " << result["input"].as<std::string>()
|
2014-10-09 02:08:36 +02:00
|
|
|
|
<< std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("output"))
|
2014-10-26 21:01:18 +01:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
std::cout << "Output = " << result["output"].as<std::string>()
|
2014-10-26 21:01:18 +01:00
|
|
|
|
<< std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("positional"))
|
2015-09-25 08:49:06 +02:00
|
|
|
|
{
|
|
|
|
|
|
std::cout << "Positional = {";
|
2017-10-18 09:27:13 +02:00
|
|
|
|
auto& v = result["positional"].as<std::vector<std::string>>();
|
2015-09-25 08:49:06 +02:00
|
|
|
|
for (const auto& s : v) {
|
|
|
|
|
|
std::cout << s << ", ";
|
|
|
|
|
|
}
|
|
|
|
|
|
std::cout << "}" << std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("int"))
|
2014-10-13 11:52:06 +02:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
std::cout << "int = " << result["int"].as<int>() << std::endl;
|
2014-10-13 11:52:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-18 09:27:13 +02:00
|
|
|
|
if (result.count("float"))
|
2017-08-16 09:55:06 +02:00
|
|
|
|
{
|
2017-10-18 09:27:13 +02:00
|
|
|
|
std::cout << "float = " << result["float"].as<float>() << std::endl;
|
2017-08-16 09:55:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-18 00:14:18 +02:00
|
|
|
|
if (result.count("vector"))
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "vector = ";
|
|
|
|
|
|
const auto values = result["vector"].as<std::vector<double>>();
|
|
|
|
|
|
for (const auto& v : values) {
|
|
|
|
|
|
std::cout << v << ", ";
|
|
|
|
|
|
}
|
|
|
|
|
|
std::cout << 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
|
|
|
|
|
2020-01-16 22:03:57 +01:00
|
|
|
|
auto arguments = result.arguments();
|
|
|
|
|
|
std::cout << "Saw " << arguments.size() << " arguments" << std::endl;
|
2022-05-02 22:21:12 +02:00
|
|
|
|
|
|
|
|
|
|
std::cout << "Unmatched options: ";
|
|
|
|
|
|
for (const auto& option: result.unmatched())
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout << "'" << option << "' ";
|
|
|
|
|
|
}
|
|
|
|
|
|
std::cout << std::endl;
|
2020-01-16 22:03:57 +01: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;
|
2022-07-11 09:02:22 +02:00
|
|
|
|
return false;
|
2014-09-10 08:47:22 +02:00
|
|
|
|
}
|
2022-07-11 09:02:22 +02:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2018-11-08 08:17:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-11 00:01:29 +02:00
|
|
|
|
int main(int argc, const char* argv[])
|
2018-11-08 08:17:33 +01:00
|
|
|
|
{
|
2022-07-11 09:02:22 +02:00
|
|
|
|
if (!parse(argc, argv))
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2014-09-10 08:47:22 +02:00
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|