Why is . passed as argument to npm without -- delimiter?
Asked Answered
I

1

2

I am having trouble figuring out why is npm taking . without -- delimiter.

In the following command . is passed to test script without -- delimiter.

npm test .

test script is defined in package.json like this:

"test": "react-app-rewired test"

Passing regular argument to test script

This happened to me when I tried to pass --coverage to npm test but later i found out that to pass arguments to npm script i need -- before any following argument.

This is what works if i want to pass argument:

npm test -- --coverage

But this is will not pass --coverage argument

npm test --coverage

Question is why is . being passed without --. Based on npm documentation to pass any argument to a script we need to use -- delimiter and npm will know that the following flags/arguments are for test script or any other script that we want to parametrize.

Intendment answered 7/9, 2021 at 12:9 Comment(7)
Can you clarify where exactly you're receiving/not receiving those arguments? How do you figure that one works and the other doesn't?Colmar
I edited my question to give more context. Tell me if this makes more sense.Intendment
Be aware of the difference between positional parameters and options: #36496169Hurter
Aha. If i get ti correctly, . is an option for npm command? But in my case it is also a parameter for test command that is wrapped by npm testIntendment
Essentially, a period (.) is not interpreted as an option because it does not begin with a hyphen (-).Aikoail
Ah i get it, I added some random characters and it was passed down like this npm test somethinElse and it ran like react-app-rewired test somethingElse.Intendment
I'll let someone else compose an answer and review it. If there is none soon, i will compile comments here into an answer. There is a question from someone else that is related to my question here that will be answered also after this. Thanks for your comments, they cleared things.Intendment
H
1

As Charles Duffy explains in his answer for this question (emphasis mine):

double hyphens (--) as an argument on its own is standardized across all UNIX commands: It means that further arguments should be treated as positional parameters, not options.

To answer your question:
. is passed as positional parameter to the react-app-rewired script because you provide it as positional parameter when running npm test ..
--something would be interpreted by npm as an option for itself unless it is prefixed with -- at some point, in which case it will as well be treated as positional parameter.

See this SO question for a more detailed explanation of the difference between command options, arguments and parameters.

Hurter answered 14/9, 2021 at 13:6 Comment(3)
You say; "Thus npm test . is equivalent to npm test -- --. and npm test -- --coverage would be equivalent to npm test coverage". However neither of the comparable examples that you've given are equivalent. In the case of your first example the consuming script/command will receives either; . or --.. In the case of your second example the consuming script/command will receive either; --coverage or coverage.Aikoail
You're right, thanks for pointing this out. I believe the updated version should be correctHurter
@Hurter To test your assumption I suggest defining the test script in package.json as "test": "node test.js". In test.js add the following line of code console.log(process.argv);. It logs an array of cmd line args that the script receives by utilizing process.argv. You'll see that your updated comparisons are also not equivalent. The '--.' in npm test '--.' and '--coverage' in npm test '--coverage' are consumed by npm (not passed to the script/cmd) because they too begin with a hyphen even though they're quoted.Aikoail

© 2022 - 2024 — McMap. All rights reserved.