Checking if ARGV is given when running a program with node
Asked Answered
H

2

5

When running a program with node:

node test.js

How do you check if the program is given an ARGV when run node test.js --example? What I've tried so far is the following:

function say(input){
    console.log(input);
}

if(process.argv[1] !== '')
{
    say('false');
}
else {
    say('success');
}

if(process.argv[1] === null)
{
    say('false');
}
else {
    say('success');
}

However the first solution won't output the else and the second solution only outputs NUL is not defined so with that, what I'm thinking is that I'm making some mistake in my coding?

Haber answered 13/5, 2016 at 16:48 Comment(13)
NUL is not definedPortion
@Portion Isn't NUL just a way to say not there?Haber
try (process.argv[1] === undefined) , NUL isn't predefined in jsPortion
@Portion Well it works, but it doesn't output the information just exits the processHaber
@Portion Why is NUL not predefined?Haber
it should work... how are you running the program?Portion
node test.js outputs nothing, but if I run node test.js --example it will output successHaber
I think you're counting process.argv wrong ; try console.log(process.argv)Portion
[ 'C:\\Program Files\\nodejs\\node.exe', 'C:\\Users\\thomas_j_perkins\\bin\\javascript\\node\\email\\test.js', '--example' ]Haber
NUL is not how you "say" null in JavaScript. There are two different kinds of null values in JS as well. null is an explicit null value, whereas undefined is null by lack of definition (though it can be explicitly set as well)Licko
@BrandonAnzaldi So it's null and not NUL?Haber
That is correct. Docs on null, Docs on undefined It's important to note that null !== undefinedLicko
@BrandonAnzaldi Awesome, thank you sir.Haber
I
7
  • argv[0] is the name of your node inerpretor --> in general node
  • argv[1] is the path to your script. So argv[1] is always filled

try this to be very well aware :

console.log(process.argv);
Ingres answered 13/5, 2016 at 17:33 Comment(0)
H
6

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.

Haber answered 13/5, 2016 at 18:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.