feat: standard argument parsing
This commit is contained in:
12
config.h
Normal file
12
config.h
Normal 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
|
||||||
85
core/file.c
85
core/file.c
@@ -7,60 +7,71 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
error_t touch(char *path, char *format, ...) {
|
error_t
|
||||||
error_t err = {0};
|
touch (char *path, char *format, ...)
|
||||||
|
{
|
||||||
|
error_t err = { 0 };
|
||||||
err.null = true;
|
err.null = true;
|
||||||
|
|
||||||
FILE *fp = fopen(path, "w");
|
FILE *fp = fopen (path, "w");
|
||||||
if (!fp) {
|
if (!fp)
|
||||||
err.null = false;
|
{
|
||||||
err.status = errno;
|
err.null = false;
|
||||||
err.src = strerror(errno);
|
err.status = errno;
|
||||||
return err;
|
err.src = strerror (errno);
|
||||||
}
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
else {
|
else
|
||||||
va_list args;
|
{
|
||||||
va_start(args, format);
|
va_list args;
|
||||||
vfprintf(fp, format, args);
|
va_start (args, format);
|
||||||
va_end(args);
|
vfprintf (fp, format, args);
|
||||||
}
|
va_end (args);
|
||||||
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose (fp);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_t dir(char *format, ...) {
|
error_t
|
||||||
error_t err = {0};
|
dir (char *format, ...)
|
||||||
|
{
|
||||||
|
error_t err = { 0 };
|
||||||
err.null = true; // success by default
|
err.null = true; // success by default
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start (args, format);
|
||||||
|
|
||||||
char path[1024];
|
char path[1024];
|
||||||
vsnprintf(path, sizeof(path), format, args);
|
vsnprintf (path, sizeof (path), format, args);
|
||||||
|
|
||||||
va_end(args);
|
va_end (args);
|
||||||
|
|
||||||
if (mkdir(path, 0777) < 0) {
|
if (mkdir (path, 0777) < 0)
|
||||||
err.null = false;
|
{
|
||||||
err.status = errno;
|
err.null = false;
|
||||||
err.src = strerror(errno);
|
err.status = errno;
|
||||||
}
|
err.src = strerror (errno);
|
||||||
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_t take(const char *dirname) {
|
error_t
|
||||||
error_t err = dir("%s", dirname);
|
take (const char *dirname)
|
||||||
if (!err.null) {
|
{
|
||||||
return err;
|
error_t err = dir ("%s", dirname);
|
||||||
}
|
if (!err.null)
|
||||||
if (chdir(dirname) != 0) {
|
{
|
||||||
err.null = false;
|
return err;
|
||||||
err.status = errno;
|
}
|
||||||
err.src = strerror(errno);
|
if (chdir (dirname) != 0)
|
||||||
return err;
|
{
|
||||||
}
|
err.null = false;
|
||||||
|
err.status = errno;
|
||||||
|
err.src = strerror (errno);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
29
core/standard.c
Normal file
29
core/standard.c
Normal 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
6
core/standard.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef STANDARD_H
|
||||||
|
#define STANDARD_H
|
||||||
|
|
||||||
|
int parse_standard_options(void (*)(), int argc, char **argv);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OPENBSD3,
|
BSD3,
|
||||||
GPLv3,
|
GPLv3,
|
||||||
MIT,
|
MIT,
|
||||||
UNLICENCE,
|
UNLICENCE,
|
||||||
@@ -17,7 +17,8 @@ typedef enum {
|
|||||||
} lib_t;
|
} lib_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool nogit;
|
bool git;
|
||||||
|
bool clang_format;
|
||||||
licence_t licence;
|
licence_t licence;
|
||||||
char *name;
|
char *name;
|
||||||
} format_t;
|
} format_t;
|
||||||
|
|||||||
42
yait/main.c
42
yait/main.c
@@ -1,9 +1,16 @@
|
|||||||
#include "../core/file.h"
|
#include "../core/file.h"
|
||||||
#include "../core/print.h"
|
#include "../core/print.h"
|
||||||
|
#include "../core/standard.h"
|
||||||
#include "format.h"
|
#include "format.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define AUTHORS vx_clutch
|
||||||
|
|
||||||
int create (format_t);
|
int create (format_t);
|
||||||
|
|
||||||
|
void usage () {};
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -12,9 +19,17 @@ main (int argc, char **argv)
|
|||||||
printfn ("error: not enough arguments.");
|
printfn ("error: not enough arguments.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
int status = parse_standard_options (usage, argc, argv);
|
||||||
|
if (status)
|
||||||
|
{
|
||||||
|
printfn ("error: %s", strerror (status));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
format_t conf;
|
format_t conf;
|
||||||
conf.name = argv[1];
|
conf.name = argv[1];
|
||||||
conf.nogit = false;
|
conf.git = true;
|
||||||
|
conf.clang_format = true;
|
||||||
|
conf.licence = BSD3;
|
||||||
create (conf);
|
create (conf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -22,11 +37,14 @@ main (int argc, char **argv)
|
|||||||
int
|
int
|
||||||
create (format_t fmt)
|
create (format_t fmt)
|
||||||
{
|
{
|
||||||
error_t err = take(fmt.name);
|
error_t err = take (fmt.name);
|
||||||
if (!err.null) {
|
if (!err.null)
|
||||||
printfn("failed to create or enter directory: %s", err.src);
|
{
|
||||||
return 1;
|
printfn ("failed to create or enter directory: %s", err.src);
|
||||||
}
|
return 1;
|
||||||
|
}
|
||||||
|
if (fmt.git)
|
||||||
|
system ("git init --quiet");
|
||||||
touch ("README",
|
touch ("README",
|
||||||
"%s ( concise description )\n\n"
|
"%s ( concise description )\n\n"
|
||||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
"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 \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
|
||||||
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
|
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
|
||||||
"printf \"done\\n\"\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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user