From 0ddac0bea44785fdf1e37de82b012788f7debbc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rory=20O=E2=80=99Kane?= Date: Sun, 3 May 2020 14:14:42 -0400 Subject: [PATCH 1/2] docs(api): clarify process.argv handling and the order of API methods --- docs/api.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index 5e2224cd4..6bb5b9f1b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -20,7 +20,7 @@ best to parse `process.argv`: require('yargs').argv ``` -You can also pass in the `process.argv` yourself: +You can also pass in the arguments yourself: ```javascript require('yargs')([ '-x', '1', '-y', '2' ]).argv @@ -38,7 +38,15 @@ Calling `.parse()` with no arguments is equivalent to calling `yargs.argv`: require('yargs').parse() ``` -The rest of these methods below come in just before the terminating `.argv`. +When passing in the arguments yourself, note that Yargs expects the passed array +to contain only the arguments after the program name, while Node’s +[`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array +starts with two extra elements:`process.execPath` and the path to the JavaScript +file being executed. So if you’re getting your arguments from `process.argv`, +pass `process.argv.slice(2)` to Yargs. + +The rest of these methods below come in just before the terminating `.argv` or +terminating `.parse()`. .alias(key, alias) ------------------ From eb6c81201ed203f60e947c3b1d58ba873a4746df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rory=20O=E2=80=99Kane?= Date: Fri, 5 Jun 2020 17:51:05 -0400 Subject: [PATCH 2/2] docs(api): describe process.argv handling more generally --- docs/api.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/api.md b/docs/api.md index e640ddeba..eaff3395f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -39,11 +39,12 @@ require('yargs').parse() ``` When passing in the arguments yourself, note that Yargs expects the passed array -to contain only the arguments after the program name, while Node’s -[`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array +to contain only the arguments after the program name, while `process.argv` +usually starts with extra elements. For example, [Node’s +`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array starts with two extra elements:`process.execPath` and the path to the JavaScript -file being executed. So if you’re getting your arguments from `process.argv`, -pass `process.argv.slice(2)` to Yargs. +file being executed. So if you’re getting your arguments from `process.argv` in +Node, pass `process.argv.slice(2)` to Yargs. The rest of these methods below come in just before the terminating `.argv` or terminating `.parse()`.