libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_option_group.h
Go to the documentation of this file.
1/*! \file
2 * \brief Grouping of parsable command-line options
3 * \ingroup utils
4 *
5 * This file contains the GblOptionGroup type and its
6 * associated API. It is used to create a standalone
7 * group of options that correspond to a single module
8 * or unit of code which can be added and parsed off of
9 * a larger, application-wide group of options.
10 *
11 * \author 2023 Falco Girgis
12 * \copyright MIT License
13 */
14#ifndef GIMBAL_OPTION_GROUP_H
15#define GIMBAL_OPTION_GROUP_H
16
17#include "../meta/instances/gimbal_object.h"
18#include "../meta/signals/gimbal_signal.h"
19#include "../strings/gimbal_string_list.h"
20#include "gimbal_version.h"
21
22/*! \name Type System
23 * \brief Type UUID and cast operators
24 * @{
25 */
26#define GBL_OPTION_GROUP_TYPE (GBL_TYPEID(GblOptionGroup)) //!< Type UUID for GblOptionGroup
27#define GBL_OPTION_GROUP(self) (GBL_CAST(GblOptionGroup, self)) //!< Casts a GblInstance to GblOptionGroup
28#define GBL_OPTION_GROUP_CLASS(klass) (GBL_CLASS_CAST(GblOptionGroup, klass)) //!< Casts a GblClass to GblOptionGroupClass
29#define GBL_OPTION_GROUP_GET_CLASS(self) (GBL_CLASSOF(GblOptionGroup, self)) //!< Gets a GblOptionGroupClass from a GblInstance
30//! @}
31
32#define GBL_SELF_TYPE GblOptionGroup
33
35
36GBL_FORWARD_DECLARE_STRUCT(GblOptionGroup);
38
39//! Callback function signature to be used with a GBL_OPTION_TYPE_CALLBACK GblOption
40typedef GBL_RESULT (*GblOptionCallbackFn)(GblOptionGroup* pGroup,
41 const GblOption* pOption,
42 GblStringView value,
43 GblBool* pConsumed);
44
45/*! \enum GBL_OPTION_FLAGS
46 * \brief GblOption flags for controlling special option behaviors
47 */
49 GBL_OPTION_FLAG_NONE = 0, //!< None
50 GBL_OPTION_FLAG_HIDDEN = 0x1, //!< Hidden (doesn't display in the --help text)
51 GBL_OPTION_FLAG_BOOL_INVERTED = 0x2, //!< Underlying boolean value is swapped from user input
52 GBL_OPTION_FLAG_BOOL_NO_VALUE = 0x4 //!< Bool option is enabled just by being present, without value
53};
54
55/*! \enum GBL_OPTION_TYPE
56 * \brief Represents the list of every supported type of GblOption value
57 */
59 GBL_OPTION_TYPE_STRING, //!< const char*
71 GBL_OPTION_TYPE_CALLBACK //!< GblOptionCallbackFn
72};
73
74//! Union for SAFELY storing either a data or callback pointer
75typedef union GblOptionPtr {
76 void* pData; //!< Data pointer of union
77 GblOptionCallbackFn pFn; //!< Callback pointer of union
78} GblOptionPtr;
79
80//! Describes a single command-line option along with handler info
81typedef struct GblOption {
82 const char* pLongName; //!< Long, hyphenated name of option
83 char shortName; //!< Shorthanded, single-character name of option (optional)
84 GBL_OPTION_TYPE type; //!< Data type of the option handler
85 GblOptionPtr pOutput; //!< Union for either data or callback pointer
86 const char* pDescription; //!< Help description of option
87 const char* pValueName; //!< Name of the value associated with the option
88 GblFlags flags; //!< Additional flags for the option
89} GblOption;
90
91/*! \struct GblOptionGroupClass
92 * \extends GblObjectClass
93 * \brief GblClass structure for GblOptionGroup
94 *
95 * GblOptionGroupClass provides polymorphic virtual
96 * functions for customizing the overall behavior of the
97 * parser as well as the behavior for parsing individual
98 * options.
99 *
100 * \sa GblOptionGroup
101 */
102GBL_CLASS_DERIVE(GblOptionGroup, GblObject)
103 //! Top-level virtual function for parsing out options and processing from a list
104 GBL_RESULT (*pFnParse)(GBL_SELF, GblStringList* pList);
105 //! Tries processing a single KV pair as an option, returning the # of args consumed (0-2)
106 GBL_RESULT (*pFnTry) (GBL_SELF, GblStringView key, GblStringView value, size_t *pUsed);
108
109/*! \struct GblOptionGroup
110 * \extends GblObject
111 * \brief Grouping of command-line options
112 * \ingroup utils
113 *
114 * GblOptionGroup represents a collection of configurable
115 * command-line options, which may be parsed directly or
116 * added to a top-level GblCmdParser. Options are divided
117 * into groups so that each module within an application
118 * can manage its own options independently.
119 *
120 * \sa GblCmdParser
121 */
123 GblOption* pOptions; //!< Array of GblOption entries
124 size_t optionCount; //!< Number of options within pOptions
125 GblStringRef* pPrefix; //!< Prefix for all options within this group
126 GblVersion version; //!< Version number for the module/unit represented by this group
127 GblStringRef* pSummary; //!< Summary of the module/unit represented by the option group
128 GblStringRef* pDescription; //!< Longer description of the module/unit represented by the option group
129 GblStringList* pParsedArgs; //!< List of all options/values which have been extracted and parsed
130 GBL_RESULT parseResult; //!< Result of GblOptionGroup_parse()
132
133//! \cond
134GBL_PROPERTIES(GblOptionGroup,
135 (name, GBL_GENERIC, (READ, CONSTRUCT, OVERRIDE), GBL_STRING_TYPE),
136 (prefix, GBL_GENERIC, (READ, CONSTRUCT), GBL_STRING_TYPE),
137 (options, GBL_GENERIC, (READ, CONSTRUCT), GBL_POINTER_TYPE),
138 (version, GBL_GENERIC, (READ, WRITE), GBL_UINT32_TYPE),
139 (summary, GBL_GENERIC, (READ, WRITE), GBL_STRING_TYPE),
140 (description, GBL_GENERIC, (READ, WRITE), GBL_STRING_TYPE)
141)
142
143GBL_SIGNALS(GblOptionGroup,
144 (parsePrePass, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pStringList)),
145 (parsePostPass, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pStringList)),
146 (parseError, (GBL_INSTANCE_TYPE, pReceiver), (GBL_ENUM_TYPE, errorCode))
147)
148//! \endcond
149
150//! Returns the GblType UUID associated with GblOptionGroup
152//! Creates a GblOptionGroup with an option name, prefix, and NULL-terminated option list
153GBL_EXPORT GblOptionGroup* GblOptionGroup_create (const char* pName,
154 const char* pPrefix,
155 const GblOption* pOptions) GBL_NOEXCEPT;
156//! Decrements the reference counter of the given GblOptionGroup by 1, destructing when it hits 0
158//! Processes the given string list, optionally requiring prefixes on all options
160 GblStringList* pList,
161 GblBool prefixOnly/*=false*/) GBL_NOEXCEPT;
162
164
165//! \cond
166#define GblOptionGroup_parse(...)
167 GblOptionGroup_parseDefault_(__VA_ARGS__, GBL_FALSE)
168#define GblOptionGroup_parseDefault_(self, list, prefix, ...)
169 (GblOptionGroup_parse)(self, list, prefix)
170//! \endcond
171
172#undef GBL_SELF_TYPE
173
174#endif // GIMBAL_OPTION_GROUP_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_DECLARE_ENUM(E)
#define GBL_CLASS_DERIVE(...)
#define GBL_DECLARE_FLAGS(F)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
GBL_RESULT GblOptionGroup_parse(GblOptionGroup *pSelf, GblStringList *pList, GblBool prefixOnly)
Processes the given string list, optionally requiring prefixes on all options.
GBL_OPTION_TYPE
Represents the list of every supported type of GblOption value.
@ GBL_OPTION_TYPE_UINT16
uint16_t
@ GBL_OPTION_TYPE_CALLBACK
GblOptionCallbackFn.
@ GBL_OPTION_TYPE_INT16
int16_t
@ GBL_OPTION_TYPE_UINT32
uint32_t
@ GBL_OPTION_TYPE_INT64
int64_t
@ GBL_OPTION_TYPE_BOOL
GblBool.
@ GBL_OPTION_TYPE_CHAR
char
@ GBL_OPTION_TYPE_UINT64
uint64_t
@ GBL_OPTION_TYPE_FLOAT
float
@ GBL_OPTION_TYPE_INT32
int32_t
@ GBL_OPTION_TYPE_DOUBLE
double
@ GBL_OPTION_TYPE_UINT8
uint8_t
@ GBL_OPTION_TYPE_STRING
const char*
GBL_OPTION_FLAGS
GblOption flags for controlling special option behaviors.
@ GBL_OPTION_FLAG_NONE
None.
@ GBL_OPTION_FLAG_BOOL_INVERTED
Underlying boolean value is swapped from user input.
@ GBL_OPTION_FLAG_HIDDEN
Hidden (doesn't display in the –help text)
@ GBL_OPTION_FLAG_BOOL_NO_VALUE
Bool option is enabled just by being present, without value.
GblType GblOptionGroup_type(void)
Returns the GblType UUID associated with GblOptionGroup.
GblOptionGroup * GblOptionGroup_create(const char *pName, const char *pPrefix, const GblOption *pOptions)
Creates a GblOptionGroup with an option name, prefix, and NULL-terminated option list.
GblRefCount GblOptionGroup_unref(GblOptionGroup *pSelf)
Decrements the reference counter of the given GblOptionGroup by 1, destructing when it hits 0.
#define GBL_STRING_TYPE
Builtin ID for string GblVariant type.
#define GBL_UINT32_TYPE
Builtin ID for uint32_t GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
#define GBL_SIGNALS(instanceStruct,...)
#define GBL_FALSE
uint32_t GblFlags
Standard-sized flags type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
#define GBL_POINTER_TYPE
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:51
GblRingList GblStringList
List of strings with array-like API.
const char GblStringRef
Reference-counted, const char*-compatible string type.
uint32_t GblVersion
32-bit unsigned integer representing a packed version in the form (MAJOR.MINOR.PATCH)
Grouping of command-line options.
GblStringList * pParsedArgs
List of all options/values which have been extracted and parsed.
GblVersion version
Version number for the module/unit represented by this group.
GblOption * pOptions
Array of GblOption entries.
size_t optionCount
Number of options within pOptions.
GblStringRef * pSummary
Summary of the module/unit represented by the option group.
GBL_RESULT parseResult
Result of GblOptionGroup_parse()
GblStringRef * pPrefix
Prefix for all options within this group.
GblStringRef * pDescription
Longer description of the module/unit represented by the option group.
Describes a single command-line option along with handler info.
GblFlags flags
Additional flags for the option.
char shortName
Shorthanded, single-character name of option (optional)
const char * pLongName
Long, hyphenated name of option.
const char * pDescription
Help description of option.
GblOptionPtr pOutput
Union for either data or callback pointer.
const char * pValueName
Name of the value associated with the option.
GBL_OPTION_TYPE type
Data type of the option handler.
Union for SAFELY storing either a data or callback pointer.
void * pData
Data pointer of union.
GblOptionCallbackFn pFn
Callback pointer of union.