singe/thirdparty/arg_parser/cmain.c

222 lines
8.6 KiB
C

/* Arg_parser - POSIX/GNU command-line argument parser. (C version)
Copyright (C) 2006-2026 Antonio Diaz Diaz.
This program is free software: you have unlimited permission to
copy, distribute, and modify it.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
Exit status: 0 for a normal exit, 1 for environmental problems
(file not found, invalid command-line options, I/O errors, etc), 2 to
indicate a corrupt or invalid input file, 3 for an internal consistency
error (e.g., bug) which caused carg_parser to panic.
*/
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "carg_parser.h"
static const char * const program_name = "carg_parser";
static const char * const program_year = "2026";
static const char * invocation_name = "carg_parser"; /* default value */
static unsigned verbosity = 0;
enum { opt_lo = 256 };
static void show_help( void )
{
fputs(
"Arg_parser - POSIX/GNU command-line argument parser. (C version)\n"
"\nArg_parser is an argument parser that follows POSIX and GNU conventions for\n"
"command-line arguments. There exist C++ and C versions of Arg_parser. The\n"
"C++ version is implemented as a C++ class, while the C version is\n"
"implemented as a single struct plus associated functions. Both are simpler,\n"
"easier to use, and safer than 'getopt_long'.\n"
"\nFor maximum stability, Arg_parser is self-contained. It extracts all the\n"
"information it needs from its arguments to avoid referring to them later.\n"
"This avoids index-out-of-bounds errors and allows the parser object to be\n"
"passed as argument to other functions for further analysis.\n"
"\nArg_parser does not modify its arguments (argc, argv), nor uses any global\n"
"variables.\n"
"\nThe C++ version of Arg_parser can also parse options from configuration\n"
"files.\n"
"\nThe C version of Arg_parser is provided in the files 'carg_parser.h' and\n"
"'carg_parser.c'. To learn how to use Arg_parser in your C programs, see the\n"
"C example in the manual and the example file 'cmain.c' in the source\n"
"tarball.\n", stdout );
printf( "\nUsage: %s [options]\n", invocation_name );
fputs( "\nOptions:\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
" -a, --append example of option with no argument\n"
" -b, --block=<arg> example of option with required argument\n"
" -c, --casual[=<arg>] example of option with optional argument\n"
" -e, --empty=<arg> example of option with maybe empty argument\n"
" -o <arg> example of short only option\n"
" --long-only example of long only option\n"
" -q, --quiet quiet operation\n"
" -u, --uncaught example of intentional bug\n"
" -v, --verbose verbose operation\n", stdout );
if( verbosity ) fputs(
" -H, --hidden example of hidden option (shown with -v -h)\n", stdout );
fputs(
"\nReport bugs to arg-parser-bug@nongnu.org\n"
"Arg_parser home page: http://www.nongnu.org/arg-parser/arg_parser.html\n", stdout );
}
static void show_version( void )
{
printf( "%s %s\n", program_name, PROGVERSION );
printf( "Copyright (C) %s Antonio Diaz Diaz.\n", program_year );
fputs( "License 2-clause BSD.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n", stdout );
}
static void show_error( const char * const msg, const int errcode,
const char help )
{
if( msg && msg[0] )
fprintf( stderr, "%s: %s%s%s\n", program_name, msg,
( errcode > 0 ) ? ": " : "",
( errcode > 0 ) ? strerror( errcode ) : "" );
if( help )
fprintf( stderr, "Try '%s --help' for more information.\n",
invocation_name );
}
static void internal_error( const char * const msg )
{
fprintf( stderr, "%s: internal error: %s\n", program_name, msg );
exit( 3 );
}
static void print_opt( const char * const arg, const char * const option_name,
const int code )
{
printf( "option '%s'", option_name );
if( arg[0] || code == 'e' ) printf( " with argument '%s'", arg );
if( code == INT_MIN || code == INT_MAX ) printf( " (code 0x%X)", code );
fputc( '\n', stdout );
}
static char process_options( const Arg_parser * const parserp )
{
int argind;
verbosity = 0;
for( argind = 0; argind < ap_arguments( parserp ); ++argind )
{
const int code = ap_code( parserp, argind );
const char * const pn = ap_parsed_name( parserp, argind );
const char * const arg = ap_argument( parserp, argind );
switch( code )
{
case 0: printf( "non-option argument '%s'\n", arg ); break;
case '.': case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c':
case 'e': print_opt( arg, pn, code ); break;
case 'h': show_help(); return 0;
case 'H': case 'i': case 'I':
case 'o': print_opt( arg, pn, code ); break;
case 'q': print_opt( arg, pn, code ); verbosity = 0; break;
/* case 'u': break; */ /* intentionally not caught */
case 'v': print_opt( arg, pn, code ); ++verbosity; break;
case 'V': show_version(); return 0;
case opt_lo:
case INT_MIN: case INT_MAX: print_opt( arg, pn, code ); break;
default: internal_error( "uncaught option." );
}
} /* end process arguments */
return 1;
}
int main( const int argc, const char * const argv[] )
{
const ap_Option options[] =
{
/* code, long_name, has_arg (no/yes/maybe/yesme) */
{ '.', 0, ap_maybe },
{ '0', 0, ap_maybe },
{ '1', 0, ap_maybe },
{ '2', 0, ap_maybe },
{ '3', 0, ap_maybe },
{ '4', 0, ap_maybe },
{ '5', 0, ap_maybe },
{ '6', 0, ap_maybe },
{ '7', 0, ap_maybe },
{ '8', 0, ap_maybe },
{ '9', 0, ap_maybe },
{ 'a', "append", ap_no },
{ 'b', "block", ap_yes },
{ 'c', "casual", ap_maybe },
{ 'e', "empty", ap_yesme },
{ 'h', "help", ap_no },
{ 'H', "hidden", ap_no },
{ 'i', 0, ap_maybe },
{ 'I', 0, ap_maybe },
{ 'o', 0, ap_yes },
{ 'q', "quiet", ap_no },
{ 'u', "uncaught", ap_no },
{ 'v', "verbose", ap_no },
{ 'V', "version", ap_no },
{ opt_lo, "long-only", ap_no },
{ INT_MIN, "int-min", ap_no },
{ INT_MAX, "int-max", ap_no },
{ 0, 0, ap_no } };
Arg_parser parser;
int flags;
if( argc > 0 ) invocation_name = argv[0];
if( !ap_init( &parser, argc, argv, options, 0 ) )
{ show_error( "Not enough memory.", 0, 0 ); return 1; }
if( ap_error( &parser ) ) /* bad option */
{ show_error( ap_error( &parser ), 0, 1 ); return 1; }
if( !process_options( &parser ) ) return 0;
fputs( !ap_arguments( &parser ) ?
"No arguments!\n" : "(options reordered)\n", stdout );
if( verbosity ) fputc( '\n', stdout ); else return 0;
if( !ap_init( &parser, argc, argv, options, ap_neg_non_opt ) )
{ show_error( "Not enough memory.", 0, 0 ); return 1; }
if( ap_error( &parser ) ) /* bad option */
{ show_error( ap_error( &parser ), 0, 1 ); return 1; }
process_options( &parser );
fputs( "(options reordered"
" and negative numbers treated as non-options)\n", stdout );
if( verbosity == 1 ) return 0;
for( flags = 1; flags < 16; ++flags )
{
int i;
if( flags == ap_neg_non_opt ) continue; /* done above */
fputc( '\n', stdout );
if( !ap_init( &parser, argc, argv, options, flags ) )
{ show_error( "Not enough memory.", 0, 0 ); return 1; }
if( ap_error( &parser ) ) /* bad option */
{ show_error( ap_error( &parser ), 0, 1 ); return 1; }
process_options( &parser );
for( i = ap_argv_index( &parser ); i < argc; ++i )
printf( "skipped non-option argument '%s'\n", argv[i] );
printf( "(0%s%s%s%s)\n",
(flags & ap_in_order) ? " | in_order" : "",
(flags & ap_in_order_stop) ? " | in_order_stop" : "",
(flags & ap_in_order_skip) ? " | in_order_skip" : "",
(flags & ap_neg_non_opt) ? " | neg_non_opt" : "" );
}
ap_free( &parser );
return 0;
}