curl: add -h cheat-sheet using curl-cheat-sheet as text

Rebase and resolve failing checks.
This commit is contained in:
John Haugabook 2025-07-29 22:41:58 -04:00 committed by Daniel Stenberg
parent 1140c8bc04
commit ed8f166f70
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2

View file

@ -248,6 +248,8 @@ void tool_help(const char *category)
/* Lets handle the string "category" differently to not print an errormsg */
else if(curl_strequal(category, "category"))
get_categories();
else if (curl_strequal(category, "cheat-sheet"))
tool_cheat_sheet();
else if(category[0] == '-') {
#ifdef USE_MANUAL
/* command line option help */
@ -405,3 +407,191 @@ void tool_list_engines(void)
curl_slist_free_all(engines);
curl_easy_cleanup(curl);
}
/* Functions for outputting cheat-sheet argument. */
/* Cheat sheet data structure organized by heading and value. */
static const struct cheat_table {
const char *heading[2];
} cheat_items[] = {
{{"Verbose", "-v, --trace-ascii file"}},
{{"Hide progress", "-s"}},
{{"extra info", "-w format"}},
{{"Write output", "-O, -o file"}},
{{"Timeout", "-m secs"}},
{{"POST", "-d string, -d @file"}},
{{"multipart", "-F name=value, -F name=@file"}},
{{"PUT", "-T file"}},
{{"HEAD", "-I"}},
{{"custom", "-X METHOD"}},
{{"Basic auth", "-u user:password"}},
{{"read cookies", "-b <file>"}},
{{"write cookies", "-c <file>"}},
{{"send cookies", "-b \"c=1; d=2\""}},
{{"user-agent", "-A string"}},
{{"Use proxy", "-x host:port"}},
{{"Headers add/remove", "-H \"name: value\", -H name:"}},
{{"follow redirs", "-L"}},
{{"gzip", "--compressed"}},
{{"insecure", "-k"}},
{{"", ""}},
{{NULL, NULL}}
};
static int incre_i(int i, int j) {
if (j == 0)
return i + 1;
else
return i + 1 + j;
}
/* Support to print number of columns per screen width. */
static void print_cheatsheet(int i, int col_num) {
/* Spacer variable for heading. */
const char *spacer = "------------------------------";
/* Use consistent width for each column. */
int width = 30;
if (col_num == 1) {
if(cheat_items[i].heading[0] &&
cheat_items[i].heading[0] != NULL &&
strcmp(cheat_items[i].heading[0], "") != 0)
printf("%-20s\n%s\n%s\n\n",
cheat_items[i].heading[0], spacer, cheat_items[i].heading[1]);
}
else if (col_num == 2) {
if(cheat_items[i+1].heading[0] &&
cheat_items[i+1].heading[0] == NULL)
print_cheatsheet(i, 1);
else
if(cheat_items[i].heading[0] &&
cheat_items[i].heading[0] != NULL &&
strcmp(cheat_items[i].heading[0], "") != 0)
printf("%-*s %-*s\n%s %s\n%-*s %-*s\n\n",
width, cheat_items[i].heading[0],
width, cheat_items[i+1].heading[0],
spacer, spacer,
width, cheat_items[i].heading[1],
width, cheat_items[i+1].heading[1]);
}
else if (col_num == 3) {
if(cheat_items[i+1].heading[0] &&
cheat_items[i+1].heading[0] == NULL)
print_cheatsheet(i, 1);
else if(cheat_items[i+2].heading[0] &&
cheat_items[i+2].heading[0] == NULL)
print_cheatsheet(i, 2);
else
if(cheat_items[i].heading[0] &&
cheat_items[i].heading[0] != NULL &&
strcmp(cheat_items[i].heading[0], "") != 0)
printf("%-*s %-*s %-*s\n%s %s %s\n%-*s %-*s %-*s\n\n",
width, cheat_items[i].heading[0],
width, cheat_items[i+1].heading[0],
width, cheat_items[i+2].heading[0],
spacer, spacer, spacer,
width, cheat_items[i].heading[1],
width, cheat_items[i+1].heading[1],
width, cheat_items[i+2].heading[1]);
}
else if (col_num == 4) {
if(cheat_items[i+1].heading[0] &&
cheat_items[i+1].heading[0] == NULL)
print_cheatsheet(i, 1);
else if(cheat_items[i+2].heading[0] &&
cheat_items[i+2].heading[0] == NULL)
print_cheatsheet(i, 2);
else if(cheat_items[i+3].heading[0] &&
cheat_items[i+3].heading[0] == NULL)
print_cheatsheet(i, 3);
else
if (cheat_items[i].heading[0] &&
cheat_items[i].heading[0] != NULL &&
strcmp(cheat_items[i].heading[0], "") != 0)
printf("%-*s %-*s %-*s %-*s\n%s %s %s %s\n"
"%-*s %-*s %-*s %-*s\n\n",
width, cheat_items[i].heading[0],
width, cheat_items[i+1].heading[0],
width, cheat_items[i+2].heading[0],
width, cheat_items[i+3].heading[0],
spacer, spacer, spacer, spacer,
width, cheat_items[i].heading[1],
width, cheat_items[i+1].heading[1],
width, cheat_items[i+2].heading[1],
width, cheat_items[i+3].heading[1]);
}
else if (col_num == 5) {
if(cheat_items[i+1].heading[0] &&
cheat_items[i+1].heading[0] == NULL)
print_cheatsheet(i, 1);
else if(cheat_items[i+2].heading[0] &&
cheat_items[i+2].heading[0] == NULL)
print_cheatsheet(i, 2);
else if(cheat_items[i+3].heading[0] &&
cheat_items[i+3].heading[0] == NULL)
print_cheatsheet(i, 3);
else if(cheat_items[i+4].heading[0] &&
cheat_items[i+4].heading[0] == NULL)
print_cheatsheet(i, 4);
else
if(cheat_items[i].heading[0] &&
cheat_items[i].heading[0] != NULL &&
strcmp(cheat_items[i].heading[0], "") != 0)
printf("%-*s %-*s %-*s %-*s %-*s\n%s %s %s %s %s\n"
"%-*s %-*s %-*s %-*s %-*s\n\n",
width, cheat_items[i].heading[0],
width, cheat_items[i+1].heading[0],
width, cheat_items[i+2].heading[0],
width, cheat_items[i+3].heading[0],
width, cheat_items[i+4].heading[0],
spacer, spacer, spacer, spacer, spacer,
width, cheat_items[i].heading[1],
width, cheat_items[i+1].heading[1],
width, cheat_items[i+2].heading[1],
width, cheat_items[i+3].heading[1],
width, cheat_items[i+4].heading[1]);
}
}
/* Output cheat-sheet. */
void tool_cheat_sheet(void)
{
/* Get terminal width for proper formatting */
unsigned int cols = get_terminal_columns();
/* Get the table length. */
unsigned int table_length = sizeof(cheat_items) / sizeof(cheat_items[0]);
/* Handle for loop according to cols */
unsigned int i, j;
/* Set j based on col width, setting once, not in a loop. */
if (cols <= 75)
j = 0;
else if (cols > 75 && cols <= 125)
j = 1;
else if (cols > 125 && cols <= 175)
j = 2;
else if (cols > 175 && cols <= 225)
j = 3;
else
j = 4;
/* Loop through cheat sheet sections */
for(i = 0; i < table_length; i = incre_i(i, j)) {
if (cheat_items[i+j].heading[0] == NULL) {
break;
} else {
if (cols <= 75) /* Output one columns. */
print_cheatsheet(i, 1);
else if(cols > 75 && cols <= 125) /* output two columns */
print_cheatsheet(i, 2);
else if(cols > 125 && cols <= 175) /* output three columns */
print_cheatsheet(i, 3);
else if(cols > 175 && cols <= 225) /* output four columns */
print_cheatsheet(i, 4);
else
print_cheatsheet(i, 5); /* output five columns */
}
}
}