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