Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add required option #2

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
docs: required flag
  • Loading branch information
privatenumber committed Dec 7, 2021
commit d379613decb13b43855bb687bca18141bf02c82f
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const parsed = typeFlag(process.argv.slice(2), {

someBoolean: {
type: Boolean,
alias: 'b'
alias: 'b',
required: true
},

// Wrap with an array to indicate an array type
Expand All @@ -46,16 +47,16 @@ const parsed = typeFlag(process.argv.slice(2), {
}
})

console.log(parsed.flags.someString[0])
console.log(parsed.flags.someString)
```

`parsed` resolves to the following type:
```ts
{
flags: {
someString: string | undefined;
someNumber: boolean | undefined;
someBoolean: number | undefined;
someNumber: number | undefined;
someBoolean: boolean;
stringArray: string[];
numberArray: number[];
};
Expand All @@ -68,6 +69,24 @@ console.log(parsed.flags.someString[0])

### Usage

#### Required flags
Non-array types can be `undefined` unless `required: true` is set:
```ts
const parsed = typeFlag(process.argv.slice(2), {
someNumber: {
type: Number,
required: true
}
})
```

When a flag is required, the return type can no longer be `undefined`:
```ts
{
someNumber: number;
}
```

#### kebab-case flags mapped to camelCase
When passing in the flags, they can be in kebab-case and will automatically map to the camelCase equivalent.
```sh
Expand Down Expand Up @@ -253,6 +272,7 @@ type FlagSchema = {
[flagName: string]: TypeFunction | [TypeFunction] | {
type: TypeFunction | [TypeFunction];
alias?: string;
required?: true;
};
};
```
Expand Down