Cleaned up argument parsing to match what was in the docs

This commit is contained in:
Jordan Bancino 2022-09-24 19:41:39 -04:00
parent dac0633c0b
commit b5d538f2ce
5 changed files with 29 additions and 41 deletions

View file

@ -50,7 +50,7 @@ Phase 2: Building a foundation
[x] Handle requests
[ ] Data abstraction layer
[x] Error generation
[ ] Properly implement the command line options as stated in telodendria(8)
[x] Properly implement the command line options as stated in telodendria(8)
Phase 3: Welcome to Matrix

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: September 22 2022 $
.Dd $Mdocdate: September 24 2022 $
.Dt TELODENDRIA 8
.Os Telodendria Project
.Sh NAME
@ -6,7 +6,7 @@
.Nd Matrix homeserver daemon
.Sh SYNOPSIS
.Nm
.Op Fl hnVv
.Op Fl nVv
.Op Fl f Ar file
.Sh DESCRIPTION
.Nm
@ -18,9 +18,6 @@ The options are as follows:
.It Fl f Ar file
Specify an alternate configuration file. The default is
.Pa /etc/telodendria.conf .
.It Fl h
Show a brief help message. This message will be very short; for
full documentation, consult this manual page.
.It Fl n
Configtest mode. Only check the configuration file for validity.
.It Fl V

View file

@ -52,7 +52,8 @@ TelodendriaSignalHandler(int signalNo)
typedef enum ArgFlag
{
ARG_VERSION = (1 << 0),
ARG_USAGE = (1 << 1)
ARG_CONFIGTEST = (1 << 1),
ARG_VERBOSE = (1 << 2)
} ArgFlag;
static void
@ -77,15 +78,6 @@ TelodendriaPrintHeader(LogConfig * lc)
Log(lc, LOG_MESSAGE, "");
}
static void
TelodendriaPrintUsage(LogConfig * lc)
{
Log(lc, LOG_MESSAGE, "Usage:");
Log(lc, LOG_MESSAGE, " -c <file> Configuration file ('-' for stdin).");
Log(lc, LOG_MESSAGE, " -V Print the header, then exit.");
Log(lc, LOG_MESSAGE, " -h Print this usage, then exit.");
}
int
main(int argc, char **argv)
{
@ -95,7 +87,7 @@ main(int argc, char **argv)
/* Arg parsing */
int opt;
int flags = 0;
char *configArg = NULL;
char *configArg = "/etc/telodendria.conf";
/* Config file */
FILE *configFile = NULL;
@ -135,18 +127,25 @@ main(int argc, char **argv)
}
#endif
while ((opt = getopt(argc, argv, "c:Vh")) != -1)
while ((opt = getopt(argc, argv, "f:Vvn")) != -1)
{
switch (opt)
{
case 'c':
case 'f':
configArg = optarg;
break;
case 'V':
flags |= ARG_VERSION;
break;
case 'h':
flags |= ARG_USAGE;
case 'v':
flags |= ARG_VERBOSE;
break;
case 'n':
flags |= ARG_CONFIGTEST;
break;
case '?':
exit = EXIT_FAILURE;
goto finish;
default:
break;
}
@ -157,20 +156,6 @@ main(int argc, char **argv)
goto finish;
}
if (flags & ARG_USAGE)
{
TelodendriaPrintUsage(lc);
goto finish;
}
if (!configArg)
{
Log(lc, LOG_ERROR, "No configuration file specified.");
TelodendriaPrintUsage(lc);
exit = EXIT_FAILURE;
goto finish;
}
if (strcmp(configArg, "-") == 0)
{
configFile = stdin;
@ -219,6 +204,12 @@ main(int argc, char **argv)
ConfigFree(config);
if (flags & ARG_CONFIGTEST)
{
Log(lc, LOG_MESSAGE, "Configuration is OK.");
goto finish;
}
#ifdef __OpenBSD__
if (unveil(tConfig->chroot, "rwc") != 0)
{
@ -238,7 +229,7 @@ main(int argc, char **argv)
LogConfigFlagClear(lc, LOG_FLAG_COLOR);
}
LogConfigLevelSet(lc, tConfig->logLevel);
LogConfigLevelSet(lc, flags & ARG_VERBOSE ? LOG_DEBUG : tConfig->logLevel);
if (tConfig->logOut)
{

View file

@ -100,7 +100,7 @@ recipe_build() {
recipe_run() {
if [ -f "build/$PROG" ]; then
"build/$PROG" -c contrib/development.conf
"build/$PROG" -f contrib/development.conf
else
echo "build/$PROG does not exist; build it first."
fi