aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2021-04-26 07:40:37 +0200
committerJan200101 <sentrycraft123@gmail.com>2021-04-26 07:40:37 +0200
commite6279af295cc0e8b985a5a43a16ac6313eb6c698 (patch)
tree52f39915b61988b784fd03495234dd75001e033e /src/common.c
parent67d5d1b742584ffeb9859eb7b92a41e0d365622d (diff)
downloadpolecat-e6279af295cc0e8b985a5a43a16ac6313eb6c698.tar.gz
polecat-e6279af295cc0e8b985a5a43a16ac6313eb6c698.zip
implement flags and sort commands by alphabet
stupid standard --help and --version
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/common.c b/src/common.c
index 36cd2d9..ae2087e 100644
--- a/src/common.c
+++ b/src/common.c
@@ -8,20 +8,59 @@
#include "common.h"
-void print_help(const struct Command* commands, const size_t size)
+void print_help(const struct Command* commands, const size_t commands_size,
+ const struct Flag* flags, size_t flags_size)
{
- size_t longestCommand = 0;
+ size_t longestStr;
+ size_t length;
- for (size_t i = 0; i < size; ++i)
+ if (commands_size)
{
- size_t commandLength = strlen(commands[i].name);
+ longestStr = 0;
- if (commandLength > longestCommand) longestCommand = commandLength;
+ for (size_t i = 0; i < commands_size; ++i)
+ {
+ length = strlen(commands[i].name);
+
+ if (length > longestStr) longestStr = length;
+ }
+
+ fprintf(stderr, "\nList of commands:\n");
+ for (size_t i = 0; i < commands_size; ++i)
+ {
+ fprintf(stderr, "\t%-*s\t %s\n", (int)longestStr, commands[i].name, commands[i].description);
+ }
}
- for (size_t i = 0; i < size; ++i)
+
+ if (flags_size)
{
- printf("\t%-*s\t %s\n", (int)longestCommand, commands[i].name, commands[i].description);
+ longestStr = 0;
+
+ for (size_t i = 0; i < flags_size; ++i)
+ {
+ length = strlen(flags[i].name);
+
+ if (length > longestStr) longestStr = length;
+ }
+
+ fprintf(stderr, "\nList of flags:\n");
+ for (size_t i = 0; i < flags_size; ++i)
+ {
+ fprintf(stderr, "\t");
+ if (flags[i].variant & SINGLE)
+ fprintf(stderr, "-%c", flags[i].name[0]);
+ else
+ fprintf(stderr, " ");
+
+ if (flags[i].variant & DOUBLE)
+ {
+ if (flags[i].variant & SINGLE) fprintf(stderr, ",");
+ fprintf(stderr, " --%-*s", (int)longestStr, flags[i].name);
+ }
+
+ fprintf(stderr, "\t %s\n", flags[i].description);
+ }
}
}
@@ -96,7 +135,7 @@ int removeDir(const char *path)
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
continue;
- len = path_len + strlen(p->d_name) + 2;
+ len = path_len + strlen(p->d_name) + 2;
buf = malloc(len);
if (buf) {