get rid of setupCli, use .option() to configure global methods

This commit is contained in:
Siddharth Singh 2024-09-27 23:47:44 +05:30
parent 8ab5e19499
commit 9f034fdf1d
No known key found for this signature in database
3 changed files with 14 additions and 27 deletions

View file

@ -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);
}

View file

@ -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 };

View file

@ -1,13 +1,5 @@
import { parseArgs as stdParseArgs } from '@std/cli';
import { cleanArg, Command } from './mod.ts';
export const setupCli = (command: Command, parserArgs: Partial<ParsedSubcommand> = {}) => {
parserArgs = parseSubcommand(command, parserArgs);
const parsed = stdParseArgs(Deno.args, parserArgs);
return { parsed, parserArgs };
};
export type ParsedArgs = ReturnType<typeof setupCli>['parsed'];
export type ParsedArgs = ReturnType<Command['parse']>['parsed'];
export interface ParsedSubcommand {
string: string[];