folly: replace old-style header guards with "pragma once"
[folly.git] / folly / experimental / ProgramOptions.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <boost/program_options.hpp>
20 #include <folly/Optional.h>
21 #include <gflags/gflags.h>
22
23 namespace folly {
24
25 enum class ProgramOptionsStyle {
26   GFLAGS,
27   GNU
28 };
29
30 // Add all GFlags to the given options_description.
31 // Use this *instead of* gflags::ParseCommandLineFlags().
32 //
33 // in GFLAGS style, the flags are named as per gflags conventions:
34 //   names_with_underscores
35 //   boolean flags have a "no" prefix
36 //
37 // in GNU style, the flags are named as per GNU conventions:
38 //   names-with-dashes
39 //   boolean flags have a "no-" prefix
40 //
41 // Consider (for example) a boolean flag:
42 //   DEFINE_bool(flying_pigs, false, "...");
43 //
44 // In GFLAGS style, the corresponding flags are named
45 //   flying_pigs
46 //   noflying_pigs
47 //
48 // In GNU style, the corresponding flags are named
49 //   flying-pigs
50 //   no-flying-pigs
51 //
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);
57
58 // Helper when parsing nested command lines:
59 //
60 // program [--common_options...] command [--command_options...] args
61 //
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.
65 //
66 // You may pass "rest" to parseNestedCommandLine again, etc.
67 struct NestedCommandLineParseResult {
68   NestedCommandLineParseResult() { }
69
70   boost::program_options::parsed_options options {nullptr};
71
72   Optional<std::string> command;
73   std::vector<std::string> rest;
74 };
75
76 NestedCommandLineParseResult parseNestedCommandLine(
77     int argc, const char* const argv[],
78     const boost::program_options::options_description& desc);
79
80 NestedCommandLineParseResult parseNestedCommandLine(
81     const std::vector<std::string>& cmdline,
82     const boost::program_options::options_description& desc);
83
84 }  // namespaces