From b3d9146a5e09152aad1d4e43f35aef5de619f9db Mon Sep 17 00:00:00 2001 From: Justin Wyer Date: Thu, 8 Sep 2022 17:05:04 +0200 Subject: [PATCH] fix: support environment variables for boolean flags (#488) fixes #487 I am happy to contribute to the docs if this is accepted. --- src/parser/parse.ts | 15 ++++++++----- test/parser/parse.test.ts | 44 +++++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/parser/parse.ts b/src/parser/parse.ts index d0eb4bdd..a7d965e2 100644 --- a/src/parser/parse.ts +++ b/src/parser/parse.ts @@ -198,13 +198,18 @@ export class Parser { - it('accepts as environment variable', async () => { - process.env.TEST_FOO = '101' - const out = await parse([], { - flags: {foo: flags.string({env: 'TEST_FOO'})}, + describe('string', () => { + it('accepts as environment variable', async () => { + process.env.TEST_FOO = '101' + const out = await parse([], { + flags: {foo: flags.string({env: 'TEST_FOO'})}, + }) + expect(out.flags.foo).to.equal('101') + delete process.env.TEST_FOO }) - expect(out.flags.foo).to.equal('101') - delete process.env.TEST_FOO + }) + + describe('boolean', () => { + const truthy = ['true', 'TRUE', '1', 'yes', 'YES', 'y', 'Y'] + for (const value of truthy) { + it(`accepts '${value}' as a truthy environment variable`, async () => { + process.env.TEST_FOO = value + const out = await parse([], { + flags: { + foo: flags.boolean({env: 'TEST_FOO'}), + }, + }) + expect(out.flags.foo).to.be.true + delete process.env.TEST_FOO + }) + } + + const falsy = ['false', 'FALSE', '0', 'no', 'NO', 'n', 'N'] + for (const value of falsy) { + it(`accepts '${value}' as a falsy environment variable`, async () => { + process.env.TEST_FOO = value + const out = await parse([], { + flags: { + foo: flags.boolean({env: 'TEST_FOO'}), + }, + }) + expect(out.flags.foo).to.be.false + delete process.env.TEST_FOO + }) + } }) })