HDTree  0.5.2
HDTree C++ API
examples.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <utility>
14 
21 namespace hdtree::examples {
22 
29 void print_help(const std::string& program, bool two_files) {
30  std::cout
31  << "USAGE:\n"
32  << " " << program << " [-h | --help ] "
33  << (two_files ? "SRC_FILE SRC_TREE [DEST_FILE DEST_TREE]" : "FILE TREE") << "\n"
34  << "\n"
35  << "OPTIONS:\n"
36  << " -h, --help : print this help and exit\n"
37  << "\n"
38  << "ARGUMENTS:\n";
39  if (two_files) {
40  std::cout
41  << " SRC_FILE : source file from-which to read HDTree (and write if no DEST_FILE)\n"
42  << " SRC_TREE : source tree from-which to read (and write if no DEST_TREE)\n"
43  << " DEST_FILE : destination file to write to (optional but needs DEST_TREE as well)\n"
44  << " DEST_TREE : destination tree to write to (optional but needs DEST_FILE as well)\n";
45  } else {
46  std::cout
47  << " FILE : file to access for interacting with HDTree\n"
48  << " TREE : name of tree in that file to interact with\n";
49  }
50  std::cout << std::endl;
51 }
52 
62 int parse_single_file_args(int argc, char** argv,
63  std::string& filename, std::string& treename) {
64  std::string program{argv[0]};
65  std::vector<std::string> positionals;
66  for (int i_arg{1}; i_arg < argc; ++i_arg) {
67  std::string arg{argv[i_arg]};
68  if (arg == "-h" or arg == "--help") {
69  print_help(program,false);
70  return 1;
71  } else {
72  positionals.emplace_back(arg);
73  }
74  }
75 
76  if (positionals.size() == 0) {
77  // absolutely zero arguments => assume user wants help
78  print_help(program,false);
79  return 2;
80  }
81 
82  if (positionals.size() != 2) {
83  std::cerr << " ERROR: " << program << " requires two arguments: FILE TREE" << std::endl;
84  return -1;
85  }
86 
87  filename = positionals.at(0);
88  treename = positionals.at(1);
89  return 0;
90 }
91 
101 int parse_two_file_args(int argc, char** argv,
102  std::pair<std::string,std::string>& src, std::pair<std::string,std::string>& dest) {
103  std::string program{argv[0]};
104  std::vector<std::string> positionals;
105  for (int i_arg{1}; i_arg < argc; ++i_arg) {
106  std::string arg{argv[i_arg]};
107  if (arg == "-h" or arg == "--help") {
108  print_help(program,true);
109  return 1;
110  } else {
111  positionals.emplace_back(arg);
112  }
113  }
114 
115  if (positionals.size() == 0) {
116  // absolutely zero arguments => assume user wants help
117  print_help(program,true);
118  return 2;
119  }
120 
121  if (positionals.size() != 2 and positionals.size() != 4) {
122  std::cerr << " ERROR: " << program << " requires two or four arguments." << std::endl;
123  return -1;
124  }
125 
126  src.first = positionals.at(0);
127  src.second = positionals.at(1);
128  if (positionals.size() > 2) {
129  dest.first = positionals.at(2);
130  dest.second = positionals.at(3);
131  }
132  return 0;
133 }
134 
135 }
namespace holding utility functions for example programs
Definition: examples.h:21
int parse_single_file_args(int argc, char **argv, std::string &filename, std::string &treename)
single file command line parser
Definition: examples.h:62
void print_help(const std::string &program, bool two_files)
print help for the single file command line parser
Definition: examples.h:29
int parse_two_file_args(int argc, char **argv, std::pair< std::string, std::string > &src, std::pair< std::string, std::string > &dest)
potentially two file command line parser
Definition: examples.h:101