Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

special characters like asterisks argv

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ 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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading