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

Viewed 218

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.

1 Answers

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.

Related