IOSS 2.0
Loading...
Searching...
No Matches
Ioss_GetLongOpt.h
Go to the documentation of this file.
1/*
2 * Copyright(C) 1999-2024 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
18namespace 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 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{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 = '-');
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
A database of program command line and environment variable options and methods for manipulating them...
Definition Ioss_GetLongOpt.h:25
char optmarker
Definition Ioss_GetLongOpt.h:48
DBL get_option_value(const char *option_txt, DBL default_value)
Definition Ioss_GetLongOpt.h:90
void usage(const char *str)
Set the program usage string.
Definition Ioss_GetLongOpt.h:76
OptType
Definition Ioss_GetLongOpt.h:27
INT get_option_value(const char *option_txt, INT default_value)
Definition Ioss_GetLongOpt.h:79
std::string get_option_value(const char *option_txt, const std::string &default_value) const
Definition Ioss_GetLongOpt.h:100
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
Definition Ioss_GetLongOpt.h:31