Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expand scenarios covered by default arguments for environments #20

Merged
merged 10 commits into from Dec 28, 2021

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Nov 23, 2021

Problem

We are proposing to determine appropriate default arguments for node --eval, Electron, and ES6.

Using require.main to determine --eval (or REPL) gives false positive for ES6 main program.

Related: #5 #18

Solution

Implement a placeholder for process.mainArgs that covers the scenarios that have been raised.

  • detect node --eval directly by checking options passed to node (-e, --eval)
  • detect node --print directly by checking options passed to node (-p, --print)
  • no need to detect REPL because no user arguments from CLI?
  • test for bundled Electron app (using same approach as Yargs and Commander)
  • fallback to usual process.argv.slice(2)

Related PRs for Electron argument processing:

And for historical interest, the addition of defaultApp in Electron to work-around the variant behaviour:

Example

% node -e 'console.log(require(".").parseArgs())' 1 --foo 2 3
{
  args: { foo: true },
  values: { foo: [ undefined ] },
  positionals: [ '1', '2', '3' ]
}
 % node es6.mjs --foo 1 2 3
{
  args: { foo: true },
  values: { foo: [ undefined ] },
  positionals: [ '1', '2', '3' ]
}

- detect REPL
- detect bundled Electron app
- otherwise process.argv.slice(2)
@shadowspawn shadowspawn marked this pull request as draft November 23, 2021 19:08
@shadowspawn
Copy link
Collaborator Author

Just spotted node -p in a comment. I had a feeling I was missing another special mode!

nodejs/node#35015 (comment)

@ljharb
Copy link
Member

ljharb commented Nov 27, 2021

node -pe is critical

@shadowspawn
Copy link
Collaborator Author

Added -p and --print. e.g.

% node --print 'require (".").parseArgs()' first second
{ args: {}, values: {}, positionals: [ 'first', 'second' ] }

index.js Show resolved Hide resolved
@shadowspawn shadowspawn marked this pull request as ready for review December 5, 2021 08:41
@shadowspawn
Copy link
Collaborator Author

Added tests, refactored code to be an explicit placeholder for process.mainArgs, describe Election as an example rather than as intended for inclusion.

@bcoe bcoe changed the title Expand scenarios covered by default arguments for environments feat: expand scenarios covered by default arguments for environments Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants