Exemple #1
0
/**
 * Show help information based on myoption->help values
 * @param oindex (i)  - if non-negative, show only help by myoption[oindex].help
 * @param options (i) - array of `myoption`
 *
 * @exit:  run `exit(-1)` !!!
 */
void showhelp(int oindex, myoption *options){
	// ATTENTION: string `help` prints through macro PRNT(), by default it is gettext,
	// but you can redefine it before `#include "parceargs.h"`
	int max_opt_len = 0; // max len of options substring - for right indentation
	const int bufsz = 255;
	char buf[bufsz+1];
	myoption *opts = options;
	assert(opts);
	assert(opts[0].name); // check whether there is at least one options
	if(oindex > -1){ // print only one message
		opts = &options[oindex];
		printf("  ");
		if(!opts->flag && isalpha(opts->val)) printf("-%c, ", opts->val);
		printf("--%s", opts->name);
		if(opts->has_arg == 1) printf("=arg");
		else if(opts->has_arg == 2) printf("[=arg]");
		printf("  %s\n", PRNT(opts->help));
		exit(-1);
	}
	// header, by default is just "progname\n"
	printf("\n");
	if(strstr(helpstring, "%s")) // print progname
		printf(helpstring, __progname);
	else // only text
		printf("%s", helpstring);
	printf("\n");
	// count max_opt_len
	do{
		int L = strlen(opts->name);
		if(max_opt_len < L) max_opt_len = L;
	}while((++opts)->name);
	max_opt_len += 14; // format: '-S , --long[=arg]' - get addition 13 symbols
	opts = options;
	// Now print all help
	do{
		int p = sprintf(buf, "  "); // a little indent
		if(!opts->flag && isalpha(opts->val)) // .val is short argument
			p += snprintf(buf+p, bufsz-p, "-%c, ", opts->val);
		p += snprintf(buf+p, bufsz-p, "--%s", opts->name);
		if(opts->has_arg == 1) // required argument
			p += snprintf(buf+p, bufsz-p, "=arg");
		else if(opts->has_arg == 2) // optional argument
			p += snprintf(buf+p, bufsz-p, "[=arg]");
		assert(p < max_opt_len); // there would be magic if p >= max_opt_len
		printf("%-*s%s\n", max_opt_len+1, buf, PRNT(opts->help)); // write options & at least 2 spaces after
	}while((++opts)->name);
	printf("\n\n");
	exit(-1);
}
void printTri(const double *A, const int n, const char *nm) {
    int c, r, i, sl= strlen(nm);

    PRNT("%s= [", nm);
    for(r= 0; r<n; r++) {
        for(c= 0; c<n; c++) {
            if(r>c) {
                i= (r*(r+1))/2 + c;
            } else {
                i= (c*(c+1))/2 + r;
            }
            PRNT("%g", A[i]);
            if(c<(n-1)) {
                PRNT(", ");
            } else {
                if(r<(n-1)) {
                    PRNT("\n   ");
                    for(i= 0; i<sl; i++) PRNT(" ");
                } else {
                    PRNT("]\n");
                }
            }
        }
    }
}
void printVec(const double *A, const int n, const char *nm) {
    int i, nc, ns= 255-20;
    char s[256], *ps;
    
    ps= &s[0];
    nc= sprintf(ps, "%s= [%g", nm, A[0]);
    ns-= nc;
    ps+= nc;
    for(i= 1; i<n && ns>0; i++) {
        nc= sprintf(ps, ", %g", A[i]);
        ns-= nc;
        ps+= nc;
    }
    if(ns>0) sprintf(ps, "]");
    
    PRNT("%s\n",s);
}
void printMat(const double *A, const int n, const int m, const char *nm) {
    int c, r, i, sl= strlen(nm);;

    PRNT("%s= [", nm);
    for(r= 0; r<n; r++) {
        for(c= 0; c<m; c++) {
            PRNT("%g", A[r + n*c]);
            if(c<(m-1)) {
                PRNT(", ");
            } else {
                if(r<(n-1)) {
                    PRNT("\n   ");
                    for(i= 0; i<sl; i++) PRNT(" ");
                } else {
                    PRNT("]\n");
                }
            }
        }
    }
}