main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "available-commands.hpp"
27 #include "help.hpp"
28 #include "core/version.hpp"
29 
30 #include <iostream>
31 
32 namespace nfd {
33 namespace tools {
34 namespace nfdc {
35 
36 static int
37 main(int argc, char** argv)
38 {
39  std::vector<std::string> args(argv + 1, argv + argc);
40 
41  CommandParser parser;
42  registerCommands(parser);
43 
44  if (args.empty()) {
45  helpList(std::cout, parser);
46  return 0;
47  }
48 
49  if (args[0] == "-V" || args[0] == "--version") {
50  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
51  return 0;
52  }
53 
54  std::string noun, verb;
56  ExecuteCommand execute;
57  try {
58  std::tie(noun, verb, ca, execute) = parser.parse(args, ParseMode::ONE_SHOT);
59  }
60  catch (const std::invalid_argument& e) {
61  int ret = help(std::cout, parser, std::move(args));
62  if (ret == 2)
63  std::cerr << e.what() << std::endl;
64  return ret;
65  }
66 
67  try {
68  Face face;
69  KeyChain keyChain;
70  Controller controller(face, keyChain);
71  ExecuteContext ctx{noun, verb, ca, 0, std::cout, std::cerr, face, keyChain, controller};
72  execute(ctx);
73  return ctx.exitCode;
74  }
75  catch (const std::exception& e) {
76  std::cerr << e.what() << std::endl;
77  return 1;
78  }
79 }
80 
81 } // namespace nfdc
82 } // namespace tools
83 } // namespace nfd
84 
85 int
86 main(int argc, char** argv)
87 {
88  return nfd::tools::nfdc::main(argc, argv);
89 }
static int main(int argc, char **argv)
Definition: main.cpp:37
context for command execution
int help(std::ostream &os, const CommandParser &parser, std::vector< std::string > args)
tries to help the user, if requested on the command line
Definition: help.cpp:80
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information, if NFD is build from specific git commit...
std::tuple< std::string, std::string, CommandArguments, ExecuteCommand > parse(const std::vector< std::string > &tokens, ParseMode mode) const
parse a command line
contains named command arguments
void helpList(std::ostream &os, const CommandParser &parser, ParseMode mode, const std::string &noun)
writes the list of available commands to a stream
Definition: help.cpp:44
void registerCommands(CommandParser &parser)
std::function< void(ExecuteContext &ctx)> ExecuteCommand
a function to execute a command