Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "ndn-dissect.hpp" |
| 21 | #include "core/version.hpp" |
| 22 | |
| 23 | #include <boost/program_options/options_description.hpp> |
| 24 | #include <boost/program_options/variables_map.hpp> |
| 25 | #include <boost/program_options/parsers.hpp> |
| 26 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 27 | #include <fstream> |
| 28 | |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 29 | namespace po = boost::program_options; |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace dissect { |
| 33 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 34 | static void |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 35 | usage(std::ostream& os, const std::string& appName, const po::options_description& options) |
| 36 | { |
| 37 | os << "Usage:\n" |
| 38 | << " " << appName << " [input-file] \n" |
| 39 | << "\n" |
| 40 | << options; |
| 41 | } |
| 42 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 43 | static int |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 44 | main(int argc, char* argv[]) |
| 45 | { |
| 46 | po::options_description visibleOptions; |
| 47 | visibleOptions.add_options() |
| 48 | ("help,h", "Print help and exit.") |
| 49 | ("version,V", "Print version and exit.") |
| 50 | ; |
| 51 | |
| 52 | std::string inputFileName; |
| 53 | po::options_description hiddenOptions; |
| 54 | hiddenOptions.add_options() |
| 55 | ("input-file", po::value<std::string>(&inputFileName)); |
| 56 | ; |
| 57 | po::positional_options_description positionalArguments; |
| 58 | positionalArguments |
| 59 | .add("input-file", -1); |
| 60 | |
| 61 | po::options_description allOptions; |
| 62 | allOptions |
| 63 | .add(visibleOptions) |
| 64 | .add(hiddenOptions) |
| 65 | ; |
| 66 | |
| 67 | po::variables_map vm; |
| 68 | try { |
| 69 | po::store(po::command_line_parser(argc, argv) |
| 70 | .options(allOptions) |
| 71 | .positional(positionalArguments) |
| 72 | .run(), |
| 73 | vm); |
| 74 | po::notify(vm); |
| 75 | } |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame] | 76 | catch (const po::error& e) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 77 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
| 78 | usage(std::cerr, argv[0], visibleOptions); |
| 79 | return 2; |
| 80 | } |
| 81 | |
| 82 | if (vm.count("help") > 0) { |
| 83 | usage(std::cout, argv[0], visibleOptions); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | if (vm.count("version") > 0) { |
| 88 | std::cout << "ndn-dissect " << tools::VERSION << std::endl; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | std::ifstream inputFile; |
| 93 | std::istream* inputStream; |
| 94 | |
| 95 | if (vm.count("input-file") > 0 && inputFileName != "-") { |
| 96 | inputFile.open(inputFileName); |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame] | 97 | if (!inputFile.is_open()) { |
| 98 | std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl; |
| 99 | return 3; |
| 100 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 101 | inputStream = &inputFile; |
| 102 | } |
| 103 | else { |
| 104 | inputStream = &std::cin; |
| 105 | } |
| 106 | |
| 107 | NdnDissect program; |
| 108 | program.dissect(std::cout, *inputStream); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | } // namespace dissect |
| 114 | } // namespace ndn |
| 115 | |
| 116 | int |
| 117 | main(int argc, char** argv) |
| 118 | { |
| 119 | return ndn::dissect::main(argc, argv); |
| 120 | } |