Advanced Types for parsing CLI flags and more.
npm install @molt/typesā Advanced Types for parsing CLI flags and more.
```
npm add @molt/types
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 --/-fooBar
- Kebab/camel case agnostic /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
`ts
// Errors
FlagName.Parse<''>
// Error: You must specify at least one name for your flag.
FlagName.Parse<'--foo-bar', { reservedNames: 'foo-bar'; usedNames: undefined }>
FlagName.Parse<'--foo-bar', { reservedNames: 'fooBar'; usedNames: undefined }>
// Error: The name "foo-bar" cannot be used because it is reserved.
FlagName.Parse<'--fooBar', { reservedNames: 'foo-bar'; usedNames: undefined }>
// Error: The name "fooBar" cannot be used because it is reserved.
FlagName.Parse<'-a', { usedNames: 'a'; reservedNames: undefined }>
// Error: The name "a" cannot be used because it is already used for another flag.
FlagName.Parse<'--v'>
//Error: A long flag must be two (2) or more characters but you have: '--v
FlagName.Parse<'-foo'>
// Error: A short flag must be exactly one (1) character but you have: '-foo'.
FlagName.Parse<'--foo --foo'>
// Error: Your alias "foo" is a duplicate.
`
`ts
import { FlagName } from '@molt/types'
const defineFlag =
name: FlagName.Errors.$Is
) => {
// ...
}
defineFlag() // Static type error-
defineFlag() // Static type error--
defineFlag() // Static type error--a
defineFlag() // Static type error-ab
defineFlag() // Static type error--foo --foo
defineFlag() // Static type error--foo-bar --fooBar
defineFlag() // Static type error--version -
defineFlag() // Static type error-v -v
defineFlag() // Static type error--version
defineFlag() // ok--version --ver
defineFlag() // ok--version --ver -v
defineFlag() // ok-v
defineFlag() // ok``