nlib
Nflags.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_NFLAGS_H_
4 #define INCLUDE_NN_NLIB_NFLAGS_H_
5 
6 #include "nn/nlib/Config.h"
7 #include "nn/nlib/UniquePtr.h"
8 
9 NLIB_NAMESPACE_BEGIN
10 
11 // You can define command line switches by using the macros:
12 // NLIB_FLAGS_DEFINE_[bool|int32|double|string](option_name, default value, "help message");
13 // Then Nflags::Parse() and you can access the specified value through NLIB_FLAGS_option_name.
14 // See misc/nflags/nflags.cpp sample codes for detailed usage.
16  public:
17  static errno_t Parse(int* argc, char*** argv) NLIB_NOEXCEPT NLIB_NONNULL;
18  static errno_t Parse(int* argc, wchar_t*** argv) NLIB_NOEXCEPT NLIB_NONNULL;
19  // use like this:
20  // if (NLIB_FLAGS_help) { Nflags::PrintHelp(); }
21  static void PrintHelp() NLIB_NOEXCEPT;
22  static const char* GetErrorMessage() NLIB_NOEXCEPT;
23 
24  static const int kMaxPath = 512;
25  static const int kMaxFileName = 256;
26  static const int kMaxDirName = 512;
27  static const int kMaxExtension = 256;
28  static errno_t GetFileName(const char* path,
29  char (&filename)[kMaxFileName]) NLIB_NOEXCEPT NLIB_NONNULL;
30  static errno_t GetDirName(const char* path,
31  char (&dirname)[kMaxDirName]) NLIB_NOEXCEPT NLIB_NONNULL;
32 
33  static errno_t GetStringCommaList(char* arg, char** vec, size_t vec_count,
34  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
35  template <size_t N>
36  static errno_t
37  GetStringCommaList(char* arg, char* (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
38  return GetStringCommaList(arg, &vec[0], N, written_count);
39  }
40 
41  static errno_t GetInt32CommaList(char* arg, int32_t* vec, size_t vec_count,
42  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
43  template <size_t N>
44  static errno_t
45  GetInt32CommaList(char* arg, int32_t (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
46  return GetInt32CommaList(arg, &vec[0], N, written_count);
47  }
48 
49  static errno_t GetDoubleCommaList(char* arg, double* vec, size_t vec_count,
50  size_t* written_count) NLIB_NOEXCEPT NLIB_NONNULL;
51  template <size_t N>
52  static errno_t
53  GetDoubleCommaList(char* arg, double (&vec)[N], size_t* written_count) NLIB_NOEXCEPT {
54  return GetDoubleCommaList(arg, &vec[0], N, written_count);
55  }
56 
57  // 1,t,y,yes,true -> true / 0,f,n,no,false -> false / otherwise -> defval
58  static bool GetBoolFromEnv(const char* varname, bool defval) NLIB_NOEXCEPT;
59  static int32_t GetInt32FromEnv(const char* varname, int32_t defval) NLIB_NOEXCEPT;
60  static int64_t GetInt64FromEnv(const char* varname, int64_t defval) NLIB_NOEXCEPT;
61  static double GetDoubleFromEnv(const char* varname, double defval) NLIB_NOEXCEPT;
62  // do not delete the return value.
63  static const char* GetStringFromEnv(UniquePtr<char[]>& buf, // NOLINT
64  const char* varname, const char* defval) NLIB_NOEXCEPT;
65 
66  private:
67  Nflags();
68 };
69 
70 namespace nflags {
71 
72 NLIB_VIS_PUBLIC void AddDefOptBool(const char* opt_name, bool def_value, const char* desc,
73  bool* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
74 NLIB_VIS_PUBLIC void AddDefOptInt32(const char* opt_name, int32_t def_value, const char* desc,
75  int32_t* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
76 NLIB_VIS_PUBLIC void AddDefOptDouble(const char* opt_name, double def_value, const char* desc,
77  double* ptr) NLIB_NOEXCEPT NLIB_NONNULL;
78 NLIB_VIS_PUBLIC void AddDefOptString(const char* opt_name, const char* def_value, const char* desc,
79  const char** ptr) NLIB_NOEXCEPT NLIB_NONNULL;
80 
81 } // namespace nflags
82 
83 #define NLIB_FLAGS_DEFINE_bool(opt_name, def_value, desc) \
84 bool NLIB_FLAGS_##opt_name; \
85 class NLIB_FLAGS_CL_##opt_name { \
86  public: /* NOLINT */ \
87  NLIB_FLAGS_CL_##opt_name() { \
88  ::nlib_ns::nflags::AddDefOptBool(#opt_name, def_value, desc, &NLIB_FLAGS_##opt_name); \
89  } \
90 } NLIB_FLAGS_CL_##opt_name##_
91 
92 #define NLIB_FLAGS_DEFINE_int32(opt_name, def_value, desc) \
93 int32_t NLIB_FLAGS_##opt_name; \
94 class NLIB_FLAGS_CL_##opt_name { \
95  public: /* NOLINT */ \
96  NLIB_FLAGS_CL_##opt_name() { \
97  ::nlib_ns::nflags::AddDefOptInt32(#opt_name, def_value, desc, \
98  &NLIB_FLAGS_##opt_name); \
99  } \
100 } NLIB_FLAGS_CL_##opt_name##_
101 
102 #define NLIB_FLAGS_DEFINE_double(opt_name, def_value, desc) \
103 double NLIB_FLAGS_##opt_name; \
104 class NLIB_FLAGS_CL_##opt_name { \
105  public: /* NOLINT */ \
106  NLIB_FLAGS_CL_##opt_name() { \
107  ::nlib_ns::nflags::AddDefOptDouble(#opt_name, def_value, desc, \
108  &NLIB_FLAGS_##opt_name); \
109  } \
110 } NLIB_FLAGS_CL_##opt_name##_
111 
112 #define NLIB_FLAGS_DEFINE_string(opt_name, def_value, desc) \
113 const char* NLIB_FLAGS_##opt_name; \
114 class NLIB_FLAGS_CL_##opt_name { \
115  public: /* NOLINT */ \
116  NLIB_FLAGS_CL_##opt_name() { \
117  ::nlib_ns::nflags::AddDefOptString(#opt_name, def_value, desc, \
118  &NLIB_FLAGS_##opt_name); \
119  } \
120 } NLIB_FLAGS_CL_##opt_name##_
121 
122 #define NLIB_FLAGS_DECLARE_bool(opt_name) extern bool NLIB_FLAGS_##opt_name
123 #define NLIB_FLAGS_DECLARE_int32(opt_name) extern int32_t NLIB_FLAGS_##opt_name
124 #define NLIB_FLAGS_DECLARE_double(opt_name) extern double NLIB_FLAGS_##opt_name
125 #define NLIB_FLAGS_DECLARE_string(opt_name) extern const char* NLIB_FLAGS_##opt_name
126 
127 NLIB_NAMESPACE_END
128 
129 // Predefined option:
130 // NLIB_FLAGS_help : set with '--help'
132 
133 #endif // INCLUDE_NN_NLIB_NFLAGS_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
static errno_t GetDoubleCommaList(char *arg, double(&vec)[N], size_t *written_count) noexcept
The function template version of GetDoubleCommaList.
Definition: Nflags.h:53
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
static errno_t GetStringCommaList(char *arg, char *(&vec)[N], size_t *written_count) noexcept
The function template version of GetStringCommaList.
Definition: Nflags.h:37
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
#define NLIB_FLAGS_DECLARE_bool(opt_name)
Enables the use of an NLIB_FLAGS_option name that was defined in a different place.
Definition: Nflags.h:122
The class for parsing command line flags.
Definition: Nflags.h:15
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
static errno_t GetInt32CommaList(char *arg, int32_t(&vec)[N], size_t *written_count) noexcept
The function template version of GetInt32CommaList.
Definition: Nflags.h:45
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24