Skip to content

Commit

Permalink
Added config option to omit undefined keys from handler options
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Jun 29, 2024
1 parent e7ab929 commit 4c565e6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@drizzle-team/brocli",
"type": "module",
"author": "Drizzle Team",
"version": "0.2.4",
"version": "0.3.0",
"description": "Typed CLI command runner",
"license": "Apache-2.0",
"sideEffects": false,
Expand Down
18 changes: 15 additions & 3 deletions src/command-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type BroCliConfig = {
argSource?: string[];
help?: HelpHandler;
version?: string | Function;
omitKeysOfUndefinedOptions?: boolean;
};

export type GenericCommandHandler = (options?: Record<string, OutputType> | undefined) => any;
Expand Down Expand Up @@ -406,7 +407,11 @@ const parseArg = (
};
};

const parseOptions = (command: Command, args: string[]): Record<string, OutputType> | undefined => {
const parseOptions = (
command: Command,
args: string[],
omitKeysOfUndefinedOptions?: boolean,
): Record<string, OutputType> | undefined => {
const options = command.options;

const optEntries = Object.entries(options ?? {} as Exclude<typeof options, undefined>);
Expand Down Expand Up @@ -436,7 +441,13 @@ const parseOptions = (command: Command, args: string[]): Record<string, OutputTy
}

for (const [optKey, { config: option }] of optEntries) {
result[optKey] = result[optKey] ?? option.default;
const data = result[optKey] ?? option.default;

if (!omitKeysOfUndefinedOptions) {
result[optKey] = data;
} else {
if (data !== undefined) result[optKey] = data;
}

if (option.isRequired && result[optKey] === undefined) missingRequiredArr.push([option.name!, ...option.aliases]);
}
Expand Down Expand Up @@ -529,6 +540,7 @@ export const rawCli = async (commands: Command[], config?: BroCliConfig) => {
const argSource = config?.argSource ?? process.argv;
const version = config?.version;
const helpHandler = config?.help ?? defaultTheme;
const omitKeysOfUndefinedOptions = config?.omitKeysOfUndefinedOptions ?? false;
const cmds = [...processedCmds, helpCommand(processedCmds, helpHandler)];

let args = argSource.slice(2, argSource.length);
Expand Down Expand Up @@ -565,7 +577,7 @@ export const rawCli = async (commands: Command[], config?: BroCliConfig) => {
if (!command) throw unknownCommand();

args = [...args.slice(0, index), ...args.slice(index + 1, args.length)];
options = parseOptions(command, args);
options = parseOptions(command, args, omitKeysOfUndefinedOptions);
cmd = command;

await cmd.handler(options);
Expand Down
82 changes: 41 additions & 41 deletions src/help-themes.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import type { Command, HelpHandler } from './command-core';
import type { GenericProcessedOptions } from './option-builder';

// export const defaultTheme: HelpHandler = (calledFor) => {
// if (Array.isArray(calledFor)) {
// const cmds = calledFor.filter((cmd) => !cmd.hidden);

// const tableCmds = cmds.map((cmd) => ({
// name: cmd.name,
// aliases: cmd.aliases ? cmd.aliases.join(', ') : '-',
// description: cmd.description ?? '-',
// }));

// console.log(`Here's the list of all available commands:`);
// console.table(tableCmds);
// console.log(
// 'To read the details about any particular command type: help [commandName] | help --command=<commandName> | help -c <comandName>',
// );
// } else {
// const options = calledFor.options
// ? Object.values(calledFor.options).filter((opt) => !opt.config?.isHidden).map(
// ({ config: opt }) => ({
// name: opt.name,
// aliases: opt.aliases.length ? `${opt.aliases.join(', ')}` : '-',
// description: opt.description ?? '-',
// type: opt.type,
// required: opt.isRequired ? '✓' : '✗',
// }),
// )
// : undefined;

// console.log(
// `Command: ${calledFor.name}${calledFor.aliases ? ` [${calledFor.aliases.join(', ')}]` : ''}${
// calledFor.description ? ` - ${calledFor.description}` : ''
// }`,
// );

// if (!options?.length) return;

// console.log('\nOptions:');
// console.table(options);
// }
// };
export const defaultTheme: HelpHandler = (calledFor) => {
if (Array.isArray(calledFor)) {
const cmds = calledFor.filter((cmd) => !cmd.hidden);

const tableCmds = cmds.map((cmd) => ({
name: cmd.name,
aliases: cmd.aliases ? cmd.aliases.join(', ') : '-',
description: cmd.description ?? '-',
}));

console.log(`Here's the list of all available commands:`);
console.table(tableCmds);
console.log(
'To read the details about any particular command type: help [commandName] | help --command=<commandName> | help -c <comandName>',
);
} else {
const options = calledFor.options
? Object.values(calledFor.options).filter((opt) => !opt.config?.isHidden).map(
({ config: opt }) => ({
name: opt.name,
aliases: opt.aliases.length ? `${opt.aliases.join(', ')}` : '-',
description: opt.description ?? '-',
type: opt.type,
required: opt.isRequired ? '✓' : '✗',
}),
)
: undefined;

console.log(
`Command: ${calledFor.name}${calledFor.aliases ? ` [${calledFor.aliases.join(', ')}]` : ''}${
calledFor.description ? ` - ${calledFor.description}` : ''
}`,
);

if (!options?.length) return;

console.log('\nOptions:');
console.table(options);
}
};

// Root help
const rootHelp = (commands: Command[]) => {
Expand Down Expand Up @@ -195,7 +195,7 @@ const commandHelp = (command: Command) => {
};

// Theme core
export const defaultTheme: HelpHandler = (calledFor) => {
export const defaultThemeWIP: HelpHandler = (calledFor) => {
if (Array.isArray(calledFor)) {
rootHelp(calledFor);
} else {
Expand Down

0 comments on commit 4c565e6

Please sign in to comment.