Home > Back-end >  special characters like asterisks argv
special characters like asterisks argv

Time:05-11

Running my script like the following:

Node index.js POST *

In my script I do this:

process.argv.forEach((val, index) => {
  console.log(`${index}: ${val}`);
});

The output will be the following where * causes to dump all my files' names in my project:

$ node index.js POST *
0: C:\Program Files\nodejs\node.exe
1: C:\Users\myComputer\myScript\index.js
2: POST
3: commands
4: data.json
5: index.js
6: node_modules
7: package-lock.json
8: package.json

How can I see if passed arg is an astricts?

It works perfectly fine for other args like other strings (in my example 'POST').

I also used https://github.com/tj/commander.js and the same issue.

Using Node version 15

CodePudding user response:

If you're running this in bash or some similar shell, then the globbing is happening on a shell level, before it ever gets to your program. You can't see whether a star was typed from inside the program, because the shell switches it out for a list of all the files in the directory.

If you want to pass an actual star, you can put it in quotes, and that way the shell won't expand it to all the files, and will pass it to the program as is.

  • Related