Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2016, 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 | |
| 27 | namespace po = boost::program_options; |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace dissect { |
| 31 | |
| 32 | void |
| 33 | usage(std::ostream& os, const std::string& appName, const po::options_description& options) |
| 34 | { |
| 35 | os << "Usage:\n" |
| 36 | << " " << appName << " [input-file] \n" |
| 37 | << "\n" |
| 38 | << options; |
| 39 | } |
| 40 | |
| 41 | int |
| 42 | main(int argc, char* argv[]) |
| 43 | { |
| 44 | po::options_description visibleOptions; |
| 45 | visibleOptions.add_options() |
| 46 | ("help,h", "Print help and exit.") |
| 47 | ("version,V", "Print version and exit.") |
| 48 | ; |
| 49 | |
| 50 | std::string inputFileName; |
| 51 | po::options_description hiddenOptions; |
| 52 | hiddenOptions.add_options() |
| 53 | ("input-file", po::value<std::string>(&inputFileName)); |
| 54 | ; |
| 55 | po::positional_options_description positionalArguments; |
| 56 | positionalArguments |
| 57 | .add("input-file", -1); |
| 58 | |
| 59 | po::options_description allOptions; |
| 60 | allOptions |
| 61 | .add(visibleOptions) |
| 62 | .add(hiddenOptions) |
| 63 | ; |
| 64 | |
| 65 | po::variables_map vm; |
| 66 | try { |
| 67 | po::store(po::command_line_parser(argc, argv) |
| 68 | .options(allOptions) |
| 69 | .positional(positionalArguments) |
| 70 | .run(), |
| 71 | vm); |
| 72 | po::notify(vm); |
| 73 | } |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame^] | 74 | catch (const po::error& e) { |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 75 | std::cerr << "ERROR: " << e.what() << std::endl << std::endl; |
| 76 | usage(std::cerr, argv[0], visibleOptions); |
| 77 | return 2; |
| 78 | } |
| 79 | |
| 80 | if (vm.count("help") > 0) { |
| 81 | usage(std::cout, argv[0], visibleOptions); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | if (vm.count("version") > 0) { |
| 86 | std::cout << "ndn-dissect " << tools::VERSION << std::endl; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | std::ifstream inputFile; |
| 91 | std::istream* inputStream; |
| 92 | |
| 93 | if (vm.count("input-file") > 0 && inputFileName != "-") { |
| 94 | inputFile.open(inputFileName); |
Junxiao Shi | 3ebb9cb | 2016-11-23 14:58:55 +0000 | [diff] [blame^] | 95 | if (!inputFile.is_open()) { |
| 96 | std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl; |
| 97 | return 3; |
| 98 | } |
Junxiao Shi | 78c78cc | 2015-06-19 15:53:53 -0700 | [diff] [blame] | 99 | inputStream = &inputFile; |
| 100 | } |
| 101 | else { |
| 102 | inputStream = &std::cin; |
| 103 | } |
| 104 | |
| 105 | NdnDissect program; |
| 106 | program.dissect(std::cout, *inputStream); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | } // namespace dissect |
| 112 | } // namespace ndn |
| 113 | |
| 114 | int |
| 115 | main(int argc, char** argv) |
| 116 | { |
| 117 | return ndn::dissect::main(argc, argv); |
| 118 | } |