mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
create getOptionName and getOptionAliases
also fixed a logical error where the default help option would accidentally clear a few fields
This commit is contained in:
parent
81eb1fafc3
commit
5cbdf30501
1 changed files with 17 additions and 7 deletions
|
|
@ -8,21 +8,31 @@ export interface ParsedSubcommand {
|
||||||
default: Record<string, string | boolean>;
|
default: Record<string, string | boolean>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultParsedSubcommand = (): ParsedSubcommand => {
|
export const defaultParsedCommand = (): ParsedSubcommand => {
|
||||||
return {
|
return {
|
||||||
string: [],
|
string: [],
|
||||||
boolean: [],
|
boolean: ['help'],
|
||||||
alias: {},
|
alias: { help: ['h'] },
|
||||||
default: {},
|
default: {},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseSubcommand = (command: Command, existing?: Partial<ParsedSubcommand>): ParsedSubcommand => {
|
const getOptionAliases = (fmt: string) => {
|
||||||
const res = Object.assign(defaultParsedSubcommand(), existing);
|
const split = fmt.split(' ').toSorted((a, b) => b.length - a.length).map(cleanArg);
|
||||||
|
return split;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getOptionName = (fmt: string) => {
|
||||||
|
const split = getOptionAliases(fmt);
|
||||||
|
return split[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const parseCommand = (command: Command, existing?: Partial<ParsedSubcommand>): ParsedSubcommand => {
|
||||||
|
const res = Object.assign(defaultParsedCommand(), existing);
|
||||||
|
|
||||||
if (command.options) {
|
if (command.options) {
|
||||||
for (const option in command.options) {
|
for (const option in command.options) {
|
||||||
const split = option.split(' ').toSorted((a, b) => b.length - a.length).map(cleanArg);
|
const split = getOptionAliases(option);
|
||||||
const name = split[0];
|
const name = split[0];
|
||||||
const body = command.options[option];
|
const body = command.options[option];
|
||||||
if ('bool' in body) {
|
if ('bool' in body) {
|
||||||
|
|
@ -36,7 +46,7 @@ export const parseSubcommand = (command: Command, existing?: Partial<ParsedSubco
|
||||||
|
|
||||||
if (command.commands) {
|
if (command.commands) {
|
||||||
for (const subcommand in command.commands) {
|
for (const subcommand in command.commands) {
|
||||||
const parsed = parseSubcommand(command.commands[subcommand]);
|
const parsed = parseCommand(command.commands[subcommand]);
|
||||||
Object.assign(res.alias, parsed.alias);
|
Object.assign(res.alias, parsed.alias);
|
||||||
Object.assign(res.default, parsed.default);
|
Object.assign(res.default, parsed.default);
|
||||||
res.boolean.push(...parsed.boolean);
|
res.boolean.push(...parsed.boolean);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue