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

docs: Fix example of coerce #2316

Merged
merged 5 commits into from Apr 28, 2023
Merged

docs: Fix example of coerce #2316

merged 5 commits into from Apr 28, 2023

Conversation

regseb
Copy link
Contributor

@regseb regseb commented Mar 29, 2023

The first example of .coerce(key, fn) is invalid: an await is used in a non-async function.

var argv = require('yargs/yargs')(process.argv.slice(2))
  .coerce('file', function (arg) {
    return await require('fs').promises.readFile(arg, 'utf8')
  })
  .argv

@shadowspawn
Copy link
Member

Making the examples correct is a good thing. The example was updated as part of adding async support to .coerce() in #1872 but was not complete, and I would rather make the example work with async than remove the async.

Would you like to do that?

@regseb
Copy link
Contributor Author

regseb commented Mar 30, 2023

I modified the example to add an async / await.

@shadowspawn
Copy link
Member

I like the direction of getting away from require embedded in the code.

It is much easier to run the example in a module where top-level await is available. How about about making this example esm? e.g.

import { readFile } from 'node:fs/promises';
import yargs from 'yargs';
const argv = await yargs(process.argv.slice(2))
   .coerce('file', async (arg) => readFile(arg, 'utf8'))
   .parseAsync();

@regseb
Copy link
Contributor Author

regseb commented Apr 2, 2023

In your example, the async is useless because the function has no await (require-await). It would be useless to add an await before the return: .coerce('file', async (arg) => await readFile(arg, 'utf8')) (no-return-await).

I propose to add an intermediate variable (which is useless, but there is no ESLint rule to detect it) to have an excuse to use an await.

import { readFile } from 'node:fs/promises';
import yargs from 'yargs';
const argv = await yargs(process.argv.slice(2))
  .coerce('file', async (arg) => {
    const content = await readFile(arg, 'utf8');
    return content;
  })
  .parseAsync();

@shadowspawn
Copy link
Member

Good points. I suggest adding JSON.parse on the contents. Then the await is needed and the async function makes sense.

@regseb
Copy link
Contributor Author

regseb commented Apr 3, 2023

I added JSON.parse() in the example.

Copy link
Member

@shadowspawn shadowspawn left a comment

Choose a reason for hiding this comment

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

LGTM

@shadowspawn
Copy link
Member

Thanks for the PR and conversation @regseb.

@bcoe bcoe merged commit 731c06b into yargs:main Apr 28, 2023
7 checks passed
@regseb regseb deleted the patch-1 branch April 28, 2023 17:35
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