From 8ab5e194996a4dd338202f3a908ac929a9f8eb58 Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Fri, 27 Sep 2024 23:46:47 +0530 Subject: [PATCH] nicer subcommand api --- tribes-cli/remote/mod.ts | 2 +- tribes-cli/tribe/mod.ts | 4 ++-- tribes-cli/tribe/new.ts | 7 +++++-- tribes-cli/utils/command.ts | 16 ++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tribes-cli/remote/mod.ts b/tribes-cli/remote/mod.ts index 715dd62e..c08c7336 100644 --- a/tribes-cli/remote/mod.ts +++ b/tribes-cli/remote/mod.ts @@ -2,4 +2,4 @@ import { Command } from '../utils/mod.ts'; import { init } from './init.ts'; export const remote = new Command('remote', 'Manage a Ditto Tribes server') - .subcommand('init', init); + .subcommand(init); diff --git a/tribes-cli/tribe/mod.ts b/tribes-cli/tribe/mod.ts index b5aac37c..b98956cb 100644 --- a/tribes-cli/tribe/mod.ts +++ b/tribes-cli/tribe/mod.ts @@ -3,5 +3,5 @@ import { config } from './config.ts'; import { Command } from '../utils/mod.ts'; export const tribe = new Command('tribe', 'Create and manage tribes.') - .subcommand('create', create) - .subcommand('config', config); + .subcommand(create) + .subcommand(config); diff --git a/tribes-cli/tribe/new.ts b/tribes-cli/tribe/new.ts index 2991e84e..c69ce2a5 100644 --- a/tribes-cli/tribe/new.ts +++ b/tribes-cli/tribe/new.ts @@ -1,9 +1,12 @@ -import { Command } from '../utils/mod.ts'; +import { Command, ParsedArgs } from '../utils/mod.ts'; -export const create = new Command('create', 'Create a new tribe.') +export const create = new Command('new', 'Create a new tribe.') .option('-c --custom-domain', { description: 'Do not use a subdomain of the tribes server; instead, use a custom domain.', }) .option('-s --subdomain', { description: 'Use a subdomain of the tribes server to host the new Ditto instance.', + }) + .setAction((args: ParsedArgs) => { + // const ssh = sshConnect(); }); diff --git a/tribes-cli/utils/command.ts b/tribes-cli/utils/command.ts index b6acc161..0e8dcc43 100644 --- a/tribes-cli/utils/command.ts +++ b/tribes-cli/utils/command.ts @@ -1,4 +1,6 @@ import { Option, ParsedArgs } from './mod.ts'; +import { parseArgs } from '@std/cli'; +import { parseSubcommand } from './parsing.ts'; export class Command { name: string; @@ -36,11 +38,11 @@ export class Command { return this; } - subcommand(name: string, command: Command) { - if (this.isValidSubcommand(name)) { - throw new Error(`tribes-cli: ${name} is already a subcommand.`); + subcommand(command: Command) { + if (this.isValidSubcommand(command.name)) { + throw new Error(`tribes-cli: ${command.name} is already a subcommand.`); } - this.commands[name] = command; + this.commands[command.name] = command; return this; } @@ -48,4 +50,10 @@ export class Command { this.options[fmt] = option; return this; } + + parse(args: string[]) { + const parserArgs = parseSubcommand(this, {}); + const parsed = parseArgs(Deno.args, parserArgs); + return { parsed, parserArgs }; + } }