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.
17 #ifndef FOLLY_PROGRAMOPTIONS_H_
18 #define FOLLY_PROGRAMOPTIONS_H_
20 #include <boost/program_options.hpp>
21 #include <folly/Optional.h>
22 #include <gflags/gflags.h>
26 enum class ProgramOptionsStyle {
31 // Add all GFlags to the given options_description.
32 // Use this *instead of* gflags::ParseCommandLineFlags().
34 // in GFLAGS style, the flags are named as per gflags conventions:
35 // names_with_underscores
36 // boolean flags have a "no" prefix
38 // in GNU style, the flags are named as per GNU conventions:
40 // boolean flags have a "no-" prefix
42 // Consider (for example) a boolean flag:
43 // DEFINE_bool(flying_pigs, false, "...");
45 // In GFLAGS style, the corresponding flags are named
49 // In GNU style, the corresponding flags are named
53 // You may not pass arguments to boolean flags, so you must use the
54 // "no" / "no-" prefix to set them to false; "--flying_pigs false"
55 // and "--flying_pigs=false" are not allowed, to prevent ambiguity.
56 boost::program_options::options_description getGFlags(
57 ProgramOptionsStyle style = ProgramOptionsStyle::GNU);
59 // Helper when parsing nested command lines:
61 // program [--common_options...] command [--command_options...] args
63 // The result has "command" set to the first positional argument, if any,
64 // and "rest" set to the remaining options and arguments. Note that any
65 // unrecognized flags must appear after the command name.
67 // You may pass "rest" to parseNestedCommandLine again, etc.
68 struct NestedCommandLineParseResult {
69 NestedCommandLineParseResult() { }
71 boost::program_options::parsed_options options {nullptr};
73 Optional<std::string> command;
74 std::vector<std::string> rest;
77 NestedCommandLineParseResult parseNestedCommandLine(
78 int argc, const char* const argv[],
79 const boost::program_options::options_description& desc);
81 NestedCommandLineParseResult parseNestedCommandLine(
82 const std::vector<std::string>& cmdline,
83 const boost::program_options::options_description& desc);
87 #endif /* FOLLY_PROGRAMOPTIONS_H_ */