libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_cmd_parser.h
Go to the documentation of this file.
1/*! \file
2 * \brief Modular command-line argument parser
3 * \ingroup utils
4 *
5 * \todo More descriptive errors, help and version options,
6 * use virtual methods for customization points,
7 * implement signals for events (probably),
8 * process(..) top-level method for handling exiting
9 * upon failure, version, or help.
10 *
11 * \author 2023 Falco Girgis
12 * \copyright MIT License
13 */
14#ifndef GIMBAL_CMD_PARSER_H
15#define GIMBAL_CMD_PARSER_H
16
18
19/*! \name Type System
20 * \brief Type UUID and cast operators
21 * @{
22 */
23#define GBL_CMD_PARSER_TYPE (GBL_TYPEID(GblCmdParser)) //!< Type UUID of GblCmdParser
24#define GBL_CMD_PARSER(self) (GBL_CAST(GblCmdParser, self)) //!< Function-style GblInstance cast
25#define GBL_CMD_PARSER_CLASS(klass) (GBL_CLASS_CAST(GblCmdParser, klass)) //!< Function-style GblClass cast
26#define GBL_CMD_PARSER_GET_CLASS(self) (GBL_CLASSOF(GblCmdParser, self)) //!< Get a GblCmdParserClass from GblInstance
27//! @}
28
29#define GBL_SELF_TYPE GblCmdParser
30
32
33GBL_FORWARD_DECLARE_STRUCT(GblCmdParser);
34
35//! Attributes of a single command-line argument
36typedef struct GblCmdArg {
37 GblStringRef* pName; //!< Name of the command-line argument
38 GblStringRef* pDesc; //!< Description of the command-line argument
39} GblCmdArg;
40
41/*! \struct GblCmdParserClass
42 * \extends GblObjectClass
43 * \brief GblClass for GblCmdParser
44 *
45 * GblCmdParserClass offers a polymorphic virtual table
46 * for processing command-line arguments.
47 *
48 * \sa GblCmdParser
49 */
50GBL_CLASS_DERIVE(GblCmdParser, GblObject)
51 //! Parses the command given by \p cmd and returns a GBL_RESULT code
52 GBL_RESULT (*pFnParse) (GBL_SELF,
53 GblStringView cmd);
54 //! Checks to see if unknown command-line option values can be used, returning number used
55 GBL_RESULT (*pFnTryUnknownOption)(GBL_SELF,
56 GblStringView key,
57 GblStringView value,
58 size_t* pUsed);
60
61/*! \struct GblCmdParser
62 * \extends GblObject
63 * \ingroup utils
64 * \brief General-purpose command-line argument parser
65 *
66 * GblCmdParser is a configurable, modular, generalized
67 * parser for processing command-line arguments. A
68 * parser may contain a single "main" option group and
69 * a collection of secondary option groups.
70 * This modularity allows each library or framework
71 * within an application to add its own individual option
72 * group to the main command parser.
73 *
74 * \sa GblOptionGroup
75 */
77 GblStringRef* pErrorMsg; //!< Current, pending error message
78 GBL_RESULT parseResult; //!< Result of last parse operation
79 uint8_t allowExtraArgs : 1; //!< Toggle for allowing extra positional arguments beyond the ones added
80 uint8_t allowUnknownOptions : 1; //!< Toggle for allowing unknown options not handled by a GblCmdOptionGroup
81 uint8_t firstArgAsExecutable : 1; //!< Toggle for determining whether to treat the first argument as the executable name
82 uint8_t enableVersionOption : 1; //!< Toggle for optionally enabling a version command-line option
83 uint8_t enableHelpOption : 1; //!< Toggle for optionally enabling an autogenerated help option
85
86//! \cond
87GBL_PROPERTIES(GblCmdParser,
88 (allowExtraArgs, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
89 (allowUnknownOptions, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
90 (firstArgAsExecutable, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
91 (enableVersionOption, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
92 (enableHelpOption, GBL_GENERIC, (READ, WRITE), GBL_BOOL_TYPE),
93 (mainOptionGroup, GBL_GENERIC, (READ, WRITE), GBL_POINTER_TYPE),
94 (optionGroups, GBL_GENERIC, (READ, WRITE), GBL_POINTER_TYPE),
95 (positionalArgs, GBL_GENERIC, (READ, WRITE), GBL_POINTER_TYPE)
96)
97
98GBL_SIGNALS(GblCmdParser,
99 (parsePrePass, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pInitialArgs)),
100 (parsePostPass, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pRemainingArgs)),
101 (parseExtraArgs, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pExtraArgs)),
102 (parseUnknownOptions, (GBL_INSTANCE_TYPE, pReceiver), (GBL_POINTER_TYPE, pRemaininArgs)),
103 (parseError, (GBL_INSTANCE_TYPE, pReceiver), (GBL_ENUM_TYPE, errorCode))
104)
105//! \endcond
106
107//! Returns the GblType UUID associated with GblCmdParser
109
110/*! \name Lifetime Management
111 * \brief Methods for controlling lifetime of a GblCmdParser
112 * @{
113 */
114//! Creates a GblCmdParser object, initializes it, and returns a pointer to it
116//! Unreferences a GblCmdParser, destroying it if it's the last one, returning the remaining reference count
118//! @}
119
120/*! \name Option Groups
121 * \brief Methods for managing GblCmdOptionGroup subobjects
122 * \relatesalso GblCmdParser
123 * @{
124 */
125//! Adds the GblOptionGroup \p pGroup to the list of option groups handled by the GblCmdParser
127 GblOptionGroup* pGroup) GBL_NOEXCEPT;
128//! Sets the internal array of option groups to the list of pointers given by the \p ppGroups array, NULL-terminated
130 GblOptionGroup** ppGroups) GBL_NOEXCEPT;
131//! Returns a pointer to the GblOptionGroup at the given index
133//! Finds a GblOptionGroup from the internal list by its GblObject::name.
135 const char* pName) GBL_NOEXCEPT;
136//! Returns the number of option groups held within the internal list of them
138//! Sets the main option group for the parser to the one given by \p pGroup, returning the result
140 GblOptionGroup* pGroup) GBL_NOEXCEPT;
141//! Returns the GblOptionGroup previously set as the main option group for the GblCmdParser
143//! @}
144
145/*! \name Positional Arguments
146 * \brief Methods for managing positional arguments
147 * \relatesalso GblCmdParser
148 * @{
149 */
150//! Dynamically creates a GblCmdArg struct from the given args and adds it to the internal list
152 const char* pName,
153 const char* pDesc) GBL_NOEXCEPT;
154//! Sets the 0-terminated list of arguments pointed to by \p pArgs as the new positional argument list
156 const GblCmdArg* pArgs) GBL_NOEXCEPT;
157//! Clears the currently held internal list of GblCmdArg structures, resetting its size to 0
159//! Returns the size of the internally managed list of GblCmdArg structures represengin command-line arguments
161//! Returns a pointer to the GblCmdArg structure in the internally maintained list at the given index
163//! After parsing, this function returns the number of positional arguments extracted
165//! After parsing, returns the GblStringList maintained internally, containing the extracted positional argument values
167//! After parsing, this function converts the value of the argument at the given \p index to the desired \p toType, copying its value to \p pData, and returning the result
169 size_t index,
170 GblType toType,
171 void* pData) GBL_NOEXCEPT;
172//! @}
173
174/*! \name Parsing
175 * \brief Methods for parsing arguments or managing parsed values
176 * @{
177 */
178//! After parsing, returns a GblStringRef to the name of the current executable (first positional argument)
180//! After parsing, retuns a GblStringList to the internally maintained list of unknown command-line options
182//! Parses the list of arguments given by \p pArgs, populating internal structures, and returning the result
185//! @}
186
188
189#undef GBL_SELF_TYPE
190
191#endif // GIMBAL_CMD_PARSER_H
#define GBL_CLASS_CAST(cType, klass)
GblCmdParser * GblCmdParser_create(void)
Creates a GblCmdParser object, initializes it, and returns a pointer to it.
GblRefCount GblCmdParser_unref(GblCmdParser *pSelf)
Unreferences a GblCmdParser, destroying it if it's the last one, returning the remaining reference co...
GblType GblCmdParser_type(void)
Returns the GblType UUID associated with GblCmdParser.
#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_CLASS_DERIVE(...)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_BOOL_TYPE
Builtin ID for boolean GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
#define GBL_SIGNALS(instanceStruct,...)
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.
Attributes of a single command-line argument.
GblStringRef * pName
Name of the command-line argument.
GblStringRef * pDesc
Description of the command-line argument.
General-purpose command-line argument parser.
size_t GblCmdParser_positionalArgValueCount(const GblCmdParser *pSelf)
After parsing, this function returns the number of positional arguments extracted.
const GblStringList * GblCmdParser_positionalArgValues(const GblCmdParser *pSelf)
After parsing, returns the GblStringList maintained internally, containing the extracted positional a...
const GblStringList * GblCmdParser_unknownOptions(const GblCmdParser *pSelf)
After parsing, retuns a GblStringList to the internally maintained list of unknown command-line optio...
GblOptionGroup * GblCmdParser_optionGroup(const GblCmdParser *pSelf, size_t idx)
Returns a pointer to the GblOptionGroup at the given index.
uint8_t firstArgAsExecutable
Toggle for determining whether to treat the first argument as the executable name.
GblOptionGroup * GblCmdParser_mainOptionGroup(const GblCmdParser *pSelf)
Returns the GblOptionGroup previously set as the main option group for the GblCmdParser.
GBL_RESULT GblCmdParser_addPositionalArg(GblCmdParser *pSelf, const char *pName, const char *pDesc)
Dynamically creates a GblCmdArg struct from the given args and adds it to the internal list.
GBL_RESULT GblCmdParser_parse(GblCmdParser *pSelf, GblStringList *pArgs)
Parses the list of arguments given by pArgs, populating internal structures, and returning the result...
GBL_RESULT GblCmdParser_positionalArgValue(const GblCmdParser *pSelf, size_t index, GblType toType, void *pData)
After parsing, this function converts the value of the argument at the given index to the desired toT...
GBL_RESULT GblCmdParser_setOptionGroups(GblCmdParser *pSelf, GblOptionGroup **ppGroups)
Sets the internal array of option groups to the list of pointers given by the ppGroups array,...
const GblStringRef * GblCmdParser_executable(const GblCmdParser *pSelf)
After parsing, returns a GblStringRef to the name of the current executable (first positional argumen...
size_t GblCmdParser_optionGroupCount(const GblCmdParser *pSelf)
Returns the number of option groups held within the internal list of them.
GBL_RESULT GblCmdParser_clearPositionalArgs(GblCmdParser *pSelf)
Clears the currently held internal list of GblCmdArg structures, resetting its size to 0.
GBL_RESULT parseResult
Result of last parse operation.
GBL_RESULT GblCmdParser_addOptionGroup(GblCmdParser *pSelf, GblOptionGroup *pGroup)
Adds the GblOptionGroup pGroup to the list of option groups handled by the GblCmdParser.
size_t GblCmdParser_positionalArgCount(const GblCmdParser *pSelf)
Returns the size of the internally managed list of GblCmdArg structures represengin command-line argu...
GblStringRef * pErrorMsg
Current, pending error message.
uint8_t allowExtraArgs
Toggle for allowing extra positional arguments beyond the ones added.
GBL_RESULT GblCmdParser_setPositionalArgs(GblCmdParser *pSelf, const GblCmdArg *pArgs)
Sets the 0-terminated list of arguments pointed to by pArgs as the new positional argument list.
GBL_RESULT GblCmdParser_setMainOptionGroup(GblCmdParser *pSelf, GblOptionGroup *pGroup)
Sets the main option group for the parser to the one given by pGroup, returning the result.
uint8_t enableVersionOption
Toggle for optionally enabling a version command-line option.
const GblCmdArg * GblCmdParser_positionalArg(const GblCmdParser *pSelf, size_t index)
Returns a pointer to the GblCmdArg structure in the internally maintained list at the given index.
uint8_t enableHelpOption
Toggle for optionally enabling an autogenerated help option.
uint8_t allowUnknownOptions
Toggle for allowing unknown options not handled by a GblCmdOptionGroup.
GblOptionGroup * GblCmdParser_findOptionGroup(const GblCmdParser *pSelf, const char *pName)
Finds a GblOptionGroup from the internal list by its GblObject::name.