Is your feature request related to a problem? Please describe.
I'm working on my own programming language, and I'd like to be able to create
an interpreter in node.js that would accept this input:
node my-interpreter.mjs eval "Two times " ++ Pi ++ " equals " ++ (2 * Pi)
> Two times 3.14159 equals 6.28319
Anything beyond node my-interpreter.mjs eval is code written in my programming
language. I need this input in raw form to be able to parse the code. It is not possible
to reconstruct the code from process.argv.
Describe the solution you'd like
A string property like rawArgv on process variable.
Describe alternatives you've considered
It would be possible to put the whole code in quotes and escape all quotes in the code, but in my view this would make the command-line "API" much uglier.
It is not possible
to reconstruct the code fromprocess.argv.
Why not?
Why not?
More specifically, why doesn't this or something like it work for your use case?
process.argv.slice(2).join(process.env.IFS || ' ')
It would be possible to put the whole code in quotes and escape all quotes in the code, but in my view this would make the command-line "API" much uglier.
This is the thing on how shell handles the arguments but not Node.js. It's notable that while we write
$ echo "bar"
bar
It's not the program echo who stripped the quotes, but shell does.
It would be possible to put the whole code in quotes and escape all quotes in the code, but in my view this would make the command-line "API" much uglier.
This is the thing on how shell handles the arguments but not Node.js. It's notable that while we write
$ echo "bar" barIt's not the program
echowho stripped the quotes, but shell does.
Yeah, in that situation, I would assume @mik-jozef would backslash-escape quotation marks and other special characters as necessary at invocation time. But if the expectation here is to make that unnecessary, then that may not be reasonably possible.
Why not?
More specifically, why doesn't this or something like it work for your use case?
process.argv.slice(2).join(process.env.IFS || ' ')
Because the parsing of cli arguments is many-to-one (ie. not injective) and programs of my language with different semantics get lumped together.
Two specific issues I can think of right now:
process.argv, I can't even find out whether it was quoted or not! This makes parsing from process.argv impossible.
- String literals in my language must use double quotes, single quotes are a syntax error. Info about which quotes were used is lost during argument parsing. EDIT: it's worse than this. Given an argument from
process.argv, I can't even find out whether it was quoted or not! This makes parsing fromprocess.argvimpossible.- My language may (I'm not entirely decided yet) contain significant whitespace, but spaces between arguments are again forgotten.
I don't think there's anything Node.js can do about either of those issues. IIUC, Node.js doesn't strip out the spaces or the quotes. The shell does.
This is possible on Windows (since Windows internally treats the command line as a single string), but not on POSIX (where the command line is treated as a list of arguments). There is nothing Node.js can do here, that's just how shells work.
If you really need to be able to pass code to the process via command line arguments, you can use the same approach as Node.js itself and pass the code as a single argument:
node my-interpreter.mjs eval '"Two times " ++ Pi ++ " equals " ++ (2 * Pi)'
Thanks for the info. I was confused by bash's $@, because I thought that it does what I want (except not in node), so it must be possible in node, too. But I was wrong - $@ is also processed. What a pity.
Most helpful comment
Yeah, in that situation, I would assume @mik-jozef would backslash-escape quotation marks and other special characters as necessary at invocation time. But if the expectation here is to make that unnecessary, then that may not be reasonably possible.