111 const char *argument =
NULL;
115 if (
strncmp ( opttext, longopt->
name, optlen ) != 0 )
119 if ( opttext[optlen] ==
'=' ) {
120 argument = &opttext[ optlen + 1 ];
121 }
else if ( opttext[optlen] ) {
137 printf (
"Option \"%s\" requires an argument\n",
145 printf (
"Option \"%s\" takes no argument\n", longopt->
name );
151 optarg = (
char * ) argument;
152 if ( longopt->
flag ) {
153 *(longopt->
flag) = longopt->
val;
172 const char *opttext,
int shortopt,
175 const char *argument =
NULL;
178 if ( *opttext != shortopt )
200 if ( ( has_arg !=
no_argument ) && ( ! argument ) )
205 printf (
"Option \"%c\" requires an argument\n", shortopt );
211 optarg = (
char * ) argument;
230 int getopt_long (
int argc,
char *
const argv[],
const char *optstring,
231 const struct option *longopts,
int *longindex ) {
232 const char *opttext = argv[
optind];
233 const struct option *longopt;
243 if ( *(opttext++) !=
'-' )
247 if ( *(opttext++) ==
'-' ) {
249 if ( *opttext ==
'\0' ) {
253 for ( longopt = longopts ; longopt->
name ; longopt++ ) {
258 *longindex = ( longopt - longopts );
262 printf (
"Unrecognised option \"--%s\"\n", opttext );
270 while ( ( shortopt = *(optstring++) ) ) {
272 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.