blob: fa1a962a9db65b69fcd412e45f268e884cacf5d5 [file] [log] [blame]
Junxiao Shi78c78cc2015-06-19 15:53:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California.
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
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 }
74 catch (po::error& e) {
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);
95 inputStream = &inputFile;
96 }
97 else {
98 inputStream = &std::cin;
99 }
100
101 NdnDissect program;
102 program.dissect(std::cout, *inputStream);
103
104 return 0;
105}
106
107} // namespace dissect
108} // namespace ndn
109
110int
111main(int argc, char** argv)
112{
113 return ndn::dissect::main(argc, argv);
114}