Skip to content

Commit

Permalink
feat: set default on argv and move to end (#4)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rearrange typeFlag parameters
  • Loading branch information
privatenumber committed Dec 7, 2021
1 parent 02a81ee commit 2295310
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
29 changes: 14 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Parse CLI argv flags with first-class type support. Think [minimist](https://github.com/substack/minimist), but typed!

### Features
- **Typed flags** Automatically infer types on parsed argvs!
- **Strongly typed** Apply types to parsed argvs!
- **Custom types & validation** Pass in any function and the type will be inferred!
- **Tiny** 2.7 kB minified!

Expand All @@ -21,8 +21,8 @@ Here's a simple usage example:
```ts
import typeFlag from 'type-flag'

// Pass in argvs, flag schemas, and parse!
const parsed = typeFlag(process.argv.slice(2), {
// Pass in flag schemas, and parse!
const parsed = typeFlag({

// Define flags here...

Expand Down Expand Up @@ -76,7 +76,7 @@ By default, all flags are optional so the flag type may include `undefined` in c
Make a flag required by setting `required: true` and TypeFlag will throw when it is not passed in.

```ts
const parsed = typeFlag(process.argv.slice(2), {
const parsed = typeFlag({
someNumber: {
type: Number,
required: true
Expand All @@ -97,7 +97,7 @@ Set a default value with the `default` property. When a default is provided, the
Pass in a function to return mutable values.

```ts
const parsed = typeFlag(process.argv.slice(2), {
const parsed = typeFlag({
someNumber: {
type: Number,
default: 1
Expand Down Expand Up @@ -186,7 +186,7 @@ function Size(size: Sizes) {
return size;
};

const parsed = typeFlag(process.argv.slice(2), {
const parsed = typeFlag({
size: Size
})
```
Expand All @@ -209,7 +209,7 @@ $ node ./cli --define:key=value
```

```ts
const parsed = typeFlag(process.argv.slice(2), {
const parsed = typeFlag({
define: String
})

Expand All @@ -234,7 +234,7 @@ function EnvObject(value: string): Env {
};
}

const parsed = typeFlag(process.argv.slice(2), {
const parsed = typeFlag({
env: [EnvObject],
});

Expand Down Expand Up @@ -267,7 +267,7 @@ $ node ./cli --boolean-flag false

## ⚙️ API

### typeFlag(argv, flagSchema)
### typeFlag(flagSchema, argv?)

Returns an object with the shape:
```ts
Expand All @@ -282,12 +282,6 @@ Returns an object with the shape:
}
```

#### argv
Type: `string[]`

The argv array to parse. Pass in `process.argv.slice(2)`.


#### flagSchema
Type:
```ts
Expand All @@ -305,6 +299,11 @@ type FlagSchema = {

An object containing flag schema definitions. Where the key is the flag name, and the value is either the type function or an object containing the type function and/or alias.

#### argv
Type: `string[]`

The argv array to parse. Defaults to `process.argv.slice(2)`.

## 🙋‍♀️ FAQ

### Why do I have to manually pass in `process.argv`?
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const isAliasPattern = /^-[\da-z]+/i;
const isFlagPattern = /^--[\w-]{2,}/;

function typeFlag<Schemas extends Flags>(
argv: string[],
schemas: Schemas,
argv: string[] = process.argv.slice(2),
) {
const aliasesMap = mapAliases(schemas);
const flags = createFlagsObject<Schemas>(schemas);
Expand Down
Loading

0 comments on commit 2295310

Please sign in to comment.