Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/parseargs.cfa

    r6f94958 r772b300  
    3030
    3131static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out)  __attribute__ ((noreturn));
    32 //-----------------------------------------------------------------------------
    33 // checking
    34 static void check_args(cfa_option options[], size_t opt_count) {
    35         for(i; opt_count) {
    36                 for(j; opt_count) {
    37                         if(i == j) continue;
    38 
    39                         if( options[i].short_name != '\0'
    40                         && options[i].short_name == options[j].short_name)
    41                                 abort("Parse Args error: two options have short name '%c' (%zu & %zu)", options[i].short_name, i, j);
    42 
    43                         if(0 == strcmp(options[i].long_name, options[j].long_name)) abort("Parse Args error: two options have long name '%s' (%zu & %zu)", options[i].long_name, i, j);
    44                 }
    45         }
    46 }
    47 
    48 
    49 //-----------------------------------------------------------------------------
    50 // Parsing args
     32
    5133void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) {
    5234        if( 0p != &cfa_args_argc ) {
     
    5941}
    6042
     43//-----------------------------------------------------------------------------
     44// getopt_long wrapping
    6145void parse_args(
    6246        int argc,
     
    6751        char ** & left
    6852) {
    69         check_args(options, opt_count);
    70 
    71         int maxv = 'h';
    72         char optstring[opt_count * 3] = { '\0' };
    73         {
    74                 int idx = 0;
    75                 for(i; opt_count) {
    76                         if (options[i].short_name) {
    77                                 maxv = max(options[i].short_name, maxv);
    78                                 optstring[idx] = options[i].short_name;
    79                                 idx++;
    80                                 if(    ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue)
    81                                 && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) {
    82                                         optstring[idx] = ':';
    83                                         idx++;
    84                                 }
    85                         }
    86                 }
    87                 optstring[idx+0] = 'h';
    88                 optstring[idx+1] = '\0';
    89         }
    90 
    9153        struct option optarr[opt_count + 2];
    9254        {
     
    9456                for(i; opt_count) {
    9557                        if(options[i].long_name) {
    96                                 options[i].val = (options[i].short_name != '\0') ? ((int)options[i].short_name) : ++maxv;
    9758                                optarr[idx].name = options[i].long_name;
    9859                                optarr[idx].flag = 0p;
    99                                 optarr[idx].val  = options[i].val;
     60                                optarr[idx].val  = options[i].short_name;
    10061                                if(    ((intptr_t)options[i].parse) == ((intptr_t)parse_settrue)
    10162                                    || ((intptr_t)options[i].parse) == ((intptr_t)parse_setfalse) ) {
     
    10970                optarr[idx+0].[name, has_arg, flag, val] = ["help", no_argument, 0, 'h'];
    11071                optarr[idx+1].[name, has_arg, flag, val] = [0, no_argument, 0, 0];
     72        }
     73
     74        char optstring[opt_count * 3] = { '\0' };
     75        {
     76                int idx = 0;
     77                for(i; opt_count) {
     78                        optstring[idx] = options[i].short_name;
     79                        idx++;
     80                        if(    ((intptr_t)options[i].parse) != ((intptr_t)parse_settrue)
     81                            && ((intptr_t)options[i].parse) != ((intptr_t)parse_setfalse) ) {
     82                                optstring[idx] = ':';
     83                                idx++;
     84                        }
     85                }
     86                optstring[idx+0] = 'h';
     87                optstring[idx+1] = '\0';
    11188        }
    11289
     
    126103                        default:
    127104                                for(i; opt_count) {
    128                                         if(opt == options[i].val) {
     105                                        if(opt == options[i].short_name) {
    129106                                                const char * arg = optarg ? optarg : "";
    130107                                                if( arg[0] == '=' ) { arg++; }
     
    148125        if(hwidth <= 0) hwidth = max;
    149126
    150         char sname[4] = { ' ', ' ', ' ', '\0' };
    151         if(sn != '\0') {
    152                 sname[0] = '-';
    153                 sname[1] = sn;
    154                 sname[2] = ',';
    155         }
    156 
    157         fprintf(out, "  %s --%-*s   %.*s\n", sname, width, ln, hwidth, help);
     127        fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help);
    158128        for() {
    159129                help += min(strlen(help), hwidth);
Note: See TracChangeset for help on using the changeset viewer.