proxygen
NestedCommandLineApp.h
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 #pragma once
17 
18 #include <functional>
19 #include <set>
20 #include <stdexcept>
21 
22 #include <folly/CPortability.h>
23 #include <folly/String.h>
25 
26 namespace folly {
27 
35 class FOLLY_EXPORT ProgramExit : public std::runtime_error {
36  public:
37  explicit ProgramExit(int status, const std::string& msg = std::string());
38  int status() const {
39  return status_;
40  }
41 
42  private:
43  int status_;
44 };
45 
52  public:
53  typedef std::function<void(
54  const std::string& command,
55  const boost::program_options::variables_map& options,
56  const std::vector<std::string>& args)>
58 
59  typedef std::function<void(
60  const boost::program_options::variables_map& options,
61  const std::vector<std::string>&)>
63 
64  static constexpr StringPiece const kHelpCommand = "help";
65  static constexpr StringPiece const kVersionCommand = "version";
76  explicit NestedCommandLineApp(
77  std::string programName = std::string(),
79  std::string programHeading = std::string(),
80  std::string programHelpFooter = std::string(),
81  InitFunction initFunction = InitFunction());
82 
87  globalOptions_.add(getGFlags(style));
88  }
89 
93  boost::program_options::options_description& globalOptions() {
94  return globalOptions_;
95  }
96 
110  boost::program_options::options_description& addCommand(
112  std::string argStr,
113  std::string shortHelp,
114  std::string fullHelp,
115  Command command);
116 
121  void addAlias(std::string newName, std::string oldName);
122 
133  int run(int argc, const char* const argv[]);
134  int run(const std::vector<std::string>& args);
135 
139  bool isBuiltinCommand(const std::string& name) const;
140 
141  private:
142  void doRun(const std::vector<std::string>& args);
143  const std::string& resolveAlias(const std::string& name) const;
144 
145  struct CommandInfo {
150  boost::program_options::options_description options;
151  };
152 
153  const std::pair<const std::string, CommandInfo>& findCommand(
154  const std::string& name) const;
155 
156  void displayHelp(
157  const boost::program_options::variables_map& options,
158  const std::vector<std::string>& args) const;
159 
160  void displayVersion() const;
161 
167  boost::program_options::options_description globalOptions_;
168  std::map<std::string, CommandInfo> commands_;
169  std::map<std::string, std::string> aliases_;
170  std::set<folly::StringPiece> builtinCommands_;
171 };
172 
173 } // namespace folly
std::function< void(const std::string &command, const boost::program_options::variables_map &options, const std::vector< std::string > &args)> InitFunction
boost::program_options::options_description options
std::map< std::string, std::string > aliases_
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
#define FOLLY_EXPORT
Definition: CPortability.h:133
boost::program_options::options_description & globalOptions()
boost::program_options::options_description globalOptions_
ProtocolVersion version
const char * name
Definition: http_parser.c:437
char ** argv
static void run(EventBaseManager *ebm, EventBase *eb, folly::Baton<> *stop, const StringPiece &name)
std::function< void(const boost::program_options::variables_map &options, const std::vector< std::string > &)> Command
ProgramOptionsStyle
void addGFlags(ProgramOptionsStyle style=ProgramOptionsStyle::GNU)
std::map< std::string, CommandInfo > commands_
const char * string
Definition: Conv.cpp:212
std::set< folly::StringPiece > builtinCommands_
po::options_description getGFlags(ProgramOptionsStyle style)