From 9b8e34fe06173e7aa10610a1b099b3d8fa8fd4cc Mon Sep 17 00:00:00 2001 From: Sandro Kalatozishvili Date: Thu, 17 Aug 2023 00:58:10 +0400 Subject: [PATCH] CLI option to print version --- src/cfg.c | 5 ++++- src/info.c | 16 +++++++++++----- src/info.h | 3 ++- src/smake.c | 9 +++++++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index b4eb27a..34c99d3 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -237,7 +237,7 @@ static xbool_t SMake_AddFindObject(smake_ctx_t *pCtx, xjson_obj_t *pFindObj, xbo int SMake_ParseArgs(smake_ctx_t *pCtx, int argc, char *argv[]) { int nChar = 0; - while ((nChar = getopt(argc, argv, "o:s:c:e:b:i:f:g:l:p:v:L:I1:d1:j1:w1:x1:h1")) != -1) + while ((nChar = getopt(argc, argv, "o:s:c:e:b:i:f:g:l:p:v:L:V1:I1:d1:j1:w1:x1:h1")) != -1) { switch (nChar) { @@ -294,6 +294,9 @@ int SMake_ParseArgs(smake_ctx_t *pCtx, int argc, char *argv[]) case 'I': pCtx->bInitProj = XTRUE; break; + case 'V': + SMake_PrintVersion(argv[0]); + return XFALSE; case 'h': default: SMake_Usage(argv[0]); diff --git a/src/info.c b/src/info.c index 229d366..fa5acdb 100644 --- a/src/info.c +++ b/src/info.c @@ -28,10 +28,15 @@ const char* SMake_VersionShort() return sVersion; } +void SMake_PrintVersion(const char *pName) +{ + printf("%s - v%s (email: s.kalatoz@gmail.com)\n", pName, SMake_Version()); +} + void SMake_Greet(const char *pName) { printf("=============================================================================\n"); - printf("%s Version: %s (email: s.kalatoz@gmail.com)\n", pName, SMake_Version()); + SMake_PrintVersion(pName); printf("=============================================================================\n"); } @@ -52,9 +57,9 @@ void SMake_Usage(const char *pName) SMake_Greet(SMAKE_FULL_NAME); int nLength = strlen(pName) + 6; - printf("Usage: %s [-f <'flags'>] [-b ] [-i ] [-c ] [-I]\n", pName); - printf(" %s [-l <'libs'>] [-e ] [-g ] [-o ] [-j] \n", WhiteSpace(nLength)); - printf(" %s [-L <'libs'>] [-p ] [-s ] [-d] [-v] [-w] [-x] [-h]\n", WhiteSpace(nLength)); + printf("Usage: %s [-f <'flags'>] [-b ] [-i ] [-c ] [-I] [-V]\n", pName); + printf(" %s [-l <'libs'>] [-e ] [-g ] [-o ] [-d] [-j]\n", WhiteSpace(nLength)); + printf(" %s [-L <'libs'>] [-p ] [-s ] [-v ] [-w] [-x] [-h]\n", WhiteSpace(nLength)); printf("Options are:\n"); printf(" -f <'flags'> # Compiler flags\n"); printf(" -l <'libs'> # Linked libraries\n"); @@ -67,10 +72,11 @@ void SMake_Usage(const char *pName) printf(" -o # Object output destination\n"); printf(" -p # Program or library name\n"); printf(" -s # Path to source files\n"); + printf(" -v # Verbosity level\n"); + printf(" -V # Print version and exit\n"); printf(" -I # Initialize project\n"); printf(" -j # Generate smake.json\n"); printf(" -d # Virtual directory\n"); - printf(" -v # Verbosity level\n"); printf(" -w # Force overwrite output\n"); printf(" -x # Create Makefile for CPP\n"); printf(" -h # Print version and usage\n\n"); diff --git a/src/info.h b/src/info.h index 300a73b..b7d6fcf 100644 --- a/src/info.h +++ b/src/info.h @@ -11,7 +11,7 @@ #define SMAKE_VERSION_MAX 1 #define SMAKE_VERSION_MIN 1 -#define SMAKE_BUILD_NUMBER 17 +#define SMAKE_BUILD_NUMBER 18 #ifndef _SMAKE_VERSION_H_ #define _SMAKE_VERSION_H_ @@ -26,6 +26,7 @@ const char* SMake_VersionShort(); void SMake_Greet(const char *pName); void SMake_Usage(const char *pName); +void SMake_PrintVersion(const char *pName); /* For include header in CPP code */ #ifdef __cplusplus diff --git a/src/smake.c b/src/smake.c index 0982834..362abc9 100644 --- a/src/smake.c +++ b/src/smake.c @@ -21,8 +21,13 @@ int main(int argc, char *argv[]) smake_ctx_t smake; SMake_InitContext(&smake); - if (!SMake_ParseArgs(&smake, argc, argv) || - !SMake_ParseConfig(&smake) || + if (!SMake_ParseArgs(&smake, argc, argv)) + { + SMake_ClearContext(&smake); + return XSTDNON; + } + + if (!SMake_ParseConfig(&smake) || !SMake_InitProject(&smake) || !SMake_LoadFiles(&smake, NULL) || !SMake_ParseProject(&smake) ||