From 8e20204948551fe73db9074415b2976083146307 Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Wed, 7 Apr 2021 20:50:52 -0500 Subject: [PATCH] showed use of `persistent` flags. --- README.md | 29 ++++++++++++++++++++++++++++- main.ts | 28 ++++++++++++---------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5b3a6a5..1a156c1 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,13 @@ import { cli } from "https://deno.land/x/cobra/mod.ts"; // create the root command const root = cli({ use: "greeting (hello|goodbye) [--name name] [--strong]" }); +root.addFlag({ + short: "s", + name: "strong", + type: "boolean", + usage: "say message strongly", + persistent: true, +}); // add a subcommand const hello = root.addCommand({ use: "hello --name string [--strong]", @@ -39,13 +46,20 @@ const hello = root.addCommand({ // and an object to let you access relevant flags. run: (cmd, args, flags): Promise => { const strong = (flags.value("strong") ?? false) ? "!!!" : ""; - const n = flags.value("name") ?? "mystery person"; + let n = flags.value("name"); + n = n === "" ? "mystery person" : n; cmd.stdout(`hello ${n}${strong}`); return Promise.resolve(0); }, }); ``` +To execute the cli, simply `root.execute()` with arguments: + +```typescript +Deno.exit(await root.execute(Deno.args)); +``` + The return of the command is a `Promise` which is a number you can use to provide to the `Deno.exit(n)` function. @@ -87,6 +101,19 @@ hello.addFlag({ }); ``` +Processing flags is similarly trivial: + +```typescript +const n = flags.value("name"); +``` + +The value returned will be the one entered by the user, or the specified default +in the configuration for the default value for the type - `""` or `false` or +`0`. + +Flags marked as `persistent` can be associated with a container command and are +available to all subcommands, reducing code duplication. + ## Running your commands Once you build your command tree and related, flags, you simply call the root diff --git a/main.ts b/main.ts index 73c2290..391efa6 100644 --- a/main.ts +++ b/main.ts @@ -2,6 +2,13 @@ import { cli } from "./mod.ts"; // create the root command const root = cli({ use: "greeting (hello|goodbye) [--name name] [--strong]" }); +root.addFlag({ + short: "s", + name: "strong", + type: "boolean", + usage: "say message strongly", + persistent: true, +}); // add a subcommand const hello = root.addCommand({ use: "hello --name string [--strong]", @@ -11,31 +18,26 @@ const hello = root.addCommand({ // and an object to let you access relevant flags. run: (cmd, args, flags): Promise => { const strong = (flags.value("strong") ?? false) ? "!!!" : ""; - const n = flags.value("name") ?? "mystery person"; + let n = flags.value("name"); + n = n === "" ? "mystery person" : n; cmd.stdout(`hello ${n}${strong}`); return Promise.resolve(0); }, }); - hello.addFlag({ short: "n", name: "name", type: "string", usage: "name to say hello to", }); -hello.addFlag({ - short: "s", - name: "strong", - type: "boolean", - usage: "say hello strongly", -}); const goodbye = root.addCommand({ use: "goodbye --name string [--strong]", short: "says goodbye", run: (cmd, args, flags): Promise => { - const strong = flags.value("strong") ? "!!!" : ""; - const n = flags.value("name") ?? "mystery person"; + const strong = (flags.value("strong") ?? false) ? "!!!" : ""; + let n = flags.value("name"); + n = n === "" ? "mystery person" : n; cmd.stdout(`goodbye ${n}${strong}`); return Promise.resolve(0); }, @@ -46,11 +48,5 @@ goodbye.addFlag({ type: "string", usage: "name to say goodbye to", }); -goodbye.addFlag({ - short: "s", - name: "strong", - type: "boolean", - usage: "say goodbye strongly", -}); Deno.exit(await root.execute(Deno.args));