NestedCommandLineAppTestHelper.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #include <folly/experimental/NestedCommandLineApp.h>
  17. #include <folly/portability/GFlags.h>
  18. DEFINE_int32(global_foo, 42, "Global foo");
  19. namespace po = ::boost::program_options;
  20. namespace {
  21. void init(
  22. const std::string& cmd,
  23. const po::variables_map& /* options */,
  24. const std::vector<std::string>& /* args */) {
  25. printf("running %s\n", cmd.c_str());
  26. }
  27. void foo(
  28. const po::variables_map& options,
  29. const std::vector<std::string>& args) {
  30. printf("foo global-foo %d\n", options["global-foo"].as<int32_t>());
  31. printf("foo local-foo %d\n", options["local-foo"].as<int32_t>());
  32. for (auto& arg : args) {
  33. printf("foo arg %s\n", arg.c_str());
  34. }
  35. }
  36. } // namespace
  37. int main(int argc, char* argv[]) {
  38. folly::NestedCommandLineApp app("", "0.1", "", "", init);
  39. app.addGFlags();
  40. // clang-format off
  41. app.addCommand("foo", "[args...]", "Do some foo", "Does foo", foo)
  42. .add_options()
  43. ("local-foo", po::value<int32_t>()->default_value(42), "Local foo");
  44. // clang-format on
  45. app.addAlias("bar", "foo");
  46. app.addAlias("baz", "bar");
  47. return app.run(argc, argv);
  48. }