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-2017, 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() || args[0] == "-h") {
45  helpList(std::cout, parser);
46  return 0;
47  }
48 
49  if (args[0] == "-V") {
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  std::cerr << e.what() << std::endl;
62  return 2;
63  }
64 
65  try {
66  Face face;
67  KeyChain keyChain;
68  Controller controller(face, keyChain);
69  ExecuteContext ctx{noun, verb, ca, 0, std::cout, std::cerr, face, keyChain, controller};
70  execute(ctx);
71  return ctx.exitCode;
72  }
73  catch (const std::exception& e) {
74  std::cerr << e.what() << std::endl;
75  return 1;
76  }
77 }
78 
79 } // namespace nfdc
80 } // namespace tools
81 } // namespace nfd
82 
83 int
84 main(int argc, char** argv)
85 {
86  return nfd::tools::nfdc::main(argc, argv);
87 }
static int main(int argc, char **argv)
Definition: main.cpp:37
context for command execution
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
contains named command arguments
std::tuple< std::string, std::string, CommandArguments, ExecuteCommand > parse(const std::vector< std::string > &tokens, ParseMode mode) const
parse a command line
void registerCommands(CommandParser &parser)
void helpList(std::ostream &os, const CommandParser &parser, ParseMode mode, const std::string &noun)
Definition: help.cpp:40
std::function< void(ExecuteContext &ctx)> ExecuteCommand
a function to execute a command