diff options
author | Evan Hunter <evan@ozhiker.com> | 2015-11-21 15:01:31 +0000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2015-11-30 09:23:21 +1000 |
commit | e56199a18844adff0b0d393a378ced02c9a4cd77 (patch) | |
tree | 1308d87cf3d2979a8dccc0c04392e62656e86a7d /jimsh.c | |
parent | ee2b03b01aa4148199b6fbc0839462be48e89034 (diff) | |
download | jimtcl-e56199a18844adff0b0d393a378ced02c9a4cd77.zip jimtcl-e56199a18844adff0b0d393a378ced02c9a4cd77.tar.gz jimtcl-e56199a18844adff0b0d393a378ced02c9a4cd77.tar.bz2 |
jimsh: Allow option "--help" and enable place for parsing other options in future
Diffstat (limited to 'jimsh.c')
-rw-r--r-- | jimsh.c | 28 |
1 files changed, 27 insertions, 1 deletions
@@ -65,15 +65,38 @@ static void JimPrintErrorMessage(Jim_Interp *interp) fprintf(stderr, "%s\n", Jim_String(Jim_GetResult(interp))); } +void usage(const char* executable_name) +{ + printf("jimsh version %d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); + printf("Usage: %s\n", executable_name); + printf("or : %s [options] [filename]\n", executable_name); + printf("\n"); + printf("Without options: Interactive mode\n"); + printf("\n"); + printf("Options:\n"); + printf(" --version : prints the version string\n"); + printf(" --help : prints this text\n"); + printf(" -e CMD : executes command CMD\n"); + printf(" NOTE: all subsequent options will be passed as arguments to the command\n"); + printf(" [filename] : executes the script contained in the named file\n"); + printf(" NOTE: all subsequent options will be passed to the script\n\n"); +} + int main(int argc, char *const argv[]) { int retcode; Jim_Interp *interp; + char *const orig_argv0 = argv[0]; + /* Parse initial arguments before interpreter is started */ if (argc > 1 && strcmp(argv[1], "--version") == 0) { printf("%d.%d\n", JIM_VERSION / 100, JIM_VERSION % 100); return 0; } + else if (argc > 1 && strcmp(argv[1], "--help") == 0) { + usage(argv[0]); + return 0; + } /* Create and initialize the interpreter */ interp = Jim_CreateInterp(); @@ -84,11 +107,12 @@ int main(int argc, char *const argv[]) JimPrintErrorMessage(interp); } - Jim_SetVariableStrWithStr(interp, "jim::argv0", argv[0]); + Jim_SetVariableStrWithStr(interp, "jim::argv0", orig_argv0); Jim_SetVariableStrWithStr(interp, JIM_INTERACTIVE, argc == 1 ? "1" : "0"); retcode = Jim_initjimshInit(interp); if (argc == 1) { + /* Executable name is the only argument - start interactive prompt */ if (retcode == JIM_ERR) { JimPrintErrorMessage(interp); } @@ -98,7 +122,9 @@ int main(int argc, char *const argv[]) } } else { + /* Additional arguments - interpret them */ if (argc > 2 && strcmp(argv[1], "-e") == 0) { + /* Evaluate code in subsequent argument */ JimSetArgv(interp, argc - 3, argv + 3); retcode = Jim_Eval(interp, argv[2]); if (retcode != JIM_ERR) { |