-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move demandSingleValue and demandOneOfOptions to core
- Loading branch information
1 parent
1e3057e
commit 4bf4f4b
Showing
5 changed files
with
64 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CommandLineArgsError } from "../errors"; | ||
|
||
/** | ||
* A helper to demand one of a set of options | ||
* via https://github.com/yargs/yargs/issues/1093#issuecomment-491299261 | ||
*/ | ||
export function demandOneOfOption(...options: string[]) { | ||
return function (argv: { [key: string]: unknown }) { | ||
const count = options.filter((option) => argv[option]).length; | ||
const lastOption = options.pop(); | ||
|
||
if (count === 0) { | ||
throw new CommandLineArgsError( | ||
`Exactly one of the arguments ${options.join( | ||
", " | ||
)} and ${lastOption} is required` | ||
); | ||
} else if (count > 1) { | ||
throw new CommandLineArgsError( | ||
`Arguments ${options.join( | ||
", " | ||
)} and ${lastOption} are mutually exclusive` | ||
); | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
/** | ||
* A helper to ensure that an argument only receives a single value. | ||
* | ||
* This is a workaround for a limitation in yargs where non-array arguments can still receive multiple values | ||
* even though the inferred type is not an array. | ||
* | ||
* @see https://github.com/yargs/yargs/issues/1318 | ||
*/ | ||
export function demandSingleValue(key: string) { | ||
return function (argv: { [key: string]: unknown }) { | ||
if (Array.isArray(argv[key])) { | ||
throw new CommandLineArgsError( | ||
`The argument '--${key}' expects a single value, but received multiple: ${JSON.stringify(argv[key])}.` | ||
); | ||
} | ||
|
||
return true; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { defineAlias, defineCommand, defineNamespace } from "./define-command"; | ||
export { demandOneOfOption, demandSingleValue } from "./helpers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters