Changeset f82f07e


Ignore:
Timestamp:
Oct 13, 2022, 5:06:16 PM (18 months ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, master
Children:
4af5396
Parents:
38cc59f
Message:

Fixes to usage and corresponding tests

Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/parseargs.cfa

    r38cc59f rf82f07e  
    1717#include "parseargs.hfa"
    1818
     19#include <assert.h>
    1920#include <ctype.h>
    2021#include <stdint.h>
     
    162163}
    163164
     165static 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
    164178//-----------------------------------------------------------------------------
    165179// Print usage
    166180static 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
    167183        int hwidth = max - (11 + width);
    168184        if(hwidth <= 0) hwidth = max;
    169185
    170         char sname[4] = { ' ', ' ', ' ', '\0' };
    171         if(sn != '\0') {
    172                 sname[0] = '-';
    173                 sname[1] = sn;
    174                 sname[2] = ',';
    175         }
    176 
    177         fprintf(out, "  %s --%-*s   %.*s\n", sname, width, ln, hwidth, help);
    178         for() {
    179                 help += min(strlen(help), hwidth);
    180                 if('\0' == *help) break;
    181                 fprintf(out, "%*s%.*s\n", width + 11, "", hwidth, help);
    182         }
     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");
    183221}
    184222
Note: See TracChangeset for help on using the changeset viewer.