ModelSpace
All Classes Namespaces Functions Variables Enumerations Pages
ArgParser.h
1/******************************************************************************
2* Copyright (c) ATTX LLC 2024. All Rights Reserved.
3*
4* This software and associated documentation (the "Software") are the
5* proprietary and confidential information of ATTX, LLC. The Software is
6* furnished under a license agreement between ATTX and the user organization
7* and may be used or copied only in accordance with the terms of the agreement.
8* Refer to 'license/attx_license.adoc' for standard license terms.
9*
10* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
11* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION
12* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or
13* transmitted in any form or by any means, for any purpose, without the express
14* written permission of ATTX, LLC.
15******************************************************************************/
16#ifndef ARG_PARSER_H
17#define ARG_PARSER_H
18
19#include <vector>
20#include <string>
21
22#include "data_management/GraphTreeObject.h"
23#include "data_management/DataIO.hpp"
24
25namespace modelspace {
26
27 const std::string ARG_INDICATOR = "--";
28
29 /**
30 * @brief Class to parse arguments from the command line in any language
31 */
32 class ArgParser {
33 public:
34 /// @brief Default constructor for the arg parser -- sets name to "args"
35 ArgParser(){}
36 ~ArgParser() {}
37
38 /// @brief Function to get the string value of an arg
39 /// @param name The arg to get
40 /// @return The arg as a string value. Empty string if arg does not exist
41 std::string operator () (const std::string &name) {std::string tmp; get(name, tmp); return tmp;}
42
43 /// @brief Function to get the string value of an arg
44 /// @param name The arg to get
45 /// @param val The returned value
46 /// @return The arg as a string value. Empty string if arg does not exist
47 int get(const std::string &name, std::string &val);
48
49 /// @brief Function to get the string value of an arg as a double
50 /// @param name The arg to get
51 /// @param val The returned value
52 /// @return The arg as a double value. Will produce error if value can't be cast to double
53 int get(const std::string &name, double &val);
54
55 /// @brief Function to get the string value of an arg as an int
56 /// @param name The arg to get
57 /// @param val The returned value
58 /// @return The arg as an int value. Will produce error if value can't be cast to int
59 int get(const std::string &name, int &val);
60
61 /// @brief Function to parse argc and argv for string arguments
62 /// @param argc C++ argc input
63 /// @param argv C++ argv input
64 /// @return Error code corresponding to success/failure
65 int parseArgs(int argc, char *argv[]);
66
67 /// @brief Function to loop through args and parse for values
68 /// @param args A std::vector of arguments
69 /// @return Error code corresponding to success/failure
70 int parseArgs(std::vector<std::string> args);
71
72 /// @brief Function to add a default argument that can be overridden from the cmd line
73 /// @param name The name of the argument to add
74 /// @param val The default value
75 void addDefaultArgument(const std::string &name, const std::string &val);
76
77 /// @brief Function to add a default argument that can be overridden from the cmd line
78 /// @param name The name of the argument to add
79 /// @param val The default value
80 void addDefaultArgument(const std::string &name, double val);
81
82 /// @brief Function to add a default argument that can be overridden from the cmd line
83 /// @param name The name of the argument to add
84 /// @param val The default value
85 void addDefaultArgument(const std::string &name, int val);
86
87 /// @brief Function to dump all argument value pairs held in the class
88 void dump();
89 private:
90 // Pair to store key value pairs as <key, value>
91 std::vector<std::pair<std::string, std::string>> _key_val_pairs;
92
93 // Pair to store default key value pairs that can be overwritten by input arguments
94 std::vector<std::pair<std::string, std::string>> _default_key_val_pairs;
95 };
96
97 /// @brief Function to return whether substr is in parent_str
98 /// @param parent_str The string to search
99 /// @param substr The string to search for
100 /// @return True if substr is in parent_str. False otherwise
101 bool contains(const std::string &parent_str, const std::string &substr);
102
103 /// @brief Function to trim leading and trailing whitespace from string
104 /// @param str The string to trim
105 /// @param whitespace The whitespace we're looking to trim off. Defaults to " \t"
106 /// @return The trimmed string
107 std::string trim(const std::string& str, const std::string& whitespace=" \t");
108
109}
110
111#endif
Class to parse arguments from the command line in any language.
Definition ArgParser.h:32
int get(const std::string &name, int &val)
Function to get the string value of an arg as an int.
Definition ArgParser.cpp:60
int get(const std::string &name, std::string &val)
Function to get the string value of an arg.
Definition ArgParser.cpp:22
void addDefaultArgument(const std::string &name, int val)
Function to add a default argument that can be overridden from the cmd line.
Definition ArgParser.cpp:152
int get(const std::string &name, double &val)
Function to get the string value of an arg as a double.
Definition ArgParser.cpp:43
void addDefaultArgument(const std::string &name, const std::string &val)
Function to add a default argument that can be overridden from the cmd line.
Definition ArgParser.cpp:144
int parseArgs(int argc, char *argv[])
Function to parse argc and argv for string arguments.
Definition ArgParser.cpp:77
ArgParser()
Default constructor for the arg parser – sets name to "args".
Definition ArgParser.h:35
int parseArgs(std::vector< std::string > args)
Function to loop through args and parse for values.
Definition ArgParser.cpp:88
void addDefaultArgument(const std::string &name, double val)
Function to add a default argument that can be overridden from the cmd line.
Definition ArgParser.cpp:148
std::string operator()(const std::string &name)
Function to get the string value of an arg.
Definition ArgParser.h:41
void dump()
Function to dump all argument value pairs held in the class.
Definition ArgParser.cpp:156
Class to propagate CR3BP dynamics in characteristic units.
Definition ConfigurationWriter.cpp:18
bool contains(const std::string &parent_str, const std::string &substr)
Function to return whether substr is in parent_str.
Definition ArgParser.cpp:195
std::string trim(const std::string &str, const std::string &whitespace=" \t")
Function to trim leading and trailing whitespace from string.
Definition ArgParser.cpp:199