Thanks to the answer provided by kevin ternet, and for the information provided by maioman what I ended up doing was this:
if(process.argv[2] === undefined){
say('false');
} else {
say('true');
}
Here's what happens when you process ARGV
in Node:
ARGV[0]
:
if(process.argv[0] === undefined){
console.log('Failure');
console.log(process.argv[0]);
} else {
console.log('Success');
console.log(process.argv[0]);
}
Output:
Success
C:\Program Files\nodejs\node.exe //Path to node executable
ARGV[1]
:
if(process.argv[1] === undefined){
console.log('Failure');
console.log(process.argv[1]);
} else {
console.log('Success');
console.log(process.argv[1]);
}
Output:
Success
C:\Users\bin\javascript\node\test.js //Path to file
ARGV[2]
:
if(process.argv[2] === undefined){
console.log('Failure');
console.log(process.argv[1]);
} else {
console.log('Success');
console.log(process.argv[2]);
}
Output:
Success
--example //The actual flag that was given
So therefore to check if a flag is actually given, you look for ARGV[2]
.
Here's an example of the entire ARGV
tree ran:
if(process.argv === undefined){
console.log('Failure');
console.log(process.argv);
} else {
console.log('Success');
console.log(process.argv);
}
Success
[ 'C:\\Program Files\\nodejs\\node.exe' //ARGV[0],
'C:\\Users\\bin\\javascript\\node\\test.js' //ARGV[1],
'--example' //ARGV[2] ]
So as you can see the tree is structured as an array, with the first argument being 0
.
NUL is not defined
– PortionNUL
just a way to say not there? – Haber(process.argv[1] === undefined)
,NUL
isn't predefined in js – PortionNUL
not predefined? – Habernode test.js
outputs nothing, but if I runnode test.js --example
it will outputsuccess
– Haber[ 'C:\\Program Files\\nodejs\\node.exe', 'C:\\Users\\thomas_j_perkins\\bin\\javascript\\node\\email\\test.js', '--example' ]
– Habernull
is an explicit null value, whereasundefined
is null by lack of definition (though it can be explicitly set as well) – Lickonull
and notNUL
? – Habernull
, Docs onundefined
It's important to note thatnull !== undefined
– Licko