Skip to content

Commit

Permalink
updated for node/deno compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Nov 1, 2024
1 parent cfdf4a6 commit 7d1000d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# cobra.js

A small cli framework for Deno. This is heavily inspired on the excellent
[Cobra](https://github.com/spf13/cobra). The framework relies on the Deno's
built-in [`flags`](https://deno.land/std/flags) library for the parsing of
A small cli framework for Deno/Node. This is heavily inspired on the excellent
[Cobra](https://github.com/spf13/cobra). The framework relies on
[inimist](https://github.com/minimistjs/minimist) library for the parsing of
arguments.

The foundation to a good cli is good help. Most of the complexity in Cobra.js is
Expand All @@ -26,7 +26,7 @@ export interface Cmd {

```typescript
// import the library
import { cli } from "https://deno.land/x/cobra/mod.ts";
import { cli } from "jsr:@aricart/cobra";

// create the root command
const root = cli({ use: "greeting (hello|goodbye) [--name name] [--strong]" });
Expand Down Expand Up @@ -57,7 +57,7 @@ const hello = root.addCommand({
To execute the cli, simply `root.execute()` with arguments:

```typescript
Deno.exit(await root.execute(Deno.args));
Deno.exit(await root.execute());
```

The return of the command is a `Promise<number>` which is a number you can use
Expand Down Expand Up @@ -117,23 +117,26 @@ available to all subcommands, reducing code duplication.
## Running your commands

Once you build your command tree and related, flags, you simply call the root
command's `execute()` with a list of arguments:
command's `execute()` with an optinoal list of arguments. If not provided it
will try to get them from `Deno.args` or `process.argv.slice(2)`

```typescript
Deno.exit(await root.execute(Deno.args));
Deno.exit(await root.execute());
```

## Help is built-in

Help is implemented as a persistent flag on the root command, simply passing
`-h` or `--help` to any command, will display help. The help you get will be
context sensitive. Container commands will list possible subcommands, and their
context-sensitive. Container commands will list possible subcommands, and their
persistent flags. A leaf command will show usage, flags and persistent flags.

This results in help that looks like this:

```bash
> deno run main.ts
# or
> node main.ts
greeting

Usage:
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"strict": true
},
"name": "@aricart/cobra",
"version": "0.0.9-1",
"version": "0.0.10",
"exports": {
".": "./mod.ts"
},
Expand Down
15 changes: 13 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,19 @@ export class RootCommand extends Command implements Execute {
return [cmd, a];
}

execute(args: string[] = Deno.args): Promise<number> {
const [cmd, a] = this.matchCmd(args);
execute(args: string[] | null = null): Promise<number> {
if (args === null) {
if ("Deno" in globalThis) {
//@ts-ignore: global
args = globalThis.Deno.args;
} else if ("process" in globalThis) {
//@ts-ignore: global
args = globalThis.process.argv.slice(2);
} else {
args = [];
}
}
const [cmd, a] = this.matchCmd(args!);
const flags = cmd.getFlags();

// deno-lint-ignore no-explicit-any
Expand Down
1 change: 0 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ Deno.test("flags - values with no value set returns empty", async () => {

let rv = await root.execute(["t"]);
assertEquals(rv, 0);
console.log(values);
assertEquals(values.length, 0);

rv = await root.execute(["t", "--v", "a"]);
Expand Down

0 comments on commit 7d1000d

Please sign in to comment.