2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/experimental/NestedCommandLineApp.h>
18 #include <gflags/gflags.h>
20 DEFINE_int32(global_foo, 42, "Global foo");
22 namespace po = ::boost::program_options;
26 void init(const std::string& cmd,
27 const po::variables_map& options,
28 const std::vector<std::string>& args) {
29 printf("running %s\n", cmd.c_str());
32 void foo(const po::variables_map& options,
33 const std::vector<std::string>& args) {
34 printf("foo global-foo %d\n", options["global-foo"].as<int32_t>());
35 printf("foo local-foo %d\n", options["local-foo"].as<int32_t>());
36 for (auto& arg : args) {
37 printf("foo arg %s\n", arg.c_str());
43 int main(int argc, char *argv[]) {
44 folly::NestedCommandLineApp app("", "0.1", init);
46 app.addCommand("foo", "[args...]", "Do some foo", "Does foo", foo)
48 ("local-foo", po::value<int32_t>()->default_value(42), "Local foo");
49 app.addAlias("bar", "foo");
50 app.addAlias("baz", "bar");
51 return app.run(argc, argv);