Skip to content

Commit

Permalink
Add ignoreTrue option (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigill committed Jun 30, 2021
1 parent b80546a commit adfe9e8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ export interface Options {
*/
readonly shortFlag?: boolean;

/**
Exclude `true` values. Can be useful when dealing with argument parsers that only expect negated arguments like `--no-foo`.
@default false
*/
readonly ignoreTrue?: boolean;

/**
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function dargs(object, options) {
continue;
}

if (value === true) {
if (value === true && !options.ignoreTrue) {
pushArguments(key, '');
}

Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ expectType<string[]>(dargs(object, {includes}));
expectType<string[]>(dargs(object, {aliases}));
expectType<string[]>(dargs(object, {useEquals: false}));
expectType<string[]>(dargs(object, {shortFlag: true}));
expectType<string[]>(dargs(object, {ignoreTrue: true}));
expectType<string[]>(dargs(object, {ignoreFalse: true}));
expectType<string[]>(dargs(object, {allowCamelCase: true}));

Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ console.log(dargs({a: true}, {shortFlag: false}));
//=> ['--a']
```

##### ignoreTrue

Type: `boolean`\
Default: `false`

Exclude `true` values. Can be useful when dealing with argument parsers that only expect negated arguments like `--no-foo`.

##### ignoreFalse

Type: `boolean`\
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ test('excludes and includes options', t => {
]);
});

test('option to ignore true values', t => {
t.deepEqual(dargs({foo: true}, {ignoreTrue: true}), []);
});

test('option to ignore false values', t => {
t.deepEqual(dargs({foo: false}, {ignoreFalse: true}), []);
});
Expand Down

0 comments on commit adfe9e8

Please sign in to comment.