openPMD-api
ls.hpp
1 /* Copyright 2020-2021 Axel Huebl
2  *
3  * This file is part of openPMD-api.
4  *
5  * openPMD-api is free software: you can redistribute it and/or modify
6  * it under the terms of of either the GNU General Public License or
7  * the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * openPMD-api is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License and the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * and the GNU Lesser General Public License along with openPMD-api.
19  * If not, see <http://www.gnu.org/licenses/>.
20  */
21 #pragma once
22 
23 #include "openPMD/Series.hpp"
24 #include "openPMD/helper/list_series.hpp"
25 
26 #include <exception>
27 #include <iostream>
28 #include <string>
29 #include <vector>
30 
31 
32 namespace openPMD
33 {
34 namespace cli
35 {
36 namespace ls
37 {
38  inline void
39  print_help( std::string const program_name )
40  {
41  std::cout << "Usage: " << program_name << " openPMD-series\n";
42  std::cout << "List information about an openPMD data series.\n\n";
43  std::cout << "Options:\n";
44  std::cout << " -h, --help display this help and exit\n";
45  std::cout << " -v, --version output version information and exit\n";
46  std::cout << "\n";
47  std::cout << "Examples:\n";
48  std::cout << " " << program_name << " ./samples/git-sample/data%T.h5\n";
49  std::cout << " " << program_name << " ./samples/git-sample/data%08T.h5\n";
50  std::cout << " " << program_name << " ./samples/serial_write.json\n";
51  std::cout << " " << program_name << " ./samples/serial_patch.bp\n";
52  }
53 
54  inline void
55  print_version( std::string const program_name )
56  {
57  std::cout << program_name << " (openPMD-api) "
58  << getVersion() << "\n";
59  std::cout << "Copyright 2017-2021 openPMD contributors\n";
60  std::cout << "Authors: Axel Huebl et al.\n";
61  std::cout << "License: LGPLv3+\n";
62  std::cout << "This is free software: you are free to change and redistribute it.\n"
63  "There is NO WARRANTY, to the extent permitted by law.\n";
64  }
65 
71  inline int
72  run( std::vector< std::string > const & argv )
73  {
74  using namespace openPMD;
75  auto const argc = argv.size();
76 
77  if (argc < 2) {
78  print_help(argv[0]);
79  return 0;
80  }
81 
82  for (int c = 1; c < int(argc); c++) {
83  if (std::string("--help") == argv[c] || std::string("-h") == argv[c]) {
84  print_help(argv[0]);
85  return 0;
86  }
87  if (std::string("--version") == argv[c] || std::string("-v") == argv[c]) {
88  print_version(argv[0]);
89  return 0;
90  }
91  }
92 
93  if (argc > 2) {
94  std::cerr << "Too many arguments! See: " << argv[0] << " --help\n";
95  return 1;
96  }
97 
98  try {
99  auto s = Series(
100  argv[1],
102  R"({"defer_iteration_parsing": true})"
103  );
104 
105  helper::listSeries(s, true, std::cout);
106  }
107  catch (std::exception const &e) {
108  std::cerr << "An error occurred while opening the specified openPMD series!\n";
109  std::cerr << e.what() << std::endl;
110  return 2;
111  }
112 
113  return 0;
114  }
115 } // namespace ls
116 } // namespace cli
117 } // namespace openPMD
Root level of the openPMD hierarchy.
Definition: Series.hpp:476
std::string getVersion()
Return the version of the openPMD-api library (run-time)
Definition: version.cpp:28
Public definitions of openPMD-api.
Definition: Date.cpp:29
open series as read-only, fails if series is not found