PRISMS-PF Manual v3.0-pre
Loading...
Searching...
No Matches
ParseCommandLineOpts.h
1#ifndef INCLUDE_PARSECOMMANDLINE_OPTS_H_
2#define INCLUDE_PARSECOMMANDLINE_OPTS_H_
3
4#include <deal.II/base/mpi.h>
5
6#include <algorithm>
7#include <fstream>
8#include <iostream>
9#include <string>
10#include <vector>
11
13{
14public:
15 ParseCommandLineOpts(int &_argc, char **argv)
16 {
17 argc = _argc;
18 for (int i = 1; i < argc; ++i)
19 tokens.push_back(std::string(argv[i]));
20 }
21
22 std::string
23 getParametersFilename()
24 {
25 std::string parameters_filename;
26
27 if (argc == 3)
28 {
29 if (cmdOptionExists("-i"))
30 {
31 parameters_filename = getCmdOption("-i");
32 if (dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
33 {
34 std::cout << "Using the input parameter file: " << parameters_filename
35 << std::endl;
36 }
37 }
38 else
39 {
40 throw("Invalid command line option given. The only argument should "
41 "be to specify the input file name.");
42 }
43 }
44 else if (argc == 2)
45 {
46 if (cmdOptionExists("-i"))
47 {
48 parameters_filename = "parameters.prm";
49 std::ifstream ifs_prm(parameters_filename);
50 std::ifstream ifs_in("parameters.in");
51 if (!ifs_prm && ifs_in)
52 throw("The previous extension .in for the parameters file is no "
53 "longer accepted. Please rename parameters.in as "
54 "parameters.prm");
55 if (dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
56 {
57 std::cout << "Using the input parameter file: " << parameters_filename
58 << std::endl;
59 }
60 }
61 else
62 {
63 throw("Invalid command line option given. The only argument should "
64 "be to specify the input file name.");
65 }
66 }
67 else if (argc == 1)
68 {
69 parameters_filename = "parameters.prm";
70 std::ifstream ifs_prm(parameters_filename);
71 std::ifstream ifs_in("parameters.in");
72 if (!ifs_prm && ifs_in)
73 throw("The previous extension .in for the parameters file is no longer "
74 "accepted. Please rename parameters.in as parameters.prm");
75 if (dealii::Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
76 {
77 std::cout << "Using the input parameter file: " << parameters_filename
78 << std::endl;
79 }
80 }
81 else
82 {
83 throw("Too many command line arguments were given. The only argument "
84 "should be to specify the input file name.");
85 }
86
87 return parameters_filename;
88 }
89
90private:
91 int argc;
92 std::vector<std::string> tokens;
93
94 const std::string &
95 getCmdOption(const std::string &option) const
96 {
97 std::vector<std::string>::const_iterator itr;
98 itr = std::find(tokens.begin(), tokens.end(), option);
99 if (itr != tokens.end() && ++itr != tokens.end())
100 {
101 return *itr;
102 }
103 static const std::string empty_string("");
104 return empty_string;
105 }
106
107 bool
108 cmdOptionExists(const std::string &option) const
109 {
110 return std::find(tokens.begin(), tokens.end(), option) != tokens.end();
111 }
112};
113
114#endif
Definition ParseCommandLineOpts.h:13