mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
get rid of setupCli, use .option() to configure global methods
This commit is contained in:
parent
8ab5e19499
commit
9f034fdf1d
3 changed files with 14 additions and 27 deletions
|
|
@ -1,27 +1,22 @@
|
||||||
import { tribe } from './tribe/mod.ts';
|
import { tribe } from './tribe/mod.ts';
|
||||||
import { remote } from './remote/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() {
|
async function main() {
|
||||||
const tribes = new Command('tribes-cli', 'Create and manage Ditto Tribes servers.')
|
const tribes = new Command('tribes-cli', 'Create and manage Ditto Tribes servers.')
|
||||||
.subcommand('tribe', tribe)
|
.subcommand(tribe)
|
||||||
.subcommand('remote', remote);
|
.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, {
|
const { parsed } = tribes.parse(Deno.args);
|
||||||
string: ['identity-file'],
|
const [s, v] = parsed._.slice(1);
|
||||||
default: {
|
|
||||||
'identity-file': await defaultIdentityFile(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const [s, v] = parsed._;
|
|
||||||
|
|
||||||
let cmd = tribes;
|
let cmd = tribes;
|
||||||
if (s && !v) {
|
if (s) cmd = tribes.getSubcommand(s);
|
||||||
cmd = tribes.getSubcommand(s);
|
if (v) cmd = cmd.getSubcommand(v);
|
||||||
} else if (s && v) {
|
|
||||||
cmd = tribes.getSubcommand(s).getSubcommand(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
await cmd.action(parsed);
|
await cmd.action(parsed);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { join } from '@std/path';
|
import { join } from '@std/path';
|
||||||
import { ParsedArgs, setupCli } from './parsing.ts';
|
import { ParsedArgs } from './parsing.ts';
|
||||||
import { Command } from './command.ts';
|
import { Command } from './command.ts';
|
||||||
|
|
||||||
export const defaultIdentityFile = async () => {
|
export const defaultIdentityFile = async () => {
|
||||||
|
|
@ -27,5 +27,5 @@ export const cleanArg = (arg: string) => {
|
||||||
return arg.replace(/^--?/g, '');
|
return arg.replace(/^--?/g, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
export { Command, setupCli };
|
export { Command };
|
||||||
export type { ParsedArgs };
|
export type { ParsedArgs };
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,5 @@
|
||||||
import { parseArgs as stdParseArgs } from '@std/cli';
|
|
||||||
import { cleanArg, Command } from './mod.ts';
|
import { cleanArg, Command } from './mod.ts';
|
||||||
|
export type ParsedArgs = ReturnType<Command['parse']>['parsed'];
|
||||||
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 interface ParsedSubcommand {
|
export interface ParsedSubcommand {
|
||||||
string: string[];
|
string: string[];
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue