Commit e3c5da37 authored by nanahira's avatar nanahira

allow arg def in put param

parent 6419ff4b
......@@ -184,7 +184,8 @@ export const PutArgv = (field?: keyof Argv) =>
field ? CommandPut.decorate('argvField', field) : CommandPut.decorate('argv');
export const PutSession = (field?: keyof Session) =>
field ? CommandPut.decorate('sessionField', field) : PutArgv('session');
export const PutArg = (i: number) => CommandPut.decorate('arg', i);
export const PutArg = (index: number, decl?: Argv.Declaration) =>
CommandPut.decorate('arg', { index, decl });
export const PutArgs = () => CommandPut.decorate('args');
export const PutOption = (
name: string,
......
......@@ -159,3 +159,8 @@ export interface CommandLocaleDef extends Store {
options?: Dict<string>;
messages?: Store;
}
export interface CommandArgDef {
index: number;
decl?: Argv.Declaration;
}
import { Argv, Command, Context, FieldCollector, Session, User } from 'koishi';
import {
CommandArgDef,
CommandOptionConfig,
GenerateMappingStruct,
KoishiCommandPutDef,
......@@ -17,7 +18,7 @@ import {
export namespace CommandPut {
export interface ConfigMap {
args: void;
arg: number;
arg: CommandArgDef;
argv: void;
argvField: keyof Argv;
option: CommandOptionConfig;
......@@ -45,9 +46,13 @@ export namespace CommandPut {
);
preRegistry.extend('arg', (data, cmd, ctx, nativeType) => {
const arg = cmd._arguments[data];
const arg = cmd._arguments[data.index];
if (arg) {
applyNativeTypeToArg(arg, nativeType);
} else if (data.decl) {
const decl = { ...data.decl };
applyNativeTypeToArg(decl, nativeType);
cmd._arguments[data.index] = decl;
}
});
......@@ -81,7 +86,7 @@ export namespace CommandPut {
>();
registry.extend('args', (data, argv, args) => args);
registry.extend('arg', (data, argv, args) => args[data]);
registry.extend('arg', (data, argv, args) => args[data.index]);
registry.extend('argv', (data, argv, args) => argv);
registry.extend('argvField', (data, argv, args) => argv[data]);
registry.extend('option', (data, argv, args) => argv.options[data.name]);
......
import { Argv } from 'koishi';
// eslint-disable-next-line @typescript-eslint/ban-types
const nativeTypeMapping = new Map<Function, Argv.Type>();
export const nativeTypeMapping = new Map<Function, Argv.Type>();
nativeTypeMapping.set(String, 'string');
nativeTypeMapping.set(Number, 'number');
nativeTypeMapping.set(Boolean, 'boolean');
......
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