Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/parseargs.cfa

    r56e8cb3 rfa5e0112  
    1919        extern          long long int strtoll (const char* str, char** endptr, int base);
    2020        extern unsigned long long int strtoull(const char* str, char** endptr, int base);
     21        extern                 double strtod  (const char* str, char** endptr);
    2122}
    2223
     
    2425#include "limits.hfa"
    2526
    26 void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) {
    27         int hwidth = max - (11 + width);
    28         if(hwidth <= 0) hwidth = max;
    29 
    30         fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help);
    31         for() {
    32                 help += min(strlen(help), hwidth);
    33                 if('\0' == *help) break;
    34                 fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help);
    35         }
    36 }
    37 
     27extern int cfa_args_argc;
     28extern char ** cfa_args_argv;
     29extern char ** cfa_args_envp;
     30
     31static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out)  __attribute__ ((noreturn));
     32
     33void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) {
     34        parse_args(cfa_args_argc, cfa_args_argv, options, opt_count, usage, left );
     35}
     36
     37//-----------------------------------------------------------------------------
     38// getopt_long wrapping
    3839void parse_args(
    3940        int argc,
     
    4546) {
    4647        struct option optarr[opt_count + 2];
    47         int width = 0;
    48         int max_width = 1_000_000;
    4948        {
    5049                int idx = 0;
     
    6160                                }
    6261                                idx++;
    63 
    64                                 int w = strlen(options[i].long_name);
    65                                 if(w > width) width = w;
    6662                        }
    6763                }
     
    9894                                out = stdout;
    9995                        case '?':
    100                                 goto USAGE;
     96                                usage(argv[0], options, opt_count, usage, out);
    10197                        default:
    10298                                for(i; opt_count) {
     
    107103
    108104                                                fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt);
    109                                                 goto USAGE;
     105                                                usage(argv[0], options, opt_count, usage, out);
    110106                                        }
    111107                                }
     
    114110
    115111        }
    116 
    117         USAGE:;
     112}
     113
     114//-----------------------------------------------------------------------------
     115// Print usage
     116static void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) {
     117        int hwidth = max - (11 + width);
     118        if(hwidth <= 0) hwidth = max;
     119
     120        fprintf(out, "  -%c, --%-*s   %.*s\n", sn, width, ln, hwidth, help);
     121        for() {
     122                help += min(strlen(help), hwidth);
     123                if('\0' == *help) break;
     124                fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help);
     125        }
     126}
     127
     128void print_args_usage(cfa_option options[], size_t opt_count, const char * usage, bool error)  __attribute__ ((noreturn)) {
     129        usage(cfa_args_argv[0], options, opt_count, usage, error ? stderr : stdout);
     130}
     131
     132void print_args_usage(int , char * argv[], cfa_option options[], size_t opt_count, const char * usage, bool error)  __attribute__ ((noreturn)) {
     133        usage(argv[0], options, opt_count, usage, error ? stderr : stdout);
     134}
     135
     136static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * help, FILE * out) __attribute__((noreturn)) {
     137        int width = 0;
     138        {
     139                for(i; opt_count) {
     140                        if(options[i].long_name) {
     141                                int w = strlen(options[i].long_name);
     142                                if(w > width) width = w;
     143                        }
     144                }
     145        }
     146
     147        int max_width = 1_000_000;
    118148        int outfd = fileno(out);
    119149        if(isatty(outfd)) {
     
    124154        }
    125155
    126         fprintf(out, "Usage:\n  %s %s\n", argv[0], usage);
     156        fprintf(out, "Usage:\n  %s %s\n", cmd, help);
    127157
    128158        for(i; opt_count) {
     
    133163}
    134164
     165//-----------------------------------------------------------------------------
     166// Typed argument parsing
    135167bool parse_yesno(const char * arg, bool & value ) {
    136168        if(strcmp(arg, "yes") == 0) {
     
    159191bool parse(const char * arg, const char * & value ) {
    160192        value = arg;
     193        return true;
     194}
     195
     196bool parse(const char * arg, int & value) {
     197        char * end;
     198        int r = strtoll(arg, &end, 10);
     199        if(*end != '\0') return false;
     200
     201        value = r;
    161202        return true;
    162203}
     
    192233}
    193234
    194 bool parse(const char * arg, int & value) {
    195         char * end;
    196         int r = strtoll(arg, &end, 10);
    197         if(*end != '\0') return false;
    198 
    199         value = r;
    200         return true;
    201 }
     235bool parse(const char * arg, double & value) {
     236        char * end;
     237        double r = strtod(arg, &end);
     238        if(*end != '\0') return false;
     239
     240        value = r;
     241        return true;
     242}
Note: See TracChangeset for help on using the changeset viewer.