Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Added ability to run interactively with arguments#5041

Closed
redchair123 wants to merge 1 commit intonodejs:masterfrom
redchair123:master
Closed

Added ability to run interactively with arguments#5041
redchair123 wants to merge 1 commit intonodejs:masterfrom
redchair123:master

Conversation

@redchair123
Copy link

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' ]

src/node.js Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Talking about tests, this PR needs one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@redchair123
Copy link
Author

@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
Copy link

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.

@bnoordhuis
Copy link
Member

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

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' ]
@redchair123
Copy link
Author

@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
Copy link

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//pull/5041#issuecomment-15026373
.

@redchair123
Copy link
Author

@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.

@redchair123
Copy link
Author

@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)

@bnoordhuis
Copy link
Member

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
Copy link

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?

@jasnell
Copy link
Member

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments