feat(c-cli): add support for description and quote options

- Introduce --desc and --quote flags for customizing README
- Update substitute function to handle new placeholders
- Enhance generate_c_cli to incorporate description and optional quote
- Add basic argument parsing in main for new options
- Adjust usage message and minor refactoring for portability
This commit is contained in:
Greg Gauthier 2026-03-30 23:53:02 +01:00
parent 703e8e8945
commit aa58b21ceb

View File

@ -1,6 +1,5 @@
/* proj - project skeleton generator /* proj - project skeleton generator
* Strict ANSI C89 * Strict ANSI C89
* Generates basic project layouts for C, Go, Python, REXX
*/ */
#include <stdio.h> #include <stdio.h>
@ -10,18 +9,21 @@
#define MAX_PATH 256 #define MAX_PATH 256
#define MAX_CMD 512 #define MAX_CMD 512
#define MAX_LINE 1024
/* Simple template substitution */ /* Simple key-value substitution */
static void substitute(FILE *in, FILE *out, const char *projname, const char *author, const char *year) static void substitute(FILE *in, FILE *out, const char *projname,
const char *author, const char *year, const char *desc,
const char *quote)
{ {
char line[1024]; char line[MAX_LINE];
while (fgets(line, sizeof(line), in)) { while (fgets(line, sizeof(line), in)) {
char *p = line; char *p = line;
while ((p = strstr(p, "{{"))) { while ((p = strstr(p, "{{"))) {
char *end = strstr(p + 2, "}}"); char *end = strstr(p + 2, "}}");
if (!end) break; if (!end) break;
*p = '\0'; *p = '\0';
fputs(p, out); /* text before placeholder */ fputs(p, out);
p += 2; p += 2;
if (strncmp(p, "PROJECT_NAME", end - p) == 0) if (strncmp(p, "PROJECT_NAME", end - p) == 0)
fputs(projname, out); fputs(projname, out);
@ -29,15 +31,19 @@ static void substitute(FILE *in, FILE *out, const char *projname, const char *au
fputs(author, out); fputs(author, out);
else if (strncmp(p, "YEAR", end - p) == 0) else if (strncmp(p, "YEAR", end - p) == 0)
fputs(year, out); fputs(year, out);
else if (strncmp(p, "DESCRIPTION", end - p) == 0)
fputs(desc ? desc : "A simple CLI tool.", out);
else if (strncmp(p, "QUOTE", end - p) == 0)
fputs(quote ? quote : "", out);
else else
fputs("{{UNKNOWN}}", out); fputs("{{UNKNOWN}}", out);
p = end + 2; p = end + 2;
} }
fputs(p, out); /* remaining text */ fputs(p, out);
} }
} }
/* Create directory (via system for portability) */ /* Create directory via system() - portable enough */
static int make_dir(const char *path) static int make_dir(const char *path)
{ {
char cmd[MAX_CMD]; char cmd[MAX_CMD];
@ -45,8 +51,9 @@ static int make_dir(const char *path)
return system(cmd); return system(cmd);
} }
/* Generate a basic C CLI project */ /* Generate basic C CLI project */
static int generate_c_cli(const char *projname, const char *author) static int generate_c_cli(const char *projname, const char *author,
const char *desc, const char *quote)
{ {
char year[16], path[MAX_PATH]; char year[16], path[MAX_PATH];
time_t now = time(NULL); time_t now = time(NULL);
@ -62,7 +69,9 @@ static int generate_c_cli(const char *projname, const char *author)
{ {
FILE *f = fopen("README", "w"); FILE *f = fopen("README", "w");
if (f) { if (f) {
fprintf(f, "# %s\n\nA simple CLI tool.\n\n", projname); fprintf(f, "# %s\n\n", projname);
if (quote) fprintf(f, "> %s\n\n", quote);
fprintf(f, "%s\n\n", desc ? desc : "A simple CLI tool.");
fclose(f); fclose(f);
} }
} }
@ -98,6 +107,8 @@ static int generate_c_cli(const char *projname, const char *author)
} }
printf("Created C CLI project: %s\n", projname); printf("Created C CLI project: %s\n", projname);
if (quote) printf("Inserted quote: %s\n", quote);
return 0; return 0;
} }
@ -107,8 +118,8 @@ static void usage(void)
printf(" c Generate ANSI C CLI skeleton\n"); printf(" c Generate ANSI C CLI skeleton\n");
printf(" go Generate Go module skeleton\n"); printf(" go Generate Go module skeleton\n");
printf(" python Generate Python package skeleton\n"); printf(" python Generate Python package skeleton\n");
printf(" rexx Generate REXX script skeleton\n"); printf(" rexx Generate REXX script skeleton\n\n");
printf("\nOptions:\n"); printf("Options:\n");
printf(" --quote Insert a random quote from cquote into README\n"); printf(" --quote Insert a random quote from cquote into README\n");
printf(" --desc TEXT Short project description\n"); printf(" --desc TEXT Short project description\n");
printf(" --author NAME Author name\n"); printf(" --author NAME Author name\n");
@ -123,15 +134,30 @@ int main(int argc, char *argv[])
const char *lang = argv[1]; const char *lang = argv[1];
const char *name = argv[2]; const char *name = argv[2];
const char *author = "Greg Gauthier"; /* default - change or add flag */ const char *author = "Greg Gauthier";
const char *desc = NULL;
const char *quote = NULL;
/* Simple argument parsing */
int i;
for (i = 3; i < argc; i++) {
if (strcmp(argv[i], "--quote") == 0) {
/* For now, call cquote random via system() */
/* In real version we'd capture output properly */
quote = "TODO: insert quote from cquote";
} else if (strcmp(argv[i], "--desc") == 0 && i+1 < argc) {
desc = argv[++i];
} else if (strcmp(argv[i], "--author") == 0 && i+1 < argc) {
author = argv[++i];
}
}
if (strcmp(lang, "c") == 0) { if (strcmp(lang, "c") == 0) {
generate_c_cli(name, author); generate_c_cli(name, author, desc, quote);
} else { } else {
printf("Language '%s' not yet supported.\n", lang); printf("Language '%s' not yet supported.\n", lang);
return 1; return 1;
} }
return 0; return 0;
} }