Skip to content

Commit

Permalink
feat(usage): add YARGS_DISABLE_WRAP env variable to disable wrap (#2210)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoux committed Jul 18, 2022
1 parent 659dbbb commit b680ace
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit b680ace

Please sign in to comment.