blob: fb146924a892b074fe22a4825c4b7a07c98a885a [file] [log] [blame]
Junxiao Shi78c78cc2015-06-19 15:53:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoda85e252019-03-18 11:42:01 -04002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2014-2021, Regents of the University of California.
Junxiao Shi78c78cc2015-06-19 15:53:53 -07004 *
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>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070024#include <boost/program_options/parsers.hpp>
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050025#include <boost/program_options/variables_map.hpp>
Junxiao Shi78c78cc2015-06-19 15:53:53 -070026
Davide Pesaventoc0702702017-08-24 22:04:00 -040027#include <fstream>
28
Junxiao Shi78c78cc2015-06-19 15:53:53 -070029namespace ndn {
30namespace dissect {
31
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050032namespace po = boost::program_options;
33
Davide Pesaventoc0702702017-08-24 22:04:00 -040034static void
Junxiao Shi78c78cc2015-06-19 15:53:53 -070035usage(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 Pesaventoc0702702017-08-24 22:04:00 -040043static int
Junxiao Shi78c78cc2015-06-19 15:53:53 -070044main(int argc, char* argv[])
45{
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050046 std::string inputFileName;
47
Junxiao Shi78c78cc2015-06-19 15:53:53 -070048 po::options_description visibleOptions;
49 visibleOptions.add_options()
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050050 ("help,h", "print help and exit")
51 ("version,V", "print version and exit")
Junxiao Shi78c78cc2015-06-19 15:53:53 -070052 ;
53
Junxiao Shi78c78cc2015-06-19 15:53:53 -070054 po::options_description hiddenOptions;
55 hiddenOptions.add_options()
56 ("input-file", po::value<std::string>(&inputFileName));
Junxiao Shi78c78cc2015-06-19 15:53:53 -070057
58 po::options_description allOptions;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050059 allOptions.add(visibleOptions).add(hiddenOptions);
60
61 po::positional_options_description pos;
62 pos.add("input-file", -1);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070063
64 po::variables_map vm;
65 try {
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050066 po::store(po::command_line_parser(argc, argv).options(allOptions).positional(pos).run(), vm);
Junxiao Shi78c78cc2015-06-19 15:53:53 -070067 po::notify(vm);
68 }
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000069 catch (const po::error& e) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070070 std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
71 usage(std::cerr, argv[0], visibleOptions);
72 return 2;
73 }
74
75 if (vm.count("help") > 0) {
76 usage(std::cout, argv[0], visibleOptions);
77 return 0;
78 }
79
80 if (vm.count("version") > 0) {
81 std::cout << "ndn-dissect " << tools::VERSION << std::endl;
82 return 0;
83 }
84
85 std::ifstream inputFile;
86 std::istream* inputStream;
87
88 if (vm.count("input-file") > 0 && inputFileName != "-") {
89 inputFile.open(inputFileName);
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +000090 if (!inputFile.is_open()) {
91 std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl;
92 return 3;
93 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070094 inputStream = &inputFile;
95 }
96 else {
97 inputStream = &std::cin;
98 }
99
100 NdnDissect program;
101 program.dissect(std::cout, *inputStream);
102
103 return 0;
104}
105
106} // namespace dissect
107} // namespace ndn
108
109int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400110main(int argc, char* argv[])
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700111{
112 return ndn::dissect::main(argc, argv);
113}