Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
Loading...
Searching...
No Matches
Args.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, Cascoda Ltd.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef POSIX_APP_CHILICTL_COMMON_ARGS_H_
30#define POSIX_APP_CHILICTL_COMMON_ARGS_H_
31
32#include <functional>
33#include <limits>
34#include <stdio.h>
35#include <vector>
36
37#include "ca821x_error.h"
38
39namespace ca {
40
44class ArgOpt
45{
46 friend class Args;
47 using argCallback = std::function<ca_error(const char *)>;
48 template <class T> using argCallbackMemRaw = ca_error (T::*)(const char *argument);
49
50public:
60
67 ArgOpt(char aShortArg, const char *aLongArg, ArgPerm aArgPerm = ArgPerm::NO_ARG);
68
73 void SetHelpString(const char *aHelpString) { helpstr = aHelpString; }
74
79 void SetArgHint(const char *aArgHint) { arghint = aArgHint; }
80
85 void SetCallback(argCallback aCallback) { callback = aCallback; }
86
92 template <class T> void SetCallback(argCallbackMemRaw<T> aCallback, T &aInstance)
93 {
94 callback = std::bind(aCallback, &aInstance, std::placeholders::_1);
95 }
96
101 int GetCallCount(void) { return count; }
102
103private:
104 char shortarg;
105 const char *longarg;
106 const char *helpstr;
107 const char *arghint;
108 bool optional_arg;
109 bool mandatory_arg;
110 argCallback callback;
111
112 int count;
113
119 ca_error call_callback(const char *aArg)
120 {
121 count++;
122 if (callback)
123 return callback(aArg);
124 return CA_ERROR_SUCCESS;
125 }
126};
127
132class Args
133{
134public:
139 : mFinalOpt(std::numeric_limits<int>::max())
140 , mOptions(){};
141
161 ca_error ProcessOption(int &argi, int argc, const char *argv[]);
162
168 void PrintOptionHelpStrings(FILE *aOutFile);
169
178 void AddOption(ArgOpt &aOption) { mOptions.push_back(&aOption); }
179
180private:
181 int mFinalOpt;
182 std::vector<ArgOpt *> mOptions;
183
189 static bool is_shortopt(const char *aStr);
190
196 static bool is_longopt(const char *aStr);
197
203 static bool is_end_of_opts(const char *aStr);
204
210 static bool is_opt(const char *aStr);
211
223 ca_error process_short_opts(int &argi, int argc, const char *argv[]);
224
236 ca_error process_long_opt(int &argi, int argc, const char *argv[]);
237
243 void print_longopt_error(const char *optptr, size_t optlen);
244
250 void print_opt_help_string(FILE *out, ArgOpt *opt);
251};
252
253} /* namespace ca */
254
255#endif /* POSIX_APP_CHILICTL_COMMON_ARGS_H_ */
Global error declarations for use across the Cascoda SDK.
Structure for holding the definition of potential options to pass on the command line.
Definition Args.hpp:45
ArgPerm
Enumeration for whether or not an option permits/requires arguments.
Definition Args.hpp:55
@ MANDATORY_ARG
Option requires an argument to be provided.
Definition Args.hpp:58
@ NO_ARG
No argument allowed to option.
Definition Args.hpp:56
@ OPTIONAL_ARG
Option has optional argument.
Definition Args.hpp:57
void SetCallback(argCallbackMemRaw< T > aCallback, T &aInstance)
Set the callback to be called when the option is detected.
Definition Args.hpp:92
void SetArgHint(const char *aArgHint)
Set the short argument hint string, describing the argument (eg "yes|no", "path", "value)
Definition Args.hpp:79
void SetHelpString(const char *aHelpString)
Set the help string of the option.
Definition Args.hpp:73
int GetCallCount(void)
Get the number of times this option was detected during parsing.
Definition Args.hpp:101
void SetCallback(argCallback aCallback)
Set the callback to be called when the option is detected.
Definition Args.hpp:85
Class for parsing and handling options passed as command line arguments.
Definition Args.hpp:133
Args()
Default constructor, initialises empty arg parser.
Definition Args.hpp:138
ca_error ProcessOption(int &argi, int argc, const char *argv[])
Process a single option from argv using the provided ArgOpt structures.
Definition Args.cpp:52
void PrintOptionHelpStrings(FILE *aOutFile)
Print all of the option help strings to the 'out' file pointer in manpage format.
Definition Args.cpp:80
void AddOption(ArgOpt &aOption)
Add an option definition to this parser.
Definition Args.hpp:178
ca_error
Cascoda error type.
Definition ca821x_error.h:51
@ CA_ERROR_SUCCESS
Success.
Definition ca821x_error.h:53
Definition Args.cpp:34
uint8_t count
Definition serial-test.c:58