feat: standard argument parsing

This commit is contained in:
2025-07-17 18:22:10 -04:00
parent ccd28fc9bf
commit d943d6db8c
6 changed files with 134 additions and 45 deletions

12
config.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef CONFIG_H
#define CONFIG_H
#define PROGRAM "yait"
#define ORGANIZATION "vx_clutch"
#define VERSION "pre-alpha"
#define YEAR 2025
#define LICENCE_LINE "License BSD-3-Clause: BSD-3-Clause <https://opensource.org/licenses/BSD-3-Clause>"
#endif

View File

@@ -7,19 +7,23 @@
#include <sys/stat.h>
#include <sys/types.h>
error_t touch(char *path, char *format, ...) {
error_t
touch (char *path, char *format, ...)
{
error_t err = { 0 };
err.null = true;
FILE *fp = fopen (path, "w");
if (!fp) {
if (!fp)
{
err.null = false;
err.status = errno;
err.src = strerror (errno);
return err;
}
else {
else
{
va_list args;
va_start (args, format);
vfprintf (fp, format, args);
@@ -30,7 +34,9 @@ error_t touch(char *path, char *format, ...) {
return err;
}
error_t dir(char *format, ...) {
error_t
dir (char *format, ...)
{
error_t err = { 0 };
err.null = true; // success by default
@@ -42,7 +48,8 @@ error_t dir(char *format, ...) {
va_end (args);
if (mkdir(path, 0777) < 0) {
if (mkdir (path, 0777) < 0)
{
err.null = false;
err.status = errno;
err.src = strerror (errno);
@@ -51,12 +58,16 @@ error_t dir(char *format, ...) {
return err;
}
error_t take(const char *dirname) {
error_t
take (const char *dirname)
{
error_t err = dir ("%s", dirname);
if (!err.null) {
if (!err.null)
{
return err;
}
if (chdir(dirname) != 0) {
if (chdir (dirname) != 0)
{
err.null = false;
err.status = errno;
err.src = strerror (errno);

29
core/standard.c Normal file
View File

@@ -0,0 +1,29 @@
#include "standard.h"
#include "../config.h"
#include "print.h"
#include <stdlib.h>
#include <string.h>
int
parse_standard_options (void (*usage) (), int argc, char **argv)
{
for (int i = 1; i < argc; ++i)
{
if (strcmp (argv[i], "--help") == 0)
{
usage ();
exit (0);
}
else if (strcmp (argv[i], "--version") == 0)
{
printfn ("%s (%s) %s\nCopyright (C) %d %s.\n%s\n"
"This is free software: you are free to change "
"and redistribute it.\nThere is NO WARRANTY, to the extent "
"permitted by law.",
PROGRAM, ORGANIZATION, VERSION, YEAR, ORGANIZATION,
LICENCE_LINE);
exit (0);
}
}
return -1;
}

6
core/standard.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef STANDARD_H
#define STANDARD_H
int parse_standard_options(void (*)(), int argc, char **argv);
#endif

View File

@@ -4,7 +4,7 @@
#include <stdbool.h>
typedef enum {
OPENBSD3,
BSD3,
GPLv3,
MIT,
UNLICENCE,
@@ -17,7 +17,8 @@ typedef enum {
} lib_t;
typedef struct {
bool nogit;
bool git;
bool clang_format;
licence_t licence;
char *name;
} format_t;

View File

@@ -1,9 +1,16 @@
#include "../core/file.h"
#include "../core/print.h"
#include "../core/standard.h"
#include "format.h"
#include <stdlib.h>
#include <string.h>
#define AUTHORS vx_clutch
int create (format_t);
void usage () {};
int
main (int argc, char **argv)
{
@@ -12,9 +19,17 @@ main (int argc, char **argv)
printfn ("error: not enough arguments.");
return 1;
}
int status = parse_standard_options (usage, argc, argv);
if (status)
{
printfn ("error: %s", strerror (status));
return 1;
}
format_t conf;
conf.name = argv[1];
conf.nogit = false;
conf.git = true;
conf.clang_format = true;
conf.licence = BSD3;
create (conf);
return 0;
}
@@ -23,10 +38,13 @@ int
create (format_t fmt)
{
error_t err = take (fmt.name);
if (!err.null) {
if (!err.null)
{
printfn ("failed to create or enter directory: %s", err.src);
return 1;
}
if (fmt.git)
system ("git init --quiet");
touch ("README",
"%s ( concise description )\n\n"
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
@@ -99,5 +117,17 @@ create (format_t fmt)
"printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
"printf \"done\\n\"\n");
if (fmt.clang_format)
touch (".clang-format", "Language: Cpp\nBasedOnStyle: GNU\n");
switch (fmt.licence)
{
case BSD3:
touch ("COPYING",
"https://raw.githubusercontent.com/teamdigitale/licenses/"
"refs/heads/master/BSD-3-Clause");
break;
default:
break;
}
return 0;
}