From 31bf64a5d75c318ac384acda9d2930dceba57e6f Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Wed, 28 Apr 2021 09:32:47 -0400 Subject: [PATCH 1/2] use argv0 to get the program name --- src/cli/main.c | 12 ++++++++---- src/cli/path.c | 14 ++++++++++++++ src/cli/path.h | 3 +++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index a277537a..3e06111d 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -3,13 +3,17 @@ #include "os.h" #include "vm.h" +#include "path.h" #include "wren.h" -int main(int argc, const char* argv[]) -{ +int main(int argc, const char* argv[]) { + Path* p = pathNew(argv[0]); + pathBaseName(p); + char* cli = p->chars; + if (argc == 2 && strcmp(argv[1], "--help") == 0) { - printf("Usage: wren [file] [arguments...]\n"); + printf("Usage: %s [file] [arguments...]\n", cli); printf("\n"); printf("Optional arguments:\n"); printf(" --help Show command line usage\n"); @@ -19,7 +23,7 @@ int main(int argc, const char* argv[]) if (argc == 2 && strcmp(argv[1], "--version") == 0) { - printf("wren %s\n", WREN_VERSION_STRING); + printf("%s %s\n", cli, WREN_VERSION_STRING); return 0; } diff --git a/src/cli/path.c b/src/cli/path.c index 6ad34a90..b4f049ca 100644 --- a/src/cli/path.c +++ b/src/cli/path.c @@ -131,6 +131,20 @@ void pathFree(Path* path) free(path); } +void pathBaseName(Path* path) { + int pos = 0; + for (size_t i = path->length - 1; i >=0 ; i--) + { + if (isSeparator(path->chars[i])) + { + pos = i; + break; + } + } + if (pos == 0) return; + strcpy(path->chars, (char*)path->chars + pos + 1); +} + void pathDirName(Path* path) { // Find the last path separator. diff --git a/src/cli/path.h b/src/cli/path.h index e87bc5da..41cbfe4d 100644 --- a/src/cli/path.h +++ b/src/cli/path.h @@ -41,6 +41,9 @@ void pathFree(Path* path); // Strips off the last component of the path name. void pathDirName(Path* path); +// returns the basename of the path +void pathBaseName(Path* path); + // Strips off the file extension from the last component of the path. void pathRemoveExtension(Path* path); From 34cf04f7833e5b1c5aee0d5b04213e906b0f2cfd Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Wed, 28 Apr 2021 09:44:47 -0400 Subject: [PATCH 2/2] update help, add -v -h --- src/cli/main.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cli/main.c b/src/cli/main.c index 3e06111d..a6e729d2 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -11,17 +11,17 @@ int main(int argc, const char* argv[]) { pathBaseName(p); char* cli = p->chars; - if (argc == 2 && strcmp(argv[1], "--help") == 0) + if (argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)) { - printf("Usage: %s [file] [arguments...]\n", cli); + printf("Usage: %s [options] [ file.wren ] [arguments...] \n", cli); printf("\n"); - printf("Optional arguments:\n"); - printf(" --help Show command line usage\n"); - printf(" --version Show version\n"); + printf("Options:\n"); + printf(" -h, --help Show command line usage\n"); + printf(" -v, --version Show version\n"); return 0; } - if (argc == 2 && strcmp(argv[1], "--version") == 0) + if (argc == 2 && (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-v") == 0)) { printf("%s %s\n", cli, WREN_VERSION_STRING); return 0;