IOSS 2.0
Loading...
Searching...
No Matches
Ioss_GetLongOpt.h
Go to the documentation of this file.
1/*
2 * Copyright(C) 1999-2023 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{nullptr}; // usage message
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,
79 typename std::enable_if<std::is_integral<INT>::value, INT>::type * = nullptr>
80 INT get_option_value(const char *option_txt, INT default_value)
81 {
82 INT value = default_value;
83 const char *temp = retrieve(option_txt);
84 if (temp != nullptr) {
85 value = std::strtol(temp, nullptr, 10);
86 }
87 return value;
88 }
89
90 template <class DBL,
91 typename std::enable_if<std::is_floating_point<DBL>::value, DBL>::type * = nullptr>
92 DBL get_option_value(const char *option_txt, DBL default_value)
93 {
94 DBL value = default_value;
95 const char *temp = retrieve(option_txt);
96 if (temp != nullptr) {
97 value = std::strtod(temp, nullptr);
98 }
99 return value;
100 }
101
102 std::string get_option_value(const char *option_txt, const std::string &default_value) const
103 {
104 auto value = default_value;
105 const char *temp = retrieve(option_txt);
106 if (temp != nullptr) {
107 value = temp;
108 }
109 return value;
110 }
111 };
112} // 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:92
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:80
std::string get_option_value(const char *option_txt, const std::string &default_value) const
Definition Ioss_GetLongOpt.h:102
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
Definition Ioss_GetLongOpt.h:31