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

process: adds process.mainArgs #29990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,12 @@ added: v0.1.27

* {string[]}

The `process.argv` property returns an array containing the command line
arguments passed when the Node.js process was launched. The first element will
be [`process.execPath`][]. See `process.argv0` if access to the original value
of `argv[0]` is needed. The second element will be the path to the JavaScript
file being executed. The remaining elements will be any additional command line
arguments.
The `process.argv` property returns an array in which the first element will be
[`process.execPath`][]. See `process.argv0` if access to the original value of
`argv[0]` is needed. The second element will be the path to the JavaScript file
being executed. The remaining elements will be any additional command line
arguments passed when the Node.js process was launched, same as
[`process.mainArgs`][].

For example, assuming the following script for `process-args.js`:

Expand Down Expand Up @@ -1425,6 +1425,38 @@ process.kill(process.pid, 'SIGHUP');
When `SIGUSR1` is received by a Node.js process, Node.js will start the
debugger. See [Signal Events][].

## process.mainArgs
<!-- YAML
added: REPLACEME
-->

The `process.mainArgs` property returns an array containing the command line
arguments passed when the Node.js process was launched.

For example, assuming the following script for `process-args.js`:

```js
// print process.mainArgs
process.mainArgs.forEach((val, index) => {
console.log(`${index}: ${val}`);
});
```

Launching the Node.js process as:

```console
$ node process-args.js one two=three four
```

Would generate the output:

```text
0: one
1: two=three
2: four
```
* {string[]}

## process.mainModule
<!-- YAML
added: v0.1.17
Expand Down Expand Up @@ -2446,6 +2478,7 @@ cases:
[`process.hrtime()`]: #process_process_hrtime_time
[`process.hrtime.bigint()`]: #process_process_hrtime_bigint
[`process.kill()`]: #process_process_kill_pid_signal
[`process.mainArgs`]: #process_process_mainargs
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
[`Promise.race()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
Expand Down
7 changes: 7 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ function setupPrepareStackTrace() {
setEnhanceStackForFatalException(beforeInspector, afterInspector);
}

const { mainArgs } = require('internal/process/main_args');
Object.defineProperty(process, 'mainArgs', {
configurable: true,
enumerable: true,
get: mainArgs
});

function setupProcessObject() {
const EventEmitter = require('events');
const origProcProto = Object.getPrototypeOf(process);
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/process/main_args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function mainArgs() {
return process.argv.slice(2);
}

module.exports = {
mainArgs
};
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
'lib/internal/priority_queue.js',
'lib/internal/process/esm_loader.js',
'lib/internal/process/execution.js',
'lib/internal/process/main_args.js',
'lib/internal/process/main_thread_only.js',
'lib/internal/process/per_thread.js',
'lib/internal/process/policy.js',
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-process-mainArgs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');

// Tests that process.mainArgs returns all arguments provided
if (process.mainArgs[0] === 'child') {
console.log(process.mainArgs.join(' '));
return;
}

const child = spawnSync(process.execPath, [__filename, 'child', '-a', 'one']);
assert.strictEqual(child.stdout.toString().trim(), 'child -a one');