Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 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 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 26 | #include "available-commands.hpp" |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 27 | #include "help.hpp" |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 28 | #include "core/version.hpp" |
| 29 | |
Davide Pesavento | a997d29 | 2017-08-24 20:16:59 -0400 | [diff] [blame] | 30 | #include <iostream> |
| 31 | |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace tools { |
| 34 | namespace nfdc { |
| 35 | |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 36 | static int |
| 37 | main(int argc, char** argv) |
| 38 | { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 39 | std::vector<std::string> args(argv + 1, argv + argc); |
| 40 | |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 41 | CommandParser parser; |
| 42 | registerCommands(parser); |
| 43 | |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 44 | if (args.empty()) { |
Junxiao Shi | 6c13562 | 2016-11-21 14:30:33 +0000 | [diff] [blame] | 45 | helpList(std::cout, parser); |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 49 | if (args[0] == "-V" || args[0] == "--version") { |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 50 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 51 | return 0; |
| 52 | } |
| 53 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 54 | std::string noun, verb; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 55 | CommandArguments ca; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 56 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 57 | try { |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 58 | std::tie(noun, verb, ca, execute) = parser.parse(args, ParseMode::ONE_SHOT); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 59 | } |
| 60 | catch (const std::invalid_argument& e) { |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 61 | int ret = help(std::cout, parser, std::move(args)); |
| 62 | if (ret == 2) |
| 63 | std::cerr << e.what() << std::endl; |
| 64 | return ret; |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 67 | try { |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 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}; |
Junxiao Shi | 88a062d | 2017-01-26 03:10:05 +0000 | [diff] [blame] | 72 | execute(ctx); |
| 73 | return ctx.exitCode; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 74 | } |
| 75 | catch (const std::exception& e) { |
| 76 | std::cerr << e.what() << std::endl; |
| 77 | return 1; |
| 78 | } |
Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 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 | } |