1 /* 2 * Copyright (c) Facebook, Inc. and its affiliates. 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 21 #include <folly/Optional.h> 22 #include <folly/portability/GFlags.h> 23 24 namespace folly { 25 26 enum class ProgramOptionsStyle { 27 GFLAGS, 28 GNU, 29 }; 30 31 // Add all GFlags to the given options_description. 32 // Use this *instead of* gflags::ParseCommandLineFlags(). 33 // 34 // in GFLAGS style, the flags are named as per gflags conventions: 35 // names_with_underscores 36 // boolean flags have a "no" prefix 37 // 38 // in GNU style, the flags are named as per GNU conventions: 39 // names-with-dashes 40 // boolean flags have a "no-" prefix 41 // 42 // Consider (for example) a boolean flag: 43 // DEFINE_bool(flying_pigs, false, "..."); 44 // 45 // In GFLAGS style, the corresponding flags are named 46 // flying_pigs 47 // noflying_pigs 48 // 49 // In GNU style, the corresponding flags are named 50 // flying-pigs 51 // no-flying-pigs 52 // 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); 58 59 // Helper when parsing nested command lines: 60 // 61 // program [--common_options...] command [--command_options...] args 62 // 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. 66 // 67 // You may pass "rest" to parseNestedCommandLine again, etc. 68 struct NestedCommandLineParseResult { NestedCommandLineParseResultNestedCommandLineParseResult69 NestedCommandLineParseResult() {} 70 71 boost::program_options::parsed_options options{nullptr}; 72 73 Optional<std::string> command; 74 std::vector<std::string> rest; 75 }; 76 77 NestedCommandLineParseResult parseNestedCommandLine( 78 int argc, 79 const char* const argv[], 80 const boost::program_options::options_description& desc, 81 boost::program_options::command_line_style::style_t style = 82 boost::program_options::command_line_style::default_style); 83 84 NestedCommandLineParseResult parseNestedCommandLine( 85 const std::vector<std::string>& cmdline, 86 const boost::program_options::options_description& desc, 87 boost::program_options::command_line_style::style_t style = 88 boost::program_options::command_line_style::default_style); 89 90 } // namespace folly 91