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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(usage): add YARGS_DISABLE_WRAP env variable to disable wrap #2210

Merged
merged 5 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/usage.ts
Expand Up @@ -163,14 +163,17 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
wrap = cols;
};

function getWrap() {
self.getWrap = () => {
if (shim.getEnv('YARGS_DISABLE_WRAP')) {
return null;
}
if (!wrapSet) {
wrap = windowWidth();
wrapSet = true;
}

return wrap;
}
};

const deferY18nLookupPrefix = '__yargsString__:';
self.deferY18nLookup = str => deferY18nLookupPrefix + str;
Expand Down Expand Up @@ -202,7 +205,7 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
}, {} as Dictionary<boolean>)
);

const theWrap = getWrap();
const theWrap = self.getWrap();
const ui = shim.cliui({
width: theWrap,
wrap: !!theWrap,
Expand Down Expand Up @@ -769,6 +772,7 @@ export interface UsageInstance {
getPositionalGroupName(): string;
getUsage(): [string, string][];
getUsageDisabled(): boolean;
getWrap(): number | nil;
help(): string;
reset(localLookup: Dictionary<boolean>): UsageInstance;
showHelp(level?: 'error' | 'log' | ((message: string) => void)): void;
Expand Down
11 changes: 10 additions & 1 deletion test/usage.cjs
Expand Up @@ -1912,7 +1912,16 @@ describe('usage tests', () => {

// the long description should cause several line
// breaks when wrapped.
r.errors[0].split('\n').length.should.gte(4);
r.errors[0].split('\n').length.should.gte(5);
});

it('should not wrap when YARGS_DISABLED_WRAP is provided', () => {
const yargsInstance = yargs().wrap(99);
process.env.YARGS_DISABLE_WRAP = 'true';
expect(
yargsInstance.getInternalMethods().getUsageInstance().getWrap()
).to.equal(null);
delete process.env.YARGS_DISABLE_WRAP;
});

it('should not raise an exception when long default and description are provided', () =>
Expand Down