feat: argument parse 1
This commit is contained in:
8
TODO
8
TODO
@@ -1,8 +1,10 @@
|
|||||||
VX yait --- TODO
|
VX yait --- TODO
|
||||||
|
|
||||||
Todo:
|
Todo:
|
||||||
|
* Project formats
|
||||||
* Argument Parsing
|
* GNU
|
||||||
* Project creation
|
* FASM
|
||||||
|
* POSIX
|
||||||
|
* LIBRARY
|
||||||
|
|
||||||
end of file TODO
|
end of file TODO
|
||||||
|
|||||||
@@ -10,14 +10,8 @@
|
|||||||
#define YAIT_H
|
#define YAIT_H
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool posix;
|
|
||||||
bool git;
|
bool git;
|
||||||
bool clang;
|
bool clang;
|
||||||
bool lib;
|
|
||||||
bool cc;
|
|
||||||
bool gnu;
|
|
||||||
/* If this flag is set it will ignore: GNU, and enforce POSIX. */
|
|
||||||
bool simple;
|
|
||||||
} flag_t;
|
} flag_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -35,10 +29,19 @@ typedef enum {
|
|||||||
UNL,
|
UNL,
|
||||||
} licence_t;
|
} licence_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
POSIX,
|
||||||
|
SIMPLE,
|
||||||
|
GNU,
|
||||||
|
LIBRARY,
|
||||||
|
FASM,
|
||||||
|
} style_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
libmap_t libraries;
|
libmap_t libraries;
|
||||||
licence_t licence;
|
licence_t licence;
|
||||||
flag_t flags;
|
flag_t flags;
|
||||||
|
style_t style;
|
||||||
|
|
||||||
char *project;
|
char *project;
|
||||||
char *name;
|
char *name;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ int create_project(manifest_t manifest)
|
|||||||
mkdir_p(manifest.project);
|
mkdir_p(manifest.project);
|
||||||
chdir(manifest.project);
|
chdir(manifest.project);
|
||||||
|
|
||||||
if (manifest.flags.simple) {
|
if (manifest.style == SIMPLE) {
|
||||||
/* This only works if the source
|
/* This only works if the source
|
||||||
files's root name is the same as the target on all of the Makefile becuase of how it checks for files. */
|
files's root name is the same as the target on all of the Makefile becuase of how it checks for files. */
|
||||||
cfprintf(
|
cfprintf(
|
||||||
|
|||||||
71
src/main.c
71
src/main.c
@@ -24,6 +24,15 @@
|
|||||||
#define print_option(option, description) \
|
#define print_option(option, description) \
|
||||||
printf(" %-20s %-20s\n", option, description)
|
printf(" %-20s %-20s\n", option, description)
|
||||||
|
|
||||||
|
char *str_dup(char *s)
|
||||||
|
{
|
||||||
|
char *new = malloc(strlen(s) + 1);
|
||||||
|
if (!new)
|
||||||
|
return NULL;
|
||||||
|
strcpy(new, s);
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
static void usage(int status)
|
static void usage(int status)
|
||||||
{
|
{
|
||||||
if (status != 0) {
|
if (status != 0) {
|
||||||
@@ -35,42 +44,59 @@ static void usage(int status)
|
|||||||
puts("Creates a C project with opinionated defaults");
|
puts("Creates a C project with opinionated defaults");
|
||||||
puts("When only given the first argument it will detect your name\n");
|
puts("When only given the first argument it will detect your name\n");
|
||||||
puts("Mandatory arguments to long options are mandatory for short options too");
|
puts("Mandatory arguments to long options are mandatory for short options too");
|
||||||
print_option("--posix", "Enable POSIX compliance");
|
print_option("--no-git", "Do not inititize git reposity");
|
||||||
print_option("--git", "Do not inititize git reposity");
|
|
||||||
print_option("--clang", "Add clang-format files and tooling");
|
print_option("--clang", "Add clang-format files and tooling");
|
||||||
print_option("-L <licence>", "Set licence");
|
print_option("-L <licence>", "Set licence");
|
||||||
print_option("-l <library>", "Add a library");
|
print_option("-l <library>", "Add a library");
|
||||||
print_option("-C", "Use C++");
|
print_option("-n <name>", "Set the name of the project");
|
||||||
print_option("--GNU", "Add version and help parsing");
|
print_option(
|
||||||
print_option("-S, --simple", "Enable simple mode");
|
"--style=<style>",
|
||||||
|
"Pick from a list of built in styles. This list can be found by passing 'list'");
|
||||||
puts(" --help display this help text and exit\n");
|
puts(" --help display this help text and exit\n");
|
||||||
puts(" --version output version information and exit\n");
|
puts(" --version output version information and exit\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
[[maybe_unused]] static int parse_arguments(manifest_t *conf, int argc,
|
static int parse_arguments(manifest_t *conf, int argc, char **argv)
|
||||||
char **argv)
|
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "L:l:CS")) != -1) {
|
// clang-format off
|
||||||
|
static struct option long_opts[] = {
|
||||||
|
{ "style", required_argument, 0, 's' },
|
||||||
|
{ "no-git", no_argument, 0, 'g' },
|
||||||
|
{ "clang", no_argument, 0, 'c' },
|
||||||
|
{ 0, 0, 0, 0 } };
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
// TODO(vx-clutch): lL
|
||||||
|
while ((opt = getopt_long(argc, argv, "s:gcn", long_opts, NULL)) !=
|
||||||
|
-1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'L':
|
case 's':
|
||||||
conf->licence = TOlicence(optarg);
|
if (strcmp(optarg, "list") == 0) {
|
||||||
|
fputs("", stderr);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'g':
|
||||||
|
conf->flags.git = false;
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'c':
|
||||||
conf->flags.cc = true;
|
conf->flags.clang = true;
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'n':
|
||||||
conf->flags.simple = true;
|
conf->name = str_dup(optarg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Usage: %s [--help]\n", argv[0]);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (optind >= argc) {
|
||||||
|
fputs("error: missing required positional argument", stderr);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
conf->project = str_dup(argv[optind]);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,19 +136,16 @@ int main(int argc, char **argv)
|
|||||||
.project = "Project",
|
.project = "Project",
|
||||||
.name = "author",
|
.name = "author",
|
||||||
.licence = UNL,
|
.licence = UNL,
|
||||||
|
.style = SIMPLE,
|
||||||
|
|
||||||
.libraries.ncurses = false,
|
.libraries.ncurses = false,
|
||||||
.libraries.raylib = false,
|
.libraries.raylib = false,
|
||||||
.libraries.stb = false,
|
.libraries.stb = false,
|
||||||
.libraries.uthash = false,
|
.libraries.uthash = false,
|
||||||
.libraries.linenoise = false,
|
.libraries.linenoise = false,
|
||||||
|
|
||||||
.flags.posix = false,
|
|
||||||
.flags.git = true,
|
.flags.git = true,
|
||||||
.flags.clang = false,
|
.flags.clang = false,
|
||||||
.flags.lib = false,
|
|
||||||
.flags.cc = false,
|
|
||||||
.flags.gnu = true,
|
|
||||||
.flags.simple = false,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
status = parse_standard_options(usage, argc, argv);
|
status = parse_standard_options(usage, argc, argv);
|
||||||
@@ -130,8 +153,10 @@ int main(int argc, char **argv)
|
|||||||
return fprintf(stderr, "error: %s\n", strerror(status)),
|
return fprintf(stderr, "error: %s\n", strerror(status)),
|
||||||
EXIT_FAILURE;
|
EXIT_FAILURE;
|
||||||
|
|
||||||
// parse_arguments(&manifest, argc, argv);
|
status = parse_arguments(&manifest, argc, argv);
|
||||||
manifest.flags.simple = true;
|
if (status) {
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
get_name(&manifest.name);
|
get_name(&manifest.name);
|
||||||
status = create_project(manifest);
|
status = create_project(manifest);
|
||||||
|
|||||||
Reference in New Issue
Block a user