Help: print command specific help message

This commit is contained in:
李通洲
2023-11-21 16:39:38 +08:00
parent 02bdcd355c
commit 7bcb015a06
2 changed files with 70 additions and 5 deletions
+4 -4
View File
@@ -592,7 +592,7 @@
"long": "size-max-prefix",
"desc": "Set the largest binary prefix to use when formatting sizes",
"arg": {
"type": "str",
"type": "enum",
"enum": {
"B": "Bytes",
"kB": "KiB",
@@ -611,7 +611,7 @@
"long": "temperature-unit",
"desc": "Set the unit of the temperature",
"arg": {
"type": "str",
"type": "enum",
"enum": {
"C": "Celsius",
"F": "Fahrenheit",
@@ -1241,7 +1241,7 @@
"long": "opengl-library",
"desc": "Set the OpenGL context creation library to use",
"arg": {
"type": "str",
"type": "enum",
"enum": {
"auto": "Auto detection",
"egl": "EGL",
@@ -1277,7 +1277,7 @@
"long": "colors-symbol",
"desc": "Set the symbol to be printed by Colors module",
"arg": {
"type": "str",
"type": "enum",
"enum": {
"block": "███",
"circle": "●",
+66 -1
View File
@@ -155,7 +155,72 @@ static bool printSpecificCommandHelp(const char* command)
assert(longKey);
if (ffStrEqualsIgnCase(command, yyjson_get_str(longKey)))
{
printf("Usage: --%s\n", command);
puts(yyjson_get_str(yyjson_obj_get(flagObj, "desc")));
printf("%10s: ", "Usage");
yyjson_val* shortKey = yyjson_obj_get(flagObj, "short");
if (shortKey)
{
if (!instance.config.display.pipe)
fputs("\e[1m", stdout);
printf("-%s", yyjson_get_str(shortKey));
if (!instance.config.display.pipe)
fputs("\e[m", stdout);
fputs(", ", stdout);
}
if (!instance.config.display.pipe)
fputs("\e[1m", stdout);
printf("--%s", yyjson_get_str(longKey));
if (!instance.config.display.pipe)
fputs("\e[m", stdout);
yyjson_val* argObj = yyjson_obj_get(flagObj, "arg");
if (argObj)
{
yyjson_val* typeKey = yyjson_obj_get(argObj, "type");
assert(typeKey);
yyjson_val* optionalKey = yyjson_obj_get(argObj, "optional");
bool optional = optionalKey && yyjson_get_bool(optionalKey);
putchar(' ');
if (!instance.config.display.pipe)
fputs("\e[3m", stdout);
printf("<%s%s>", optional ? "?" : "", yyjson_get_str(typeKey));
if (!instance.config.display.pipe)
fputs("\e[m", stdout);
putchar('\n');
yyjson_val* defaultKey = yyjson_obj_get(argObj, "default");
if (defaultKey)
{
if (ffStrEqualsIgnCase(yyjson_get_str(typeKey), "structure"))
printf("%10s: %s\n", "Default", FASTFETCH_DATATEXT_STRUCTURE);
else if (yyjson_is_bool(defaultKey))
printf("%10s: %s\n", "Default", yyjson_get_bool(defaultKey) ? "true" : "false");
else if (yyjson_is_num(defaultKey))
printf("%10s: %g\n", "Default", yyjson_get_num(defaultKey));
else if (yyjson_is_str(defaultKey))
printf("%10s: %s\n", "Default", yyjson_get_str(defaultKey));
else
printf("%10s: Unknown\n", "Default");
}
yyjson_val* enumKey = yyjson_obj_get(argObj, "enum");
if (enumKey)
{
printf("%10s:\n", "Options");
yyjson_val *optKey, *optVal;
size_t optIdx, optMax;
yyjson_obj_foreach(enumKey, optIdx, optMax, optKey, optVal)
printf("%12s: %s\n", yyjson_get_str(optKey), yyjson_get_str(optVal));
}
}
else
putchar('\n');
yyjson_val* remarkKey = yyjson_obj_get(flagObj, "remark");
if (remarkKey)
printf("%10s: %s\n", "Remark", yyjson_get_str(remarkKey));
return true;
}
}