Skip to content

Commit

Permalink
showed use of persistent flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Apr 8, 2021
1 parent cf93a74 commit 8e20204
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand All @@ -39,13 +46,20 @@ const hello = root.addCommand({
// and an object to let you access relevant flags.
run: (cmd, args, flags): Promise<number> => {
const strong = (flags.value<boolean>("strong") ?? false) ? "!!!" : "";
const n = flags.value<string>("name") ?? "mystery person";
let n = flags.value<string>("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<number>` which is a number you can use
to provide to the `Deno.exit(n)` function.

Expand Down Expand Up @@ -87,6 +101,19 @@ hello.addFlag({
});
```

Processing flags is similarly trivial:

```typescript
const n = flags.value<string>("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
Expand Down
28 changes: 12 additions & 16 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand All @@ -11,31 +18,26 @@ const hello = root.addCommand({
// and an object to let you access relevant flags.
run: (cmd, args, flags): Promise<number> => {
const strong = (flags.value<boolean>("strong") ?? false) ? "!!!" : "";
const n = flags.value<string>("name") ?? "mystery person";
let n = flags.value<string>("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<number> => {
const strong = flags.value<boolean>("strong") ? "!!!" : "";
const n = flags.value<string>("name") ?? "mystery person";
const strong = (flags.value<boolean>("strong") ?? false) ? "!!!" : "";
let n = flags.value<string>("name");
n = n === "" ? "mystery person" : n;
cmd.stdout(`goodbye ${n}${strong}`);
return Promise.resolve(0);
},
Expand All @@ -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));

0 comments on commit 8e20204

Please sign in to comment.