blob: 7232e321ae3aebcb3a500d268ce09810c1ea5484 [file] [log] [blame]
Junxiao Shi78c78cc2015-06-19 15:53:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi3ebb9cb2016-11-23 14:58:55 +00003 * Copyright (c) 2014-2016, 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
27namespace po = boost::program_options;
28
29namespace ndn {
30namespace dissect {
31
32void
33usage(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
41int
42main(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 Shi3ebb9cb2016-11-23 14:58:55 +000074 catch (const po::error& e) {
Junxiao Shi78c78cc2015-06-19 15:53:53 -070075 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 Shi3ebb9cb2016-11-23 14:58:55 +000095 if (!inputFile.is_open()) {
96 std::cerr << argv[0] << ": " << inputFileName << ": File does not exist or is unreadable" << std::endl;
97 return 3;
98 }
Junxiao Shi78c78cc2015-06-19 15:53:53 -070099 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
114int
115main(int argc, char** argv)
116{
117 return ndn::dissect::main(argc, argv);
118}