diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/program_options/example/custom_syntax.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/program_options/example/custom_syntax.cpp')
-rw-r--r-- | src/boost/libs/program_options/example/custom_syntax.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/boost/libs/program_options/example/custom_syntax.cpp b/src/boost/libs/program_options/example/custom_syntax.cpp new file mode 100644 index 00000000..829087d9 --- /dev/null +++ b/src/boost/libs/program_options/example/custom_syntax.cpp @@ -0,0 +1,63 @@ +// Copyright Vladimir Prus 2002-2004. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) + +/** This example shows how to support custom options syntax. + + It's possible to install 'custom_parser'. It will be invoked on all command + line tokens and can return name/value pair, or nothing. If it returns + nothing, usual processing will be done. +*/ + + +#include <boost/program_options/options_description.hpp> +#include <boost/program_options/parsers.hpp> +#include <boost/program_options/variables_map.hpp> + +using namespace boost::program_options; + +#include <iostream> +using namespace std; + +/* This custom option parse function recognize gcc-style + option "-fbar" / "-fno-bar". +*/ +pair<string, string> reg_foo(const string& s) +{ + if (s.find("-f") == 0) { + if (s.substr(2, 3) == "no-") + return make_pair(s.substr(5), string("false")); + else + return make_pair(s.substr(2), string("true")); + } else { + return make_pair(string(), string()); + } +} + +int main(int ac, char* av[]) +{ + try { + options_description desc("Allowed options"); + desc.add_options() + ("help", "produce a help message") + ("foo", value<string>(), "just an option") + ; + + variables_map vm; + store(command_line_parser(ac, av).options(desc).extra_parser(reg_foo) + .run(), vm); + + if (vm.count("help")) { + cout << desc; + cout << "\nIn addition -ffoo and -fno-foo syntax are recognized.\n"; + } + if (vm.count("foo")) { + cout << "foo value with the value of " + << vm["foo"].as<string>() << "\n"; + } + } + catch(exception& e) { + cout << e.what() << "\n"; + } +} |