110 const char *argument =
NULL;
114 if (
strncmp ( opttext, longopt->
name, optlen ) != 0 )
118 if ( opttext[optlen] ==
'=' ) {
119 argument = &opttext[ optlen + 1 ];
120 }
else if ( opttext[optlen] ) {
136 printf (
"Option \"%s\" requires an argument\n",
144 printf (
"Option \"%s\" takes no argument\n", longopt->
name );
150 optarg = (
char * ) argument;
151 if ( longopt->
flag ) {
152 *(longopt->
flag) = longopt->
val;
171 const char *opttext,
int shortopt,
174 const char *argument =
NULL;
177 if ( *opttext != shortopt )
199 if ( ( has_arg !=
no_argument ) && ( ! argument ) )
204 printf (
"Option \"%c\" requires an argument\n", shortopt );
210 optarg = (
char * ) argument;
229 int getopt_long (
int argc,
char *
const argv[],
const char *optstring,
230 const struct option *longopts,
int *longindex ) {
231 const char *opttext = argv[
optind];
232 const struct option *longopt;
242 if ( *(opttext++) !=
'-' )
246 if ( *(opttext++) ==
'-' ) {
248 if ( *opttext ==
'\0' ) {
252 for ( longopt = longopts ; longopt->
name ; longopt++ ) {
257 *longindex = ( longopt - longopts );
261 printf (
"Unrecognised option \"--%s\"\n", opttext );
269 while ( ( shortopt = *(optstring++) ) ) {
271 while ( *optstring ==
':' ) {
static const char * get_argv_argument(int argc, char *const argv[])
Get option argument from argv[] array.
int nextchar
Current option character index.
int printf(const char *fmt,...)
Write a formatted string to the console.
int optopt
Unrecognised option.
int optind
Current option index.
int strncmp(const char *first, const char *second, size_t max)
Compare strings.
Parse command-line options.
A long option, as used for getopt_long()
static int match_short_option(int argc, char *const argv[], const char *opttext, int shortopt, enum getopt_argument_requirement has_arg, int *option)
Match short option.
getopt_argument_requirement
const char * name
Long name of this option.
size_t strlen(const char *src)
Get length of string.
Option does not take an argument.
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Parse command-line options.
int * flag
Location into which to store val, or NULL.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Option requires an argument.
char * optarg
Option argument.
int has_arg
Option takes an argument.
#define NULL
NULL pointer (VOID *)
static int match_long_option(int argc, char *const argv[], const char *opttext, const struct option *longopt, int *option)
Match long option.