"Fossies" - the Fresh Open Source Software Archive

Member "lzip-1.22-rc2/arg_parser.h" (30 Apr 2020, 3580 Bytes) of package /linux/misc/lzip-1.22-rc2.tar.lz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "arg_parser.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_vs_1.22-rc1.

    1 /* Arg_parser - POSIX/GNU command line argument parser. (C++ version)
    2    Copyright (C) 2006-2020 Antonio Diaz Diaz.
    3 
    4    This library is free software. Redistribution and use in source and
    5    binary forms, with or without modification, are permitted provided
    6    that the following conditions are met:
    7 
    8    1. Redistributions of source code must retain the above copyright
    9    notice, this list of conditions, and the following disclaimer.
   10 
   11    2. Redistributions in binary form must reproduce the above copyright
   12    notice, this list of conditions, and the following disclaimer in the
   13    documentation and/or other materials provided with the distribution.
   14 
   15    This library is distributed in the hope that it will be useful,
   16    but WITHOUT ANY WARRANTY; without even the implied warranty of
   17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   18 */
   19 
   20 /* Arg_parser reads the arguments in 'argv' and creates a number of
   21    option codes, option arguments, and non-option arguments.
   22 
   23    In case of error, 'error' returns a non-empty error message.
   24 
   25    'options' is an array of 'struct Option' terminated by an element
   26    containing a code which is zero. A null name means a short-only
   27    option. A code value outside the unsigned char range means a
   28    long-only option.
   29 
   30    Arg_parser normally makes it appear as if all the option arguments
   31    were specified before all the non-option arguments for the purposes
   32    of parsing, even if the user of your program intermixed option and
   33    non-option arguments. If you want the arguments in the exact order
   34    the user typed them, call 'Arg_parser' with 'in_order' = true.
   35 
   36    The argument '--' terminates all options; any following arguments are
   37    treated as non-option arguments, even if they begin with a hyphen.
   38 
   39    The syntax for optional option arguments is '-<short_option><argument>'
   40    (without whitespace), or '--<long_option>=<argument>'.
   41 */
   42 
   43 class Arg_parser
   44   {
   45 public:
   46   enum Has_arg { no, yes, maybe };
   47 
   48   struct Option
   49     {
   50     int code;           // Short option letter or code ( code != 0 )
   51     const char * name;      // Long option name (maybe null)
   52     Has_arg has_arg;
   53     };
   54 
   55 private:
   56   struct Record
   57     {
   58     int code;
   59     std::string argument;
   60     explicit Record( const int c ) : code( c ) {}
   61     explicit Record( const char * const arg ) : code( 0 ), argument( arg ) {}
   62     };
   63 
   64   const std::string empty_arg;
   65   std::string error_;
   66   std::vector< Record > data;
   67 
   68   bool parse_long_option( const char * const opt, const char * const arg,
   69                           const Option options[], int & argind );
   70   bool parse_short_option( const char * const opt, const char * const arg,
   71                            const Option options[], int & argind );
   72 
   73 public:
   74   Arg_parser( const int argc, const char * const argv[],
   75               const Option options[], const bool in_order = false );
   76 
   77   // Restricted constructor. Parses a single token and argument (if any).
   78   Arg_parser( const char * const opt, const char * const arg,
   79               const Option options[] );
   80 
   81   const std::string & error() const { return error_; }
   82 
   83   // The number of arguments parsed. May be different from argc.
   84   int arguments() const { return data.size(); }
   85 
   86   /* If code( i ) is 0, argument( i ) is a non-option.
   87      Else argument( i ) is the option's argument (or empty). */
   88   int code( const int i ) const
   89     {
   90     if( i >= 0 && i < arguments() ) return data[i].code;
   91     else return 0;
   92     }
   93 
   94   const std::string & argument( const int i ) const
   95     {
   96     if( i >= 0 && i < arguments() ) return data[i].argument;
   97     else return empty_arg;
   98     }
   99   };