Changeset 419c434
- Timestamp:
- Aug 12, 2020, 3:31:51 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:
- d9265a2
- Parents:
- cd02108
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/parseargs.cfa
rcd02108 r419c434 29 29 extern char ** cfa_args_envp; 30 30 31 void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { 32 int hwidth = max - (11 + width); 33 if(hwidth <= 0) hwidth = max; 34 35 fprintf(out, " -%c, --%-*s %.*s\n", sn, width, ln, hwidth, help); 36 for() { 37 help += min(strlen(help), hwidth); 38 if('\0' == *help) break; 39 fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); 40 } 41 } 31 static void usage(char * cmd, cfa_option options[], size_t opt_count, const char * usage, FILE * out) __attribute__ ((noreturn)); 42 32 43 33 void parse_args( cfa_option options[], size_t opt_count, const char * usage, char ** & left ) { … … 45 35 } 46 36 37 //----------------------------------------------------------------------------- 38 // getopt_long wrapping 47 39 void parse_args( 48 40 int argc, … … 54 46 ) { 55 47 struct option optarr[opt_count + 2]; 56 int width = 0;57 int max_width = 1_000_000;58 48 { 59 49 int idx = 0; … … 70 60 } 71 61 idx++; 72 73 int w = strlen(options[i].long_name);74 if(w > width) width = w;75 62 } 76 63 } … … 107 94 out = stdout; 108 95 case '?': 109 goto USAGE;96 usage(argv[0], options, opt_count, usage, out); 110 97 default: 111 98 for(i; opt_count) { … … 116 103 117 104 fprintf(out, "Argument '%s' for option %c could not be parsed\n\n", arg, (char)opt); 118 goto USAGE;105 usage(argv[0], options, opt_count, usage, out); 119 106 } 120 107 } … … 123 110 124 111 } 125 126 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 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; 127 149 int outfd = fileno(out); 128 150 if(isatty(outfd)) { … … 133 155 } 134 156 135 fprintf(out, "Usage:\n %s %s\n", argv[0], usage);157 fprintf(out, "Usage:\n %s %s\n", cmd, help); 136 158 137 159 for(i; opt_count) { … … 142 164 } 143 165 166 //----------------------------------------------------------------------------- 167 // Typed argument parsing 144 168 bool parse_yesno(const char * arg, bool & value ) { 145 169 if(strcmp(arg, "yes") == 0) { -
libcfa/src/parseargs.hfa
rcd02108 r419c434 34 34 void parse_args( int argc, char * argv[], cfa_option options[], size_t opt_count, const char * usage, char ** & left ); 35 35 36 void print_args_usage(cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)); 37 void print_args_usage(int argc, char * argv[], cfa_option options[], size_t opt_count, const char * usage, bool error) __attribute__ ((noreturn)); 38 36 39 bool parse_yesno (const char *, bool & ); 37 40 bool parse_settrue (const char *, bool & );
Note: See TracChangeset
for help on using the changeset viewer.