@molt/types
⛑ Advanced Types for parsing CLI flags and more.
Installation
npm add @molt/types
Namespaces
FlagName
The FlagName
namespace provides a Parse
type and some other utility types. Parse
turns an expression of CLI flags into structured object type data at the type level.
Features
- Capture long flag
- Capture short flag
- Capture alias short flags
- Capture alias long flags
- Flexible syntax
- Optional leading dashes
--
/-
- Kebab/camel case agnostic
fooBar
/foo-bar
- Clear human-friendly error messages when parsing fails.
- Catch name duplicate
- Enforce reserved names
- Statically Normalize kebob case to camel case
API
.parse
Examples
FlagName.Parse<''>
FlagName.Parse<'--foo-bar', { reservedNames: 'foo-bar'; usedNames: undefined }>
FlagName.Parse<'--foo-bar', { reservedNames: 'fooBar'; usedNames: undefined }>
FlagName.Parse<'--fooBar', { reservedNames: 'foo-bar'; usedNames: undefined }>
FlagName.Parse<'-a', { usedNames: 'a'; reservedNames: undefined }>
FlagName.Parse<'--v'>
FlagName.Parse<'-foo'>
FlagName.Parse<'--foo --foo'>
import { FlagName } from '@molt/types'
const defineFlag = <Name>(
name: FlagName.Errors.$Is<FlagName.Parse<Name>> extends true ? FlagName.Parse<Name> : Name
) => {
}
defineFlag(``)
defineFlag(`-`)
defineFlag(`--`)
defineFlag(`--a`)
defineFlag(`-ab`)
defineFlag(`--foo --foo`)
defineFlag(`--foo-bar --fooBar`)
defineFlag(`--version -`)
defineFlag(`-v -v`)
defineFlag(`--version`)
defineFlag(`--version --ver`)
defineFlag(`--version --ver -v`)
defineFlag(`-v`)