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

terminalWidth appears to be typed incorrectly #2381

Closed
Thenlie opened this issue Dec 29, 2023 · 2 comments
Closed

terminalWidth appears to be typed incorrectly #2381

Thenlie opened this issue Dec 29, 2023 · 2 comments

Comments

@Thenlie
Copy link

Thenlie commented Dec 29, 2023

Description

When using terminalWidth as shown in the docs and invoking it as a function, I get the following error:

TypeError: yargs.terminalWidth is not a function

This error is thrown and my application can not run. If I instead call .wrap(yargs.terminalWidth), the application runs as expected.

Full command

#!/usr/bin/env node

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { Props, main } from '.';

yargs(hideBin(process.argv))
    .options({
        o: {
            alias: 'outputFile',
            default: 'stdio',
            describe: 'Filename to write response to',
            type: 'string',
        },
        // truncated for brevity
    })
    .command(
        'run',
        'run the CLI tool',
        () => {},
        (argv) => {
            const { outputFile, inputFile, inputPath, useDefault } = argv as unknown as Props;
            main({ outputFile, inputFile, inputPath, useDefault });
        }
    )
    .demandCommand(1)
    .wrap(yargs.terminalWidth()) // <-- ❌ Does not work
    .wrap(yargs.terminalWidth)   // <-- ✅ Works
    .parse();
@shadowspawn
Copy link
Member

.terminalWidth() is a method on a yargs instance. It is a bit subtle, but the trick is to make the instance separately from configuring it so that you can call the method.

import yargsFactory from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';

const yargsInstance = yargsFactory(hideBin(process.argv));
yargsInstance.wrap(yargsInstance.terminalWidth());

This is actually covered in the TypeScript usage examples, albeit I think with a typo where myYargs is used instead of yargsInstance.

@Thenlie
Copy link
Author

Thenlie commented Jan 1, 2024

Thanks @shadowspawn for the explanation!

@Thenlie Thenlie closed this as completed Jan 1, 2024
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

No branches or pull requests

2 participants