1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
void print_para_help(const char *option1, const char *option2,
const char *description);
int main(void) {
char *s = "l'explication de l'option qui peut parfois être sur plusieurs "
"lignes mais qui reste aligné.";
print_para_help("-h", "--help", s);
printf("\n");
return EXIT_SUCCESS;
}
#define ESP 2 // Espaces avant option1
#define OPT1 2 // Nombre de case accueillant option1
#define VIRG 2 // Nombre de case accueillant la virgule
#define OPT2 18 // Nombre de case accueillant option2
#define SPACE (ESP + OPT1 + VIRG + OPT2) // espaces après le retour à la ligne lorsque description est long
void print_para_help(const char *option1, const char *option2,
const char *description) {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
char *virgule = "";
if (strcmp(option1, "") != 0) {
virgule = ", ";
}
printf("%-*s%-*s%-*s%-*s", ESP, " ", OPT1, option1, VIRG, virgule, OPT2,
option2);
int k = SPACE;
size_t i = 0;
const char *s = description;
while (s[i] != '\0') {
if (k >= w.ws_col - 1) {
printf("\n%-*s", SPACE, "");
k = SPACE;
}
printf("%c", s[i]);
++i;
++k;
}
printf("\n");
} |
Partager