ProgramOptions.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2015-present 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. #pragma once
  17. #include <boost/program_options.hpp>
  18. #include <folly/Optional.h>
  19. #include <folly/portability/GFlags.h>
  20. namespace folly {
  21. enum class ProgramOptionsStyle {
  22. GFLAGS,
  23. GNU,
  24. };
  25. // Add all GFlags to the given options_description.
  26. // Use this *instead of* gflags::ParseCommandLineFlags().
  27. //
  28. // in GFLAGS style, the flags are named as per gflags conventions:
  29. // names_with_underscores
  30. // boolean flags have a "no" prefix
  31. //
  32. // in GNU style, the flags are named as per GNU conventions:
  33. // names-with-dashes
  34. // boolean flags have a "no-" prefix
  35. //
  36. // Consider (for example) a boolean flag:
  37. // DEFINE_bool(flying_pigs, false, "...");
  38. //
  39. // In GFLAGS style, the corresponding flags are named
  40. // flying_pigs
  41. // noflying_pigs
  42. //
  43. // In GNU style, the corresponding flags are named
  44. // flying-pigs
  45. // no-flying-pigs
  46. //
  47. // You may not pass arguments to boolean flags, so you must use the
  48. // "no" / "no-" prefix to set them to false; "--flying_pigs false"
  49. // and "--flying_pigs=false" are not allowed, to prevent ambiguity.
  50. boost::program_options::options_description getGFlags(
  51. ProgramOptionsStyle style = ProgramOptionsStyle::GNU);
  52. // Helper when parsing nested command lines:
  53. //
  54. // program [--common_options...] command [--command_options...] args
  55. //
  56. // The result has "command" set to the first positional argument, if any,
  57. // and "rest" set to the remaining options and arguments. Note that any
  58. // unrecognized flags must appear after the command name.
  59. //
  60. // You may pass "rest" to parseNestedCommandLine again, etc.
  61. struct NestedCommandLineParseResult {
  62. NestedCommandLineParseResult() {}
  63. boost::program_options::parsed_options options{nullptr};
  64. Optional<std::string> command;
  65. std::vector<std::string> rest;
  66. };
  67. NestedCommandLineParseResult parseNestedCommandLine(
  68. int argc,
  69. const char* const argv[],
  70. const boost::program_options::options_description& desc);
  71. NestedCommandLineParseResult parseNestedCommandLine(
  72. const std::vector<std::string>& cmdline,
  73. const boost::program_options::options_description& desc);
  74. } // namespace folly