Skip to content

Commit

Permalink
changed interface to type
Browse files Browse the repository at this point in the history
fixed a bug where runtime was not initialized in grouped commands
  • Loading branch information
aricart committed Dec 3, 2024
1 parent 12a125f commit 1eaf64c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
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.13",
"version": "0.0.15",
"exports": {
".": "./mod.ts"
},
Expand Down
29 changes: 19 additions & 10 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { sprintf } from "@std/fmt/printf";
import { getRuntime } from "@aricart/runtime";

/**
* The `Flag` interface represents a command-line argument or option that can be used in various
* applications. This interface includes properties that define the behavior, identity,
* The `Flag` type represents a command-line argument or option that can be used in various
* applications. This type includes properties that define the behavior, identity,
* and requirements of the flag.
*/
export type Flag = {
Expand Down Expand Up @@ -54,7 +54,7 @@ type FlagState = {
value: null | unknown | unknown[];
} & Flag;

export interface Flags {
export type Flags = {
/**
* Returns the value of the flag
* @param n
Expand All @@ -78,7 +78,7 @@ export interface Flags {
* If a flag is not specified, this will terminate the command with an error;
*/
checkRequired(): void;
}
};

/**
* The callback for a command.
Expand All @@ -98,7 +98,7 @@ export type Run = (
/**
* Configuration for a Cmd
*/
export interface Cmd {
export type Cmd ={
/**
* Single line usage for the command, the first word should be the name of the cmd
*/
Expand All @@ -114,7 +114,7 @@ export interface Cmd {
/**
* Callback for the command
*/
run: Run;
run?: Run;
}

export function isCommand(t: Command | Cmd): t is Command {
Expand Down Expand Up @@ -358,7 +358,7 @@ export class Command implements Cmd {

async run(cmd: Command, args: string[], flags: Flags): Promise<number> {
try {
const exit = await this.cmd.run(cmd, args, flags);
const exit = await this.cmd.run!(cmd, args, flags);
if (exit > 0 && this.showHelp) {
cmd.help();
}
Expand Down Expand Up @@ -407,7 +407,7 @@ export class Command implements Cmd {
}
}

export interface Execute {
export type Execute = {
execute(args: string[]): void;
}

Expand Down Expand Up @@ -472,11 +472,20 @@ export class RootCommand extends Command implements Execute {
this.stdout = runtime.stdout;
this.stderr = runtime.stderr;
this.exit = runtime.exit;
this.commands?.forEach((c) => {

const stack: Command[] = [];
if (this.commands) {
stack.push(...this.commands);
}
while (stack.length) {
const c = stack.pop()!;
c.stdout = runtime.stdout;
c.stderr = runtime.stderr;
c.exit = runtime.exit;
});
if (c.commands?.length) {
stack.push(...c.commands);
}
}
if (args === null) {
args = runtime.args();
}
Expand Down
14 changes: 14 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,17 @@ Deno.test("flags - values with no value set returns empty", async () => {
assertEquals(rv, 0);
assertEquals(values.length, 0);
});

Deno.test("groups", async () => {
const root = cli({ use: "test" });
const hello = root.addCommand({ use: "hello" });
hello.addCommand({
use: "hi",
run: (cmd, _args, _flags): Promise<number> => {
cmd.stdout("hello");
return Promise.resolve(0);
},
});

await root.execute(["hello", "hi"]);
});

0 comments on commit 1eaf64c

Please sign in to comment.