Changeset 13d33a75 for libcfa/src/parseargs.cfa
- Timestamp:
- Aug 18, 2020, 4:31:19 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 8e9d567
- Parents:
- ef9988b (diff), f2384c9a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/parseargs.cfa
ref9988b r13d33a75 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); 21 22 } 22 23 … … 28 29 extern char ** cfa_args_envp; 29 30 30 void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { 31 int hwidth = max - (11 + width); 32 if(hwidth <= 0) hwidth = max; 33 34 fprintf(out, " -%c, --%-*s %.*s\n", sn, width, ln, hwidth, help); 35 for() { 36 help += min(strlen(help), hwidth); 37 if('\0' == *help) break; 38 fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); 39 } 40 } 31 static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out) __attribute__ ((noreturn)); 41 32 42 33 void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) { … … 44 35 } 45 36 37 //----------------------------------------------------------------------------- 38 // getopt_long wrapping 46 39 void parse_args( 47 40 int argc, … … 53 46 ) { 54 47 struct option optarr[opt_count + 2]; 55 int width = 0;56 int max_width = 1_000_000;57 48 { 58 49 int idx = 0; … … 69 60 } 70 61 idx++; 71 72 int w = strlen(options[i].long_name);73 if(w > width) width = w;74 62 } 75 63 } … … 106 94 out = stdout; 107 95 case '?': 108 goto USAGE;96 usage(argv[0], options, opt_count, usage, out); 109 97 default: 110 98 for(i; opt_count) { … … 115 103 116 104 fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); 117 goto USAGE;105 usage(argv[0], options, opt_count, usage, out); 118 106 } 119 107 } … … 122 110 123 111 } 124 125 USAGE:; 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 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; 126 148 int outfd = fileno(out); 127 149 if(isatty(outfd)) { … … 132 154 } 133 155 134 fprintf(out, "Usage:\n %s %s\n", argv[0], usage);156 fprintf(out, "Usage:\n %s %s\n", cmd, help); 135 157 136 158 for(i; opt_count) { … … 141 163 } 142 164 165 //----------------------------------------------------------------------------- 166 // Typed argument parsing 143 167 bool parse_yesno(const char * arg, bool & value ) { 144 168 if(strcmp(arg, "yes") == 0) { … … 167 191 bool parse(const char * arg, const char * & value ) { 168 192 value = arg; 193 return true; 194 } 195 196 bool 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; 169 202 return true; 170 203 } … … 200 233 } 201 234 202 bool parse(const char * arg, int& value) {203 char * end; 204 int r = strtoll(arg, &end, 10);205 if(*end != '\0') return false; 206 207 value = r; 208 return true; 209 } 235 bool 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.