Changes in libcfa/src/parseargs.cfa [419c434:56e8cb3]
- File:
-
- 1 edited
-
libcfa/src/parseargs.cfa (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/parseargs.cfa
r419c434 r56e8cb3 19 19 extern long long int strtoll (const char* str, char** endptr, int base); 20 20 extern unsigned long long int strtoull(const char* str, char** endptr, int base); 21 extern double strtod (const char* str, char** endptr);22 21 } 23 22 … … 25 24 #include "limits.hfa" 26 25 27 extern int cfa_args_argc; 28 extern char ** cfa_args_argv;29 extern char ** cfa_args_envp;30 31 static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out) __attribute__ ((noreturn));32 33 void 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 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 39 38 void parse_args( 40 39 int argc, … … 46 45 ) { 47 46 struct option optarr[opt_count + 2]; 47 int width = 0; 48 int max_width = 1_000_000; 48 49 { 49 50 int idx = 0; … … 60 61 } 61 62 idx++; 63 64 int w = strlen(options[i].long_name); 65 if(w > width) width = w; 62 66 } 63 67 } … … 94 98 out = stdout; 95 99 case '?': 96 usage(argv[0], options, opt_count, usage, out);100 goto USAGE; 97 101 default: 98 102 for(i; opt_count) { … … 103 107 104 108 fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); 105 usage(argv[0], options, opt_count, usage, out);109 goto USAGE; 106 110 } 107 111 } … … 110 114 111 115 } 112 } 113 114 //----------------------------------------------------------------------------- 115 // Print usage 116 static 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 128 void 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 132 void 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 136 static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * help, FILE * out) __attribute__((noreturn)) { 137 int width = 0; 138 { 139 int idx = 0; 140 for(i; opt_count) { 141 if(options[i].long_name) { 142 int w = strlen(options[i].long_name); 143 if(w > width) width = w; 144 } 145 } 146 } 147 148 int max_width = 1_000_000; 116 117 USAGE:; 149 118 int outfd = fileno(out); 150 119 if(isatty(outfd)) { … … 155 124 } 156 125 157 fprintf(out, "Usage:\n %s %s\n", cmd, help);126 fprintf(out, "Usage:\n %s %s\n", argv[0], usage); 158 127 159 128 for(i; opt_count) { … … 164 133 } 165 134 166 //-----------------------------------------------------------------------------167 // Typed argument parsing168 135 bool parse_yesno(const char * arg, bool & value ) { 169 136 if(strcmp(arg, "yes") == 0) { … … 192 159 bool parse(const char * arg, const char * & value ) { 193 160 value = arg; 194 return true;195 }196 197 bool parse(const char * arg, int & value) {198 char * end;199 int r = strtoll(arg, &end, 10);200 if(*end != '\0') return false;201 202 value = r;203 161 return true; 204 162 } … … 234 192 } 235 193 236 bool parse(const char * arg, double& value) {194 bool parse(const char * arg, int & value) { 237 195 char * end; 238 double r = strtod(arg, &end);196 int r = strtoll(arg, &end, 10); 239 197 if(*end != '\0') return false; 240 198
Note:
See TracChangeset
for help on using the changeset viewer.