Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/parseargs.cfa

    rf82f07e raffb51b  
    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 
    171#include "parseargs.hfa"
    182
    19 #include <assert.h>
    203#include <ctype.h>
    214#include <stdint.h>
     
    163146}
    164147
    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 
    178148//-----------------------------------------------------------------------------
    179149// Print usage
    180150static 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
    183151        int hwidth = max - (11 + width);
    184152        if(hwidth <= 0) hwidth = max;
    185153
    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");
     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        }
    221167}
    222168
Note: See TracChangeset for help on using the changeset viewer.