unjs / citty

🌆 Elegant CLI Builder
Other
775 stars 26 forks source link

Undefined value of subcommand argument of run method #123

Closed Kotletka89 closed 8 months ago

Kotletka89 commented 8 months ago

Environment

node.js 21.0.0 LTS

Reproduction

just try the code

usage/output:

$ node test_citty.js format
Formatting!
undefined
This is the main command. Use 'format' or 'help'.
import { defineCommand, runMain } from "citty";

const formatCommand = defineCommand({
  meta: {
    name: "format",
    description: "Format something",
  },
  run() {
    console.log("Formatting!");
  },
});

const helpCommand = defineCommand({
  meta: {
    name: "help",
    description: "Display help information",
  },
  run() {
    console.log("Look at this!");
  },
});

const mainCommand = defineCommand({
  meta: {
    name: "main.js",
    version: "1.0.0",
    description: "CLI with 'format' and 'help' commands",
  },
  subCommands: {
    format: formatCommand,
    help: helpCommand,
  },
  run({subCommand}) {
    console.log(subCommand)
    console.log("This is the main command. Use 'format' or 'help'.");
  },
});

runMain(mainCommand);

Describe the bug

I expect it to have all properties like meta, args, and stuff of subcommand currently runned

Additional context

No response

Logs

No response

Kotletka89 commented 8 months ago

the lib lacks documentation, I don't know how to work with that

peterroe commented 8 months ago

You can try the following:

const formatCommand = defineCommand({
  meta: {
    name: "format",
    description: "Format something",
  },
- run() {
+ run(ctx) {
+   console.log(ctx)
    console.log("Formatting!");
  },
});
Kotletka89 commented 8 months ago

You can try the following:

const formatCommand = defineCommand({
  meta: {
    name: "format",
    description: "Format something",
  },
- run() {
+ run(ctx) {
+ console.log(ctx)
    console.log("Formatting!");
  },
});

thanks a lot! I got it