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 namespace openPMD
32 {
33 namespace cli
34 {
35  namespace ls
36  {
37  inline void print_help(std::string const program_name)
38  {
39  std::cout << "Usage: " << program_name << " openPMD-series\n";
40  std::cout << "List information about an openPMD data series.\n\n";
41  std::cout << "Options:\n";
42  std::cout << " -h, --help display this help and exit\n";
43  std::cout
44  << " -v, --version output version information and exit\n";
45  std::cout << "\n";
46  std::cout << "Examples:\n";
47  std::cout << " " << program_name
48  << " ./samples/git-sample/data%T.h5\n";
49  std::cout << " " << program_name
50  << " ./samples/git-sample/data%08T.h5\n";
51  std::cout << " " << program_name
52  << " ./samples/serial_write.json\n";
53  std::cout << " " << program_name
54  << " ./samples/serial_patch.bp\n";
55  }
56 
57  inline void print_version(std::string const program_name)
58  {
59  std::cout << program_name << " (openPMD-api) " << getVersion()
60  << "\n";
61  std::cout << "Copyright 2017-2021 openPMD contributors\n";
62  std::cout << "Authors: Axel Huebl et al.\n";
63  std::cout << "License: LGPLv3+\n";
64  std::cout
65  << "This is free software: you are free to change and "
66  "redistribute it.\n"
67  "There is NO WARRANTY, to the extent permitted by law.\n";
68  }
69 
75  inline int run(std::vector<std::string> const &argv)
76  {
77  using namespace openPMD;
78  auto const argc = argv.size();
79 
80  if (argc < 2)
81  {
82  print_help(argv[0]);
83  return 0;
84  }
85 
86  for (int c = 1; c < int(argc); c++)
87  {
88  if (std::string("--help") == argv[c] ||
89  std::string("-h") == argv[c])
90  {
91  print_help(argv[0]);
92  return 0;
93  }
94  if (std::string("--version") == argv[c] ||
95  std::string("-v") == argv[c])
96  {
97  print_version(argv[0]);
98  return 0;
99  }
100  }
101 
102  if (argc > 2)
103  {
104  std::cerr << "Too many arguments! See: " << argv[0]
105  << " --help\n";
106  return 1;
107  }
108 
109  try
110  {
111  auto s = Series(
112  argv[1],
114  R"({"defer_iteration_parsing": true})");
115 
116  helper::listSeries(s, true, std::cout);
117  }
118  catch (std::exception const &e)
119  {
120  std::cerr << "An error occurred while opening the specified "
121  "openPMD series!\n";
122  std::cerr << e.what() << std::endl;
123  return 2;
124  }
125 
126  return 0;
127  }
128  } // namespace ls
129 } // namespace cli
130 } // namespace openPMD
Implementation for the root level of the openPMD hierarchy.
Definition: Series.hpp:186
std::string getVersion()
Return the version of the openPMD-api library (run-time)
Definition: version.cpp:26
Public definitions of openPMD-api.
Open Series as read-only, fails if Series is not found.