https://github.com/moontaiworks/yargs-typebox
Use TypeBox to define your yargs arguments.
https://github.com/moontaiworks/yargs-typebox
arguments typebox typescript yargs
Last synced: 11 months ago
JSON representation
Use TypeBox to define your yargs arguments.
- Host: GitHub
- URL: https://github.com/moontaiworks/yargs-typebox
- Owner: moontaiworks
- License: mit
- Created: 2024-10-04T06:45:02.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-09T07:55:15.000Z (over 1 year ago)
- Last Synced: 2024-12-12T10:48:20.865Z (over 1 year ago)
- Topics: arguments, typebox, typescript, yargs
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/yargs-typebox
- Size: 191 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# yargs-typebox
[](https://www.npmjs.com/package/yargs-typebox)
[](https://www.npmjs.com/package/yargs-typebox)
[](https://codecov.io/gh/moontaiworks/yargs-typebox)
Use [TypeBox](https://github.com/sinclairzx81/typebox) to define your [yargs](https://www.npmjs.com/package/yargs) arguments.
## Install
### NPM
```bash
npm install yargs-typebox
```
### Yarn
```bash
yarn add yargs-typebox
```
### PNPM
```bash
pnpm add yargs-typebox
```
## Usage
### getOptions(TObject)
Transform a whole TypeBox object into yargs options object.
```typescript
import { type Static, Type } from "@sinclair/typebox";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getOptions } from "yargs-typebox";
const schema = Type.Object({
page: Type.Number({ description: "page number" }),
size: Type.Number({ description: "page size", default: 10 }),
query: Type.Optional(Type.String()),
sort: Type.Optional(
Type.Array(Type.Union([Type.Literal("id"), Type.Literal("createdAt")])),
),
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")], {
default: "asc",
implies: ["sort"],
}),
pretty: Type.Boolean({ description: "pretty print" }),
count: Type.Any({ description: "count", count: true }),
});
const options = getOptions(schema);
// {
// page: {
// type: "number",
// demandOption: true,
// describe: "page number",
// },
// size: {
// type: "number",
// default: 10,
// demandOption: false,
// describe: "page size",
// },
// query: {
// type: "string",
// demandOption: false,
// },
// sort: {
// type: "array",
// demandOption: false,
// choices: ["id", "createdAt"],
// },
// order: {
// type: "string",
// implies: ["sort"],
// default: "asc",
// demandOption: false,
// choices: ["asc", "desc"],
// },
// pretty: {
// type: "boolean",
// demandOption: true,
// describe: "pretty print",
// },
// count: {
// count: true,
// demandOption: true,
// describe: "count",
// },
// }
const argv = yargs(hideBin(process.argv))
.options(options)
.help()
.parse() as Static;
console.log(argv);
```
### getOption(TSchema)
Transform a single TypeBox schema into yargs option.
```typescript
import { Type } from "@sinclair/typebox";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getOption } from "yargs-typebox";
const prettyArgument = Type.Boolean({ description: "pretty print" });
const countArgument = Type.Any({ description: "count", count: true });
const prettyOption = getOption(prettyArgument);
// {
// type: "boolean",
// demandOption: true,
// describe: "pretty print",
// }
const countOption = getOption(countArgument);
// {
// count: true,
// demandOption: true,
// describe: "count",
// }
const argv = yargs(hideBin(process.argv))
.option("pretty", prettyOption)
.option("count", countOption)
.help()
.parse();
console.log(argv);
```
## API Document
For more informations, see the [API documentation](https://moontaiworks.github.io/yargs-typebox/).