Commit 6419ff4b authored by nanahira's avatar nanahira

infer arg type

parent d3fb2095
...@@ -7,7 +7,11 @@ import { ...@@ -7,7 +7,11 @@ import {
TemplateConfig, TemplateConfig,
} from '../../def'; } from '../../def';
import { MethodRegistry } from '../abstract-registry'; import { MethodRegistry } from '../abstract-registry';
import { applyOptionToCommand, registerTemplate } from '../../utility'; import {
applyNativeTypeToArg,
applyOptionToCommand,
registerTemplate,
} from '../../utility';
// eslint-disable-next-line @typescript-eslint/no-namespace // eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CommandPut { export namespace CommandPut {
...@@ -40,6 +44,13 @@ export namespace CommandPut { ...@@ -40,6 +44,13 @@ export namespace CommandPut {
applyOptionToCommand(ctx, cmd, data, nativeType), 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) => { preRegistry.extend('user', (data, cmd) => {
if (data) { if (data) {
cmd.userFields(data); cmd.userFields(data);
......
export * from './utility'; 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 { import {
CommandOptionConfig, CommandOptionConfig,
ContextSelector, ContextSelector,
OnContextFunction, OnContextFunction,
TemplateConfig, TemplateConfig,
} from '../def'; } from '../def';
import { applyNativeTypeToArg } from './native-type-mapping';
export function applySelector( export function applySelector(
ctx: Context, ctx: Context,
...@@ -55,13 +56,6 @@ export const registerTemplate = ( ...@@ -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( export function applyOptionToCommand(
ctx: Context, ctx: Context,
cmd: Command, cmd: Command,
...@@ -81,11 +75,6 @@ export function applyOptionToCommand( ...@@ -81,11 +75,6 @@ export function applyOptionToCommand(
delete clonedConfig.description; delete clonedConfig.description;
cmd = cmd.option(name, desc, clonedConfig); cmd = cmd.option(name, desc, clonedConfig);
const option = cmd._options[name]; const option = cmd._options[name];
if (!option.type && nativeType) { applyNativeTypeToArg(option, nativeType);
const inferredType = nativeTypeMapping.get(nativeType);
if (inferredType) {
option.type = inferredType;
}
}
return cmd; 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