-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic cli-argument validation (#72)
* refactor: explicit check return code of commands * refactor: specify cli arguments explicitly This syntax is necessary in order to specify parsing functions in a future commit * deps: bump commander * refactor: remove unused cli option As far as I can tell this "registry" option is not used. The login function seems to use the global registry option. * feat: basic cli option/argument validation * refactor: remove unecessary try-catch * fix: confusing wording on error message
- Loading branch information
1 parent
17a7859
commit 49d0a96
Showing
4 changed files
with
136 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { InvalidArgumentError } from "@commander-js/extra-typings"; | ||
|
||
/** | ||
* @throws {InvalidArgumentError} | ||
*/ | ||
type CliValueParser<TOut> = (input: string, previous?: TOut) => TOut; | ||
|
||
export function mustSatisfy<TOut extends string>( | ||
typeAssertion: (input: string) => input is TOut, | ||
makeErrorMessage: (input: string) => string | ||
): CliValueParser<TOut> { | ||
return (input) => { | ||
if (!typeAssertion(input)) | ||
throw new InvalidArgumentError(makeErrorMessage(input)); | ||
return input; | ||
}; | ||
} | ||
|
||
export function mustBeParceable<TOut>( | ||
parse: (input: string) => TOut, | ||
makeErrorMessage: (input: string, error: unknown) => string | ||
): CliValueParser<TOut> { | ||
return (input) => { | ||
try { | ||
return parse(input); | ||
} catch (error) { | ||
throw new InvalidArgumentError(makeErrorMessage(input, error)); | ||
} | ||
}; | ||
} | ||
|
||
export function eachValue<TOut>( | ||
parser: CliValueParser<TOut> | ||
): CliValueParser<TOut[]> { | ||
return (input, previous) => { | ||
const parsed = parser(input); | ||
if (previous === undefined) return [parsed]; | ||
else return [...previous, parsed]; | ||
}; | ||
} |
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