Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 266
Pablo Prado Avatar asked Nov 09 '22 18:11

Pablo Prado


1 Answers

Vorpal.js is a framework I made for building interactive CLIs in Node. As part of this, it has a built-in API for easy command parsing.

like image 89
dthree Avatar answered Nov 14 '22 22:11

dthree