Skip to content

Commit

Permalink
chore: example nit
Browse files Browse the repository at this point in the history
  • Loading branch information
hongaar authored Dec 4, 2024
1 parent 24857ed commit 3ca5d1d
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions examples/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { command, program } from "../src/index.js";

function printArgs<T>(args: T) {
console.log("Args are", args);
}

/**
* Keep in mind that argument/option types are not validated at runtime.
* For example, when providing a default value with type boolean, it can be set
Expand All @@ -15,9 +19,7 @@ const string = command("string")
default: "foo",
description: "String option with default",
})
.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const number = command("number")
.argument("arg1", { type: "number", description: "Required number argument" })
Expand All @@ -31,11 +33,9 @@ const number = command("number")
variadic: true,
description: "Variadic number argument",
})
.option("opt1", { type: "number", description: "number option" })
.option("opt1", { type: "number", description: "Number option" })
.option("opt2", { default: 100, description: "Number option with default" })
.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const boolean = command("boolean")
.argument("arg1", {
Expand All @@ -52,15 +52,12 @@ const boolean = command("boolean")
variadic: true,
description: "Variadic boolean argument",
})
.option("opt1", { type: "boolean", description: "number option" })
.option("opt1", { type: "boolean", description: "Boolean option" })
.option("opt2", {
type: "boolean",
default: false,
description: "Number option with default",
description: "Boolean option with default",
})
.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const choices = command("choices")
.argument("arg", {
Expand All @@ -72,9 +69,7 @@ const choices = command("choices")
choices: ["option1", "option2"] as const,
default: "option3",
})
.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const defaultValues = command("default")
.argument("arg", {
Expand All @@ -83,9 +78,7 @@ const defaultValues = command("default")
optional: true,
})
.option("opt", { description: "Default value", default: true })
.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const constraints = command("constraint")
.argument("arg", {
Expand All @@ -110,9 +103,7 @@ const constraints = command("constraint")
requires: "optionalArg",
})

.action((args) => {
console.log("Args are", args);
});
.action(printArgs);

const app = program()
.description("All argument and option types")
Expand Down

0 comments on commit 3ca5d1d

Please sign in to comment.