2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <boost/program_options.hpp>
20 #include <folly/Optional.h>
21 #include <gflags/gflags.h>
25 enum class ProgramOptionsStyle {
30 // Add all GFlags to the given options_description.
31 // Use this *instead of* gflags::ParseCommandLineFlags().
33 // in GFLAGS style, the flags are named as per gflags conventions:
34 // names_with_underscores
35 // boolean flags have a "no" prefix
37 // in GNU style, the flags are named as per GNU conventions:
39 // boolean flags have a "no-" prefix
41 // Consider (for example) a boolean flag:
42 // DEFINE_bool(flying_pigs, false, "...");
44 // In GFLAGS style, the corresponding flags are named
48 // In GNU style, the corresponding flags are named
52 // You may not pass arguments to boolean flags, so you must use the
53 // "no" / "no-" prefix to set them to false; "--flying_pigs false"
54 // and "--flying_pigs=false" are not allowed, to prevent ambiguity.
55 boost::program_options::options_description getGFlags(
56 ProgramOptionsStyle style = ProgramOptionsStyle::GNU);
58 // Helper when parsing nested command lines:
60 // program [--common_options...] command [--command_options...] args
62 // The result has "command" set to the first positional argument, if any,
63 // and "rest" set to the remaining options and arguments. Note that any
64 // unrecognized flags must appear after the command name.
66 // You may pass "rest" to parseNestedCommandLine again, etc.
67 struct NestedCommandLineParseResult {
68 NestedCommandLineParseResult() { }
70 boost::program_options::parsed_options options {nullptr};
72 Optional<std::string> command;
73 std::vector<std::string> rest;
76 NestedCommandLineParseResult parseNestedCommandLine(
77 int argc, const char* const argv[],
78 const boost::program_options::options_description& desc);
80 NestedCommandLineParseResult parseNestedCommandLine(
81 const std::vector<std::string>& cmdline,
82 const boost::program_options::options_description& desc);