I am learning how to use node.js, but am having trouble parsing command-line arguments. The following line:
node test.js --input=in.txt
Gives a parsing error when the code reaches this command:
var fileName = JSONparse.(process.argv[2]);
Results in the error:
undefined
--input=in.txt
Syntax error: Unexpected number
What I am trying to do is create an optional variable for the input file. If it is not specified in the command-line, it should resolve to 'a.txt'.
I have not found an easy way of creating default parameters, or use identifiers such as '--input=' to not have to worry about the order in which arguments are passed (I know it does not matter in this case with one argument).
Syntax error
is because the JS syntax is bad. There is noJSONparse
, and even if there was,JSONparse.(
doesn't make sense;.
is to access a member, so you need an identifier after.
;(
doesn't work. Perhaps you meantJSON.parse(process.argv[2])
, but that doesn't make sense either since the string is not JSON. – Jacob