Merge pull request 'merge 14-argument-handler int command-line-interface' (#36) from 14-argument-handler into command-line-interface

Reviewed-on: #36
This commit is contained in:
Adam Fordsmand 2022-09-28 18:44:18 +00:00
commit 0b3cc7f5db
2 changed files with 88 additions and 0 deletions

View File

88
focussg.c Normal file
View File

@ -0,0 +1,88 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "BlogDB.h"
static int HelpMessage();
static int VersionMessage();
static int ShortArg(char currentArg[], int argIndex, char *argv[]);
static int LongArg(char currentArg[], int argIndex, char *argv[]);
int HelpMessage()
{
printf("Usage: focussg [OPTION]\n");
printf("FoCuSSG (Fordsmand's C Static Site Generator)\n");
printf("\n");
printf(" -h, --help Show this help message.\n");
printf(" -V, --version Show the version number of FoCuSSG.\n");
exit(0);
}
int VersionMessage()
{
printf("Pre-Release\n");
exit(0);
}
int ShortArg(char currentArg[], int argIndex, char *argv[])
{
char* options;
options = &currentArg[1];
int count = strlen(options);
if (count == 0) { HelpMessage(); exit(0); }
for (int i = 0; i < count; ++i) {
switch (options[i]) {
case 'h':
HelpMessage();
break;
case 'V':
VersionMessage();
break;
case 0: // Missing character, should never happen
exit(1);
break;
default:
HelpMessage();
break;
}
}
}
int LongArg(char currentArg[], int argIndex, char *argv[])
{
char* option;
option = &currentArg[2];
if (strcmp(option, "help") == 0)
HelpMessage();
else if (strcmp(option, "version") == 0)
VersionMessage();
else if (strcmp(option, "") == 0)
HelpMessage();
else
HelpMessage();
}
int main(int argc, char *argv[])
{
if (argc == 1) HelpMessage();
for (int i = 1; i < argc; ++i)
{
if (argv[i][0] != '-')
HelpMessage();
else if (argv[i][1] != '-')
ShortArg(argv[i], i, argv);
else
LongArg(argv[i], i, argv);
}
return 0;
}