proxygen
NestedCommandLineAppTestHelper.cpp
Go to the documentation of this file.
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 
19 
20 DEFINE_int32(global_foo, 42, "Global foo");
21 
22 namespace po = ::boost::program_options;
23 
24 namespace {
25 
26 void init(
27  const std::string& cmd,
28  const po::variables_map& /* options */,
29  const std::vector<std::string>& /* args */) {
30  printf("running %s\n", cmd.c_str());
31 }
32 
33 void foo(
34  const po::variables_map& options,
35  const std::vector<std::string>& args) {
36  printf("foo global-foo %d\n", options["global-foo"].as<int32_t>());
37  printf("foo local-foo %d\n", options["local-foo"].as<int32_t>());
38  for (auto& arg : args) {
39  printf("foo arg %s\n", arg.c_str());
40  }
41 }
42 
43 } // namespace
44 
45 int main(int argc, char* argv[]) {
46  folly::NestedCommandLineApp app("", "0.1", "", "", init);
47  app.addGFlags();
48  // clang-format off
49  app.addCommand("foo", "[args...]", "Do some foo", "Does foo", foo)
50  .add_options()
51  ("local-foo", po::value<int32_t>()->default_value(42), "Local foo");
52  // clang-format on
53  app.addAlias("bar", "foo");
54  app.addAlias("baz", "bar");
55  return app.run(argc, argv);
56 }
void addAlias(std::string newName, std::string oldName)
program_options
Definition: CMakeCache.txt:862
int main(int argc, char *argv[])
void init()
boost::program_options::options_description & addCommand(std::string name, std::string argStr, std::string shortHelp, std::string fullHelp, Command command)
char ** argv
DEFINE_int32(global_foo, 42,"Global foo")
cmd
Definition: gtest-cfgcmd.txt:1
void addGFlags(ProgramOptionsStyle style=ProgramOptionsStyle::GNU)
const char * string
Definition: Conv.cpp:212
int run(int argc, const char *const argv[])