_.cpp (1308B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <errno.h> 4 #include <string.h> 5 #include <assert.h> 6 7 #include <iterator> 8 9 #include <vector.hpp> 10 #include <optparser.hpp> 11 12 void print_help( char const* pgm, FILE* target, opt_desc_t const* start, opt_desc_t const* end ); 13 14 int main( int argc, char** argv ) 15 { 16 opt_desc_t opts[] = 17 { 18 STD_HELP, 19 {}, 20 }; 21 22 auto b_opts = std::begin( opts ); 23 auto e_opts = std::end( opts ); 24 char **arg = &argv[1]; 25 assert( argc > 0 ); 26 for( int iarg = 1; iarg != argc; ++iarg, ++arg ) 27 { 28 auto error = parse_cmd_opt( *arg, b_opts, e_opts ); 29 switch( error ) 30 { 31 case MAX_COUNT: 32 arg_warning( *arg, error ); 33 break; 34 case NONE: 35 case IGNORED: 36 break; 37 case SET_NO_VAL: 38 case SET_VAL_IGN: 39 case SET_FAIL: 40 case BAD_ARGS: 41 case BAD_SETTER: 42 print_help( argv[0], stderr, b_opts, e_opts ); 43 arg_error( *arg, error ); 44 return EXIT_FAILURE; 45 } 46 } 47 if( opts[0].count ) 48 { 49 print_help( argv[0], stdout, b_opts, e_opts ); 50 return EXIT_SUCCESS; 51 } 52 53 return EXIT_SUCCESS; 54 } 55 56 void print_help( char const* pgm, FILE* target, opt_desc_t const* start, opt_desc_t const* end ) 57 { 58 fputs( "Usage: ", target ); 59 fputs( pgm, target ); 60 fputs( " [OPTIONS]\n" 61 "Description:\n" 62 "Options:\n" 63 , target ); 64 print_opts( target, start, end ); 65 }