This repository has been archived by the owner. It is now read-only.

Added ability to run interactively with arguments #5041

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants

Niggler commented Mar 16, 2013

There currently is no way to run node interactively and pass arguments without
a file: node -i foo bar baz will instruct node to load foo.

This commit adds a -- argument that instructs node to run interactive and not
to load any file in the argv:

$ node -i -- foo bar baz
> process.argv
[ '/the/path/to/node',
  '--',
  'foo',
  'bar',
  'baz' ]

The same thing works without the '-i':

$ node -- foo bar baz
> process.argv
[ '/the/path/to/node',
  '--',
  'foo',
  'bar',
  'baz' ]

@bnoordhuis bnoordhuis and 1 other commented on an outdated diff Mar 16, 2013

src/node.js
@@ -78,7 +78,7 @@
} else if (process._eval != null) {
// User passed '-e' or '--eval' arguments to Node.
evalScript('[eval]');
- } else if (process.argv[1]) {
+ } else if (process.argv[1] && process.argv[1] !== "--") {
@bnoordhuis

bnoordhuis Mar 16, 2013

Member

Have you run make test? I would expect this to break some of them.

Talking about tests, this PR needs one.

@Niggler

Niggler Mar 16, 2013

actually it didn't fail any of the tests from make test, but it did fail linting (though you already pointed that out)

@bnoordhuis bnoordhuis commented on an outdated diff Mar 16, 2013

src/node.js
@@ -121,7 +121,7 @@
} else {
var Module = NativeModule.require('module');
-
+ if(process.argv[0] === "--") process._forceRepl = true;
@bnoordhuis

bnoordhuis Mar 16, 2013

Member

Style: space between keyword and parenthesis.

Niggler commented Mar 16, 2013

@bnoordhuis i updated the PR with code that satisfies the linter (a few space and string-constant issues). Both the original and this version pass all tests. I couldn't find a test in which you controlled the actual arguments passed to the test (looking in the files in the test/ directory). Could you point me to one so that I can craft a relevant test?

isaacs commented Mar 17, 2013

@Niggler Make a test that calls node using child_process.spawn() with whatever args, and then write to child.stdin, and verify the stdout and stderr output.

Member

bnoordhuis commented Mar 17, 2013

@Niggler Maybe test/simple/test-cli-eval.js?

Added ability to run interactively with arguments
There currently is no way to run node interactively and pass arguments without
a file: `node -i foo bar baz` will instruct node to load `foo`.

This commit adds a `--` argument that instructs node to run interactive and not
to load any file in the argv:

    $ node -i -- foo bar baz
    > process.argv
    [ '/the/path/to/node',
      '--',
      'foo',
      'bar',
      'baz' ]

The same thing works without the '-i':

    $ node -- foo bar baz
    > process.argv
    [ '/the/path/to/node',
      '--',
      'foo',
      'bar',
      'baz' ]

Niggler commented Mar 17, 2013

@isaacs @bnoordhuis I have added a few tests to verify that -- is passed along as the script name and that -i -- runs without trying to look for a script.

isaacs commented Mar 18, 2013

Why not just always run interactively with -i? Do we really need -- at all?

On Sunday, March 17, 2013, Niggler wrote:

@isaacs https://github.com/isaacs @bnoordhuishttps://github.com/bnoordhuisI have added a few tests to verify that
-- is passed along as the script name and that -i -- runs without trying
to look for a script.


Reply to this email directly or view it on GitHubhttps://github.com/joyent/node/pull/5041#issuecomment-15026373
.

Niggler commented Mar 18, 2013

@isaacs with your blessing there'll be a patch, but what I'd like to see node -i do is something like python's python -i mode: try to run the file and return control to the user when it finishes.

Niggler commented Mar 18, 2013

@isaacs to explain that point further:

$ cat test.js
x=3
$ node -i test.js
> x
3

(that's not what happens now, but that's what I think node -i should do)

Member

bnoordhuis commented Mar 18, 2013

Whitespace errors but nothing that git am --whitespace=fix can't deal with. LGTM, tests pass and I'm +1 on the feature. Should be safe to land in v0.10, I think?

isaacs commented Mar 27, 2013

Discussed in IRC. Bottom line:

This is a pretty good start. But I'd rather just go whole-hog here, and do the whole thing in 0.12, rather than have a piecemeal fix where we use -- and -i and they do basically the same thing.

Strategy:

  • -i means "Always run a repl". If a filename is provided, then the file is executed in the repl context prior to the first prompt.
  • - as the filename means "stdin".
  1. node -i foo.js bar Runs foo.js in the repl context, then starts a repl. argv = node foo.js bar
  2. node - foo.js bar Does not run a file. Loads the repl if stdin is a TTY. argv = node - foo.js bar
  3. node -i - foo.js bar Does not run a file. Loads the repl. argv = node - foo.js bar
  4. echo $program | node -i foo.js bar Load foo.js, and run it in the repl context. Then start the repl. (Note: whatever part of $program that foo.js didn't read will get dumped there as the repl input.) argv = node foo.js bar
  5. echo $program | node - foo.js bar Does not run a file. Reads $program and runs it. argv = node - foo.js bar
  6. echo $program | node -i - foo.js bar Does not run a file. Dumps $program into the repl. argv = node - foo.js bar

@Niggler For this pull req, can you review your approach and see if it still makes sense as a partial solution?

Owner

jasnell commented Aug 3, 2015

Closing due to lack of activity. Can revisit if someone is interested in updating the PR. Given that this is a feature request, however, it would need to be targeted at either http://github.com/nodejs/io.js or http://github.com/nodejs/node

@jasnell jasnell closed this Aug 3, 2015

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.