From 612d65d57f5287094f363b77df9f90eb645eb266 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 19 Sep 2026 22:30:59 +0200 Subject: feat: add version/help --- Makefile | 10 +++++++++- README | 2 ++ main.c | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 97a3646..fb7a0d8 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,9 @@ CFLAGS = -std=c17 -Wall -Wextra -Werror -Wpedantic -O2 -Ibin SOURCES = ast.c ast.h eval.c eval.h lexer.h main.c OBJECTS = bin/lexer.o bin/parser.o bin/ast.o bin/eval.o bin/main.o SANITIZE = -g -O0 -fsanitize=address,undefined +PREFIX ?= /usr/local -.PHONY: all debug check format lint clean +.PHONY: all debug check format lint install uninstall clean all: sfinae @@ -50,5 +51,12 @@ format: lint: clang-tidy ast.c eval.c main.c -- -std=c17 -Ibin +install: sfinae + install -d $(DESTDIR)$(PREFIX)/bin + install -m 755 sfinae $(DESTDIR)$(PREFIX)/bin/sfinae + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/sfinae + clean: rm -rf bin sfinae diff --git a/README b/README index 32e77c7..eea77c0 100644 --- a/README +++ b/README @@ -11,6 +11,8 @@ a toy language which takes' C++ template metaprogramming to another level. make format clang-format the sources make lint clang-tidy naming check make clean + make install copy ./sfinae to PREFIX/bin, PREFIX defaults to /usr/local + make uninstall ## run diff --git a/main.c b/main.c index db2c879..a3d1caa 100644 --- a/main.c +++ b/main.c @@ -1,9 +1,13 @@ +#include #include +#include #include "ast.h" #include "eval.h" #include "lexer.h" +#define VERSION "0.1.0" + extern struct Node* program_root; int yyparse(void); @@ -24,14 +28,49 @@ static int run_program(void) return status; } +static void print_usage(FILE* stream) +{ + (void)fprintf(stream, + "usage: sfinae \n" + " sfinae [-v | -h]\n"); +} + +static void print_help(void) +{ + (void)printf("sfinae " VERSION " - substitution failure is not an error\n\n" + "a toy language inspired by c++ template meta programming\n\n"); + print_usage(stdout); + (void)printf("\noptions:\n" + " -v, --version print version\n" + " -h, --help print this\n\n" + "by hachem \n"); +} + +static bool is_flag(const char* argument, const char* brief, const char* full) +{ + return strcmp(argument, brief) == 0 || strcmp(argument, full) == 0; +} + int main(int argc, char** argv) { if (argc != 2) { - (void)fprintf(stderr, "usage: sfinae \n"); + print_usage(stderr); return 2; } + if (is_flag(argv[1], "-h", "--help")) + { + print_help(); + return 0; + } + + if (is_flag(argv[1], "-v", "--version")) + { + (void)printf("sfinae " VERSION "\n"); + return 0; + } + yyin = fopen(argv[1], "r"); if (yyin == NULL) { -- cgit v1.3