arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.12k stars 65 forks source link

route unexpected command when single user command registered. #57

Closed magicdawn closed 3 years ago

magicdawn commented 4 years ago

src/test.ts

#!/usr/bin/env ts-node-script

import {Cli, Command} from 'clipanion'

// greet [-v,--verbose] [--name ARG]
class GreetCommand extends Command {
  @Command.Boolean(`-v,--verbose`)
  verbose: boolean

  @Command.String(`--name`)
  name: string

  static paths = [[`greet`]]
  async execute() {
    if (typeof this.name === `undefined`) {
      this.context.stdout.write(`You're not registered.\n`)
    } else {
      this.context.stdout.write(`Hello, ${this.name}!\n`)
    }
  }
}

const cli = new Cli({
  binaryLabel: `My Utility`,
  binaryName: `bin`,
  binaryVersion: `1.0.0`,
})

cli.register(Command.Entries.Help)
cli.register(Command.Entries.Version)

cli.register(GreetCommand)

cli.runExit(process.argv.slice(2), {
  ...Cli.defaultContext,
})

when execute

$ ./src/test.ts             
You're not registered.

tsconfig.json

{
  "include": ["src/**/*"],
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "lib",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "target": "es2019",
    "module": "CommonJS",
    "skipLibCheck": true,
    "incremental": true,
    "experimentalDecorators": true
  }
}

the greet command should be called when execute ./src/test.ts greet, but not ./src/test.ts

magicdawn commented 3 years ago

@Command.Path() decorator will work, close this.