From 11303eb0ef5daa8256fdafabd09d995d3ae3c39e Mon Sep 17 00:00:00 2001 From: John Gee Date: Thu, 23 Jan 2025 11:15:12 +1300 Subject: [PATCH] [Docs]: Document and test boolean options consuming true/false strings Fixes #64 Co-authored-by: Jordan Harband --- README.md | 7 ++++--- test/all_bool.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 891abf0..90a6219 100644 --- a/README.md +++ b/README.md @@ -72,9 +72,10 @@ options can be: * `opts.string` - a string or array of strings argument names to always treat as strings -* `opts.boolean` - a boolean, string or array of strings to always treat as -booleans. if `true` will treat all double hyphenated arguments without equal signs -as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`) +* `opts.boolean` - A boolean, string, or array of strings to always treat as +booleans. If `true` will treat all double-hyphenated arguments without equal signs +as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`) A boolean option will +consume the following argument if it is the string `true` or `false`. (e.g. `--foo false`) * `opts.alias` - an object mapping string names to strings or arrays of string argument names to use as aliases * `opts.default` - an object mapping string argument names to default values diff --git a/test/all_bool.js b/test/all_bool.js index befa0c9..90252c5 100644 --- a/test/all_bool.js +++ b/test/all_bool.js @@ -32,3 +32,19 @@ test('flag boolean true only affects double hyphen arguments without equals sign t.deepEqual(typeof argv.honk, 'boolean'); t.end(); }); + +test('flag boolean true includes consuming true/false', function (t) { + var argv = parse(['--aaa', 'true', '--bbb', 'false', '--ccc=true', '--ddd=false'], { + boolean: true, + }); + + t.deepEqual(argv, { + aaa: true, + bbb: false, + ccc: 'true', // [sic] check legacy behaviour + ddd: 'false', // [sic] check legacy behaviour + _: [], + }); + + t.end(); +});