Node: Filenames / dirnames with a leading hyphen: a CLI argument issue

Created on 6 Jan 2017  Â·  16Comments  Â·  Source: nodejs/node

  • Version: 7.4.0
  • Platform: Windows 7 x64
  • Subsystem: cli

I don't know if this is a deprecated style in *nix to start filenames with a hyphen, but in Windows, it seems there are no shell problems with it: dir -test or type -test.txt are OK. However, Node.js confuses these names with arguments:

node -test.js
node: bad option: -test.js

node -test\test.js
node: bad option: -test\test.js

If this is a wontfix, maybe it worth to be documented in the 'Command Line Options' doc.

question

Most helpful comment

Ahh, I initially understood operands to be just the args passed into the script. i.e.
node <options> script.js <operands>

But I believe what you are saying is that the operands include the script name. i.e.
node <options> <operands>
where
operands = "<script name> <arg1> <arg2> ..."

All 16 comments

The “standard” way of working around that problem would be using node ./-test.js.

But maybe https://github.com/nodejs/node/pull/10651 could be updated to address this use case, too? /cc @jBarz

The only way to do this would be to introduce a new option like
-f <filename>

So CLI usage now becomes
node [options] [v8 options] [ [ -f ] script.js | -e "script"] [arguments]

The code change wouldn't be trivial in my estimation so probably better to open a separate PR.

I'd rather not introduce a new command line flag for this, especially given that there are ways of working around it already.

The only way to do this would be to introduce a new option like

Why wouldn’t -- work?

Do you mean
node -- -test.js?

@jbarz exactly. ;) The solution in your PR seems tailored for -e/-p but I’m not sure it actually needs to be?

Yes, that would work ! My reason for using "--" in https://github.com/nodejs/node/pull/10651 comes from the POSIX.2 standard

Guideline 10: The argument "--" should be accepted as a delimiter
indicating the end of options. Any following
arguments should be treated as operands, even if they
begin with the '-' character. The "--" argument
should not be used as an option or as an operand.

So using the "--" for any other purpose would deviate from other unix tools, I think.

@jBarz Okay, would you want to update the PR with this then? :) It looks to me like you’d just need to drop the && eval_string != nullptr bit but I may be mistaken.

I do not think that -- is the correct syntax for this. node ./-test.js seems like the best way to go.

I do not think that -- is the correct syntax for this.

I mean… it’s pretty standard to support it that way?

I think that conflicts with the potential use in https://github.com/nodejs/node/pull/10651 though?

@jBarz I don't understand why the -- suggestion (node -- -test.js) conflicts with the docs you quote, or with #10651. Perhaps the word "operand" means something different to you than to @addaleax and I?

Traditional UNIX usage, which is what is usually specified by POSIX, is that getopt() and similar stop treating - to be meaningful after the --, which seems to be called for here.

The ./ trick is also time-honoured.

What is the distinction you are drawing?

Ahh, I initially understood operands to be just the args passed into the script. i.e.
node <options> script.js <operands>

But I believe what you are saying is that the operands include the script name. i.e.
node <options> <operands>
where
operands = "<script name> <arg1> <arg2> ..."

I could be missing a corner case, and the distinction between node and v8, but above is my understanding.

This can be closed.
0a9f360c98e4864327888a5030d55f7b2cdcc6d9 fixes the "--" option so that you can now do the following.
node -- -test.js

Yup, this should work fine now. :)

Was this page helpful?
0 / 5 - 0 ratings