| 1 |
/* |
| 2 |
* Copyright(C) 1999-2025 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
/* S Manoharan. Advanced Computer Research Institute. Lyon. France */ |
| 9 |
|
| 10 |
#pragma once |
| 11 |
|
| 12 |
#include <cstdlib> |
| 13 |
#include <iostream> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "ioss_export.h" |
| 17 |
|
| 18 |
namespace Ioss { |
| 19 |
/** \brief A database of program command line and environment variable options and methods for |
| 20 |
* manipulating them. |
| 21 |
* |
| 22 |
* A collection of long command line option names for a program that uses the Ioss library. |
| 23 |
*/ |
| 24 |
class IOSS_EXPORT GetLongOption |
| 25 |
{ |
| 26 |
public: |
| 27 |
enum class OptType { NoValue, OptionalValue, MandatoryValue }; |
| 28 |
|
| 29 |
private: |
| 30 |
struct Cell |
| 31 |
{ |
| 32 |
const char *option{nullptr}; // option name |
| 33 |
const char *description{nullptr}; // a description of option |
| 34 |
const char *value{nullptr}; // value of option (string) |
| 35 |
const char *opt_value{ |
| 36 |
nullptr}; // If optional value and value not entered, assign opt_value to value |
| 37 |
Cell *next{nullptr}; // pointer to the next cell |
| 38 |
OptType type{OptType::NoValue}; // option type |
| 39 |
bool extra_line{false}; // True if `usage()` should output extra line at end of entry |
| 40 |
|
| 41 |
Cell() = default; |
| 42 |
}; |
| 43 |
|
| 44 |
Cell *table{nullptr}; // option table |
| 45 |
const char *ustring{"[valid options and arguments]"}; |
| 46 |
char *pname{nullptr}; // program basename |
| 47 |
Cell *last{nullptr}; // last entry in option table |
| 48 |
char optmarker; // option marker |
| 49 |
bool options_parsed{false}; // parsed options, cannot enroll anymore options |
| 50 |
|
| 51 |
int setcell(Cell *c, char *valtoken, char *nexttoken, const char *name); |
| 52 |
|
| 53 |
public: |
| 54 |
explicit GetLongOption(char optmark = '-'); |
| 55 |
~GetLongOption(); |
| 56 |
|
| 57 |
static char *basename(char *pathname); |
| 58 |
|
| 59 |
int parse(int argc, char *const *argv); |
| 60 |
int parse(char *str, char *p); |
| 61 |
|
| 62 |
bool enroll(const char *opt, OptType t, const char *desc, const char *val, |
| 63 |
const char *optval = nullptr, bool extra_line = false); |
| 64 |
const char *retrieve(const char *opt) const; |
| 65 |
const char *program_name() const; |
| 66 |
|
| 67 |
void usage(std::ostream &outfile = std::cout) const; |
| 68 |
|
| 69 |
/** \brief Set the program usage string. |
| 70 |
* |
| 71 |
* The program usage string should define the command line |
| 72 |
* syntax for program options and arguments and contain |
| 73 |
* other helpful usage text. |
| 74 |
* \param[in] str The usage string. |
| 75 |
*/ |
| 76 |
void usage(const char *str) { ustring = str; } |
| 77 |
|
| 78 |
template <class INT, typename std::enable_if_t<std::is_integral_v<INT>, INT> * = nullptr> |
| 79 |
INT get_option_value(const char *option_txt, INT default_value) |
| 80 |
{ |
| 81 |
INT value = default_value; |
| 82 |
const char *temp = retrieve(option_txt); |
| 83 |
if (temp != nullptr) { |
| 84 |
value = std::strtol(temp, nullptr, 10); |
| 85 |
} |
| 86 |
return value; |
| 87 |
} |
| 88 |
|
| 89 |
template <class DBL, typename std::enable_if_t<std::is_floating_point_v<DBL>, DBL> * = nullptr> |
| 90 |
DBL get_option_value(const char *option_txt, DBL default_value) |
| 91 |
{ |
| 92 |
DBL value = default_value; |
| 93 |
const char *temp = retrieve(option_txt); |
| 94 |
if (temp != nullptr) { |
| 95 |
value = std::strtod(temp, nullptr); |
| 96 |
} |
| 97 |
return value; |
| 98 |
} |
| 99 |
|
| 100 |
std::string get_option_value(const char *option_txt, const std::string &default_value) const |
| 101 |
{ |
| 102 |
auto value = default_value; |
| 103 |
const char *temp = retrieve(option_txt); |
| 104 |
if (temp != nullptr) { |
| 105 |
value = temp; |
| 106 |
} |
| 107 |
return value; |
| 108 |
} |
| 109 |
}; |
| 110 |
} // namespace Ioss |