Commit 6419ff4b authored by nanahira's avatar nanahira

infer arg type

parent d3fb2095
......@@ -7,7 +7,11 @@ import {
TemplateConfig,
} from '../../def';
import { MethodRegistry } from '../abstract-registry';
import { applyOptionToCommand, registerTemplate } from '../../utility';
import {
applyNativeTypeToArg,
applyOptionToCommand,
registerTemplate,
} from '../../utility';
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CommandPut {
......@@ -40,6 +44,13 @@ export namespace CommandPut {
applyOptionToCommand(ctx, cmd, data, nativeType),
);
preRegistry.extend('arg', (data, cmd, ctx, nativeType) => {
const arg = cmd._arguments[data];
if (arg) {
applyNativeTypeToArg(arg, nativeType);
}
});
preRegistry.extend('user', (data, cmd) => {
if (data) {
cmd.userFields(data);
......
export * from './utility';
export * from './native-type-mapping';
import { Argv } from 'koishi';
// eslint-disable-next-line @typescript-eslint/ban-types
const nativeTypeMapping = new Map<Function, Argv.Type>();
nativeTypeMapping.set(String, 'string');
nativeTypeMapping.set(Number, 'number');
nativeTypeMapping.set(Boolean, 'boolean');
nativeTypeMapping.set(Date, 'date');
export function applyNativeTypeToArg(
arg: Argv.Declaration,
// eslint-disable-next-line @typescript-eslint/ban-types
nativeType: Function,
) {
if (arg.type || !nativeType) {
return;
}
if (nativeTypeMapping.has(nativeType)) {
arg.type = nativeTypeMapping.get(nativeType);
}
}
import { Argv, Command, Context, Dict } from 'koishi';
import { Command, Context, Dict } from 'koishi';
import {
CommandOptionConfig,
ContextSelector,
OnContextFunction,
TemplateConfig,
} from '../def';
import { applyNativeTypeToArg } from './native-type-mapping';
export function applySelector(
ctx: Context,
......@@ -55,13 +56,6 @@ export const registerTemplate = (
}
};
// eslint-disable-next-line @typescript-eslint/ban-types
const nativeTypeMapping = new Map<Function, Argv.Type>();
nativeTypeMapping.set(String, 'string');
nativeTypeMapping.set(Number, 'number');
nativeTypeMapping.set(Boolean, 'boolean');
nativeTypeMapping.set(Date, 'date');
export function applyOptionToCommand(
ctx: Context,
cmd: Command,
......@@ -81,11 +75,6 @@ export function applyOptionToCommand(
delete clonedConfig.description;
cmd = cmd.option(name, desc, clonedConfig);
const option = cmd._options[name];
if (!option.type && nativeType) {
const inferredType = nativeTypeMapping.get(nativeType);
if (inferredType) {
option.type = inferredType;
}
}
applyNativeTypeToArg(option, nativeType);
return cmd;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment