samedi 28 février 2015

Parsing Node Command Line Arguments

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).


Aucun commentaire:

Enregistrer un commentaire