Changeset 4af5396 for libcfa/src/parseargs.cfa
- Timestamp:
- Oct 13, 2022, 10:00:03 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 80fbdc9
- Parents:
- a25bcf8 (diff), f82f07e (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
ra25bcf8 r4af5396 1 // 2 // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo 3 // 4 // The contents of this file are covered under the licence agreement in the 5 // file "LICENCE" distributed with Cforall. 6 // 7 // parseargs.cfa 8 // implementation of arguments parsing (argc, argv) 9 // 10 // Author : Thierry Delisle 11 // Created On : Wed Oct 12 15:28:01 2022 12 // Last Modified By : 13 // Last Modified On : 14 // Update Count : 15 // 16 1 17 #include "parseargs.hfa" 2 18 19 #include <assert.h> 3 20 #include <ctype.h> 4 21 #include <stdint.h> … … 146 163 } 147 164 165 static inline int next_newline(const char * str) { 166 int ret; 167 const char * ptr = strstr(str, "\n"); 168 if(!ptr) return MAX; 169 170 /* paranoid */ verify( str <= ptr); 171 intptr_t low = (intptr_t)str; 172 intptr_t hi = (intptr_t)ptr; 173 ret = hi - low; 174 175 return ret; 176 } 177 148 178 //----------------------------------------------------------------------------- 149 179 // Print usage 150 180 static void printopt(FILE * out, int width, int max, char sn, const char * ln, const char * help) { 181 // check how wide we should be printing 182 // this includes all options and the help message 151 183 int hwidth = max - (11 + width); 152 184 if(hwidth <= 0) hwidth = max; 153 185 154 char sname[4] = { ' ', ' ', ' ', '\0' }; 155 if(sn != '\0') { 156 sname[0] = '-'; 157 sname[1] = sn; 158 sname[2] = ','; 159 } 160 161 fprintf(out, " %s --%-*s %.*s\n", sname, width, ln, hwidth, help); 162 for() { 163 help += min(strlen(help), hwidth); 164 if('\0' == *help) break; 165 fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help); 166 } 186 // check which pieces we have 187 bool has_ln = ln && strcmp("", ln); 188 bool has_help = help && strcmp("", help); 189 190 // print the small name if present 191 if(sn != '\0') fprintf(out, " -%c", sn); 192 else fprintf(out, " "); 193 194 // print a comma if we have both short and long names 195 if(sn != '\0' && has_ln) fprintf(out, ", "); 196 else fprintf(out, " "); 197 198 // print the long name if present 199 if(has_ln) fprintf(out, "--%-*s", width, ln); 200 else if(has_help) fprintf(out, " %-*s", width, ""); 201 202 if(has_help) { 203 // print the help 204 // We need to wrap at the max width, and also indent newlines so everything is nice and pretty 205 206 // for each line to print 207 for() { 208 //find out if there is a newline 209 int nextnl = next_newline(help); 210 int real = min(min(strlen(help), hwidth), nextnl); 211 212 fprintf(out, " %.*s", real, help); 213 // printf("%d %d\n", real, nextnl); 214 help += real; 215 if( nextnl == real ) help++; 216 if('\0' == *help) break; 217 fprintf(out, "\n%*s", width + 8, ""); 218 } 219 } 220 fprintf(out, "\n"); 167 221 } 168 222
Note: See TracChangeset
for help on using the changeset viewer.