From 9f034fdf1de047777542285b7d973281345202db Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Fri, 27 Sep 2024 23:47:44 +0530 Subject: [PATCH] get rid of setupCli, use .option() to configure global methods --- tribes-cli/cli.ts | 27 +++++++++++---------------- tribes-cli/utils/mod.ts | 4 ++-- tribes-cli/utils/parsing.ts | 10 +--------- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/tribes-cli/cli.ts b/tribes-cli/cli.ts index 397fcce3..43496f4d 100644 --- a/tribes-cli/cli.ts +++ b/tribes-cli/cli.ts @@ -1,27 +1,22 @@ import { tribe } from './tribe/mod.ts'; import { remote } from './remote/mod.ts'; -import { Command, defaultIdentityFile, setupCli } from './utils/mod.ts'; +import { Command, defaultIdentityFile } from './utils/mod.ts'; async function main() { const tribes = new Command('tribes-cli', 'Create and manage Ditto Tribes servers.') - .subcommand('tribe', tribe) - .subcommand('remote', remote); + .subcommand(tribe) + .subcommand(remote) + .option('-i --identity-file', { + description: 'The ssh identity file to use. Defaults to ~/.ssh/id_rsa.', + default: await defaultIdentityFile(), + }); - const { parsed } = setupCli(tribes, { - string: ['identity-file'], - default: { - 'identity-file': await defaultIdentityFile(), - }, - }); - - const [s, v] = parsed._; + const { parsed } = tribes.parse(Deno.args); + const [s, v] = parsed._.slice(1); let cmd = tribes; - if (s && !v) { - cmd = tribes.getSubcommand(s); - } else if (s && v) { - cmd = tribes.getSubcommand(s).getSubcommand(v); - } + if (s) cmd = tribes.getSubcommand(s); + if (v) cmd = cmd.getSubcommand(v); await cmd.action(parsed); } diff --git a/tribes-cli/utils/mod.ts b/tribes-cli/utils/mod.ts index 9efdb3fd..036eeb5a 100644 --- a/tribes-cli/utils/mod.ts +++ b/tribes-cli/utils/mod.ts @@ -1,5 +1,5 @@ import { join } from '@std/path'; -import { ParsedArgs, setupCli } from './parsing.ts'; +import { ParsedArgs } from './parsing.ts'; import { Command } from './command.ts'; export const defaultIdentityFile = async () => { @@ -27,5 +27,5 @@ export const cleanArg = (arg: string) => { return arg.replace(/^--?/g, ''); }; -export { Command, setupCli }; +export { Command }; export type { ParsedArgs }; diff --git a/tribes-cli/utils/parsing.ts b/tribes-cli/utils/parsing.ts index 60945c2d..3a1c6bea 100644 --- a/tribes-cli/utils/parsing.ts +++ b/tribes-cli/utils/parsing.ts @@ -1,13 +1,5 @@ -import { parseArgs as stdParseArgs } from '@std/cli'; import { cleanArg, Command } from './mod.ts'; - -export const setupCli = (command: Command, parserArgs: Partial = {}) => { - parserArgs = parseSubcommand(command, parserArgs); - const parsed = stdParseArgs(Deno.args, parserArgs); - return { parsed, parserArgs }; -}; - -export type ParsedArgs = ReturnType['parsed']; +export type ParsedArgs = ReturnType['parsed']; export interface ParsedSubcommand { string: string[];