TDMS
Time Domain Maxwell Solver
All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
argument_parser.h
Go to the documentation of this file.
1/**
2 * @file argument_parser.h
3 * @brief Parse the command line options.
4 */
5#pragma once
6
7#include <string>
8#include <vector>
9
10#include "globals.h"
11
12/**
13 * @brief Wraps a vector of string CL arguments with some helpful functionality.
14 */
16
17private:
18 std::vector<std::string> arguments; //< The arguments from flags
19 std::vector<std::string> non_flag_arguments;//< Arguments not from flags
20
21public:
22 int num_non_flag = 0;//< A count of arguments not from flags
23
24 /**
25 * @brief Construct a new Argument Namespace object
26 *
27 * @param n_args the number of arguments (argc)
28 * @param argv pointers to the arguments (argv)
29 */
30 explicit ArgumentNamespace(int n_args, char *argv[]);
31
32 /**
33 * @brief Searches the arguments for the flag provided
34 *
35 * @param flag to search for
36 * @return true if the flag is present
37 * @return false otherwise
38 */
39 bool have_flag(std::string const &flag) const;
40
41 /**
42 * @brief Return true if the user has requested the output file we written in
43 * compressed format.
44 * @details Compressed output is toggled by the -m, --minimise-file-size
45 * options on the CLI.
46 *
47 * This option is used when the outputs are being written, after the
48 * simulation has been performed.
49 */
50 bool compressed_output() const;
51
52 /**
53 * @brief Have we been provided with a grid filename?
54 *
55 * @return true if there are 3 non-flag arguments (therefore a grid filename)
56 * @return false otherwise
57 */
58 bool has_grid_filename() const;
59
60 /**
61 * @brief Check that the correct number of filename arguments are provided
62 * (either 2 or 3 non-flag arguments)
63 *
64 * @return true if correct
65 * @return false otherwise
66 */
68
69 /**
70 * @brief Check whether an argument is a flag (starts '-')
71 *
72 * @param arg the argument
73 * @return true if arg is a flag
74 * @return false otherwise
75 */
76 static bool is_a_flag_argument(std::string arg);
77
78 /**
79 * @brief Gets the input filename
80 *
81 * @return const char* the input filename
82 */
83 const char *input_filename();
84
85 /**
86 * @brief Gets the output filename
87 *
88 * The output filename is either the second or third positional argument
89 * depending on whether a grid filename is provided or not.
90 *
91 * @return const char* the output filename
92 */
93 const char *output_filename();
94
95 /**
96 * @brief Gets the grid filename
97 *
98 * @return const char* the grid filename
99 */
100 const char *grid_filename();
101
102 /**
103 * @brief Get all input filenames
104 *
105 * A vector containing the input filename and the grid filename (if
106 * provided)
107 *
108 * @return std::vector<std::string> the input filenames
109 */
110 std::vector<std::string> input_filenames();
111
112 /**
113 * @brief Check that all input and output files can be accessed with the
114 * correct privileges
115 */
117};
118
119/**
120 * @brief Performs the argument parsing and returns an ArgumentNamespace
121 */
123
124private:
125 /** Prints the help message (all options). */
126 static void print_help_message();
127 /** Prints the version of the compiled executable. */
128 static void print_version();
129
130public:
131 /**
132 * @brief Parse the command line arguments and perform relevant actions.
133 *
134 * @param n_args The number of arguments (argc)
135 * @param arg_ptrs Pointers to to the arguments (argv)
136 * @return ArgumentNamespace populated with options
137 */
138 static ArgumentNamespace parse_args(int n_args, char *arg_ptrs[]);
139};
Wraps a vector of string CL arguments with some helpful functionality.
Definition argument_parser.h:15
bool have_correct_number_of_filenames() const
Check that the correct number of filename arguments are provided (either 2 or 3 non-flag arguments)
Definition argument_parser.cpp:136
const char * input_filename()
Gets the input filename.
Definition argument_parser.cpp:108
bool have_flag(std::string const &flag) const
Searches the arguments for the flag provided.
Definition argument_parser.cpp:80
static bool is_a_flag_argument(std::string arg)
Check whether an argument is a flag (starts '-')
Definition argument_parser.cpp:76
std::vector< std::string > input_filenames()
Get all input filenames.
Definition argument_parser.cpp:117
const char * grid_filename()
Gets the grid filename.
Definition argument_parser.cpp:101
const char * output_filename()
Gets the output filename.
Definition argument_parser.cpp:89
bool compressed_output() const
Return true if the user has requested the output file we written in compressed format.
Definition argument_parser.cpp:140
bool has_grid_filename() const
Have we been provided with a grid filename?
Definition argument_parser.cpp:115
void check_files_can_be_accessed()
Check that all input and output files can be accessed with the correct privileges.
Definition argument_parser.cpp:127
Performs the argument parsing and returns an ArgumentNamespace.
Definition argument_parser.h:122
static ArgumentNamespace parse_args(int n_args, char *arg_ptrs[])
Parse the command line arguments and perform relevant actions.
Definition argument_parser.cpp:12
static void print_help_message()
Definition argument_parser.cpp:45
static void print_version()
Definition argument_parser.cpp:58
Type definitions and global constants.