blob: a2a7034983efdb41dd1cc175e5169b2c7590e914 [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/*
3 * Copyright (c) 2014-2019, 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>
24#include <boost/program_options/variables_map.hpp>
25#include <boost/program_options/parsers.hpp>
26
Davide Pesaventoc0702702017-08-24 22:04:00 -040027#include <fstream>
28
Junxiao Shi78c78cc2015-06-19 15:53:53 -070029namespace po = boost::program_options;
30
31namespace ndn {
32namespace dissect {
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{
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 Shi3ebb9cb2016-11-23 14:58:55 +000076 catch (const po::error& e) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070077 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 Shi3ebb9cb2016-11-23 14:58:55 +000097 if (!inputFile.is_open()) {
98 std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl;
99 return 3;
100 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700101 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
116int
Davide Pesaventoda85e252019-03-18 11:42:01 -0400117main(int argc, char* argv[])
Junxiao Shi78c78cc2015-06-19 15:53:53 -0700118{
119 return ndn::dissect::main(argc, argv);
120}