char *command;
size_t n_paths;
char **paths;
+ char **argv;
+ njs_uint_t argc;
} njs_opts_t;
vm_options.ops = &njs_console_ops;
vm_options.external = &njs_console;
- vm_options.argv = argv;
- vm_options.argc = argc;
+ vm_options.argv = opts.argv;
+ vm_options.argc = opts.argc;
if (opts.interactive) {
ret = njs_interactive_shell(&opts, &vm_options);
static njs_int_t
njs_get_options(njs_opts_t *opts, int argc, char **argv)
{
- char *p, **paths;
- njs_int_t i, ret;
+ char *p, **paths;
+ njs_int_t i, ret;
+ njs_uint_t n;
static const char help[] =
"Interactive njs shell.\n"
"\n"
+ "njs [options] [-c string | script.js | -] [script args]"
+ "\n"
"Options:\n"
" -c specify the command to execute.\n"
" -d print disassembled code.\n"
" -t script|module source code type (script is default).\n"
" -v print njs version and exit.\n"
" -u disable \"unsafe\" mode.\n"
- " <filename> | - run code from a file or stdin.\n";
+ " script.js | - run code from a file or stdin.\n";
ret = NJS_DONE;
if (p[0] != '-' || (p[0] == '-' && p[1] == '\0')) {
opts->interactive = 0;
opts->file = argv[i];
- continue;
+ goto done;
}
p++;
if (++i < argc) {
opts->command = argv[i];
- break;
+ goto done;
}
njs_stderror("option \"-c\" requires argument\n");
}
}
+done:
+
+ opts->argc = njs_max(argc - i + 1, 2);
+ opts->argv = malloc(sizeof(char*) * opts->argc);
+ if (opts->argv == NULL) {
+ njs_stderror("failed to alloc argv\n");
+ return NJS_ERROR;
+ }
+
+ opts->argv[0] = argv[0];
+ opts->argv[1] = (opts->file != NULL) ? opts->file : (char *) "";
+ for (n = 2; n < opts->argc; n++) {
+ opts->argv[n] = argv[i + n - 1];
+ }
+
return NJS_OK;
}
# process
njs_run {"-c" "console.log(typeof process.argv)"} "object"
-njs_run {"-c" "console.log(process.argv[3])" "AAA"} "AAA"
+njs_run {"-c" "console.log(process.argv.slice(2))" "AAA"} "AAA"
njs_run {"-c" "console.log(typeof process.env)"} "object"
njs_run {"-c" "console.log(process.env.HOME != undefined)"} "true"
njs_run {"-c" "console.log(process.ppid)"} "\\d+"
+# script args
+
+njs_run {"test/script_args.js" "A" "B"} "AB"
+
# disassemble
njs_test {