Skip to content

Commit b680ace

Browse files
authored
feat(usage): add YARGS_DISABLE_WRAP env variable to disable wrap (#2210)
1 parent 659dbbb commit b680ace

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/usage.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,17 @@ export function usage(yargs: YargsInstance, shim: PlatformShim) {
163163
wrap = cols;
164164
};
165165

166-
function getWrap() {
166+
self.getWrap = () => {
167+
if (shim.getEnv('YARGS_DISABLE_WRAP')) {
168+
return null;
169+
}
167170
if (!wrapSet) {
168171
wrap = windowWidth();
169172
wrapSet = true;
170173
}
171174

172175
return wrap;
173-
}
176+
};
174177

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

205-
const theWrap = getWrap();
208+
const theWrap = self.getWrap();
206209
const ui = shim.cliui({
207210
width: theWrap,
208211
wrap: !!theWrap,
@@ -769,6 +772,7 @@ export interface UsageInstance {
769772
getPositionalGroupName(): string;
770773
getUsage(): [string, string][];
771774
getUsageDisabled(): boolean;
775+
getWrap(): number | nil;
772776
help(): string;
773777
reset(localLookup: Dictionary<boolean>): UsageInstance;
774778
showHelp(level?: 'error' | 'log' | ((message: string) => void)): void;

test/usage.cjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,16 @@ describe('usage tests', () => {
19121912

19131913
// the long description should cause several line
19141914
// breaks when wrapped.
1915-
r.errors[0].split('\n').length.should.gte(4);
1915+
r.errors[0].split('\n').length.should.gte(5);
1916+
});
1917+
1918+
it('should not wrap when YARGS_DISABLED_WRAP is provided', () => {
1919+
const yargsInstance = yargs().wrap(99);
1920+
process.env.YARGS_DISABLE_WRAP = 'true';
1921+
expect(
1922+
yargsInstance.getInternalMethods().getUsageInstance().getWrap()
1923+
).to.equal(null);
1924+
delete process.env.YARGS_DISABLE_WRAP;
19161925
});
19171926

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

0 commit comments

Comments
 (0)