import type { ArgumentDefinitions, SerializableArgumentValues } from "../types.ts"; import type { DefinedTypedCommand, RegisteredTypedCommand, TypedCommandHandle, TypedSubcommandDefinitions, } from "../pi/command-types.ts"; /** Create the disposable handle returned from typed command registration. */ export function createCommandHandle< TDefinitions extends ArgumentDefinitions, TSubcommands extends TypedSubcommandDefinitions, >( definition: DefinedTypedCommand, command: RegisteredTypedCommand, invocationName: string, unregister: (command: RegisteredTypedCommand) => void, ): TypedCommandHandle { let disposed = false; return Object.freeze({ definition, invocationName, parse(rawArgs: string) { return definition.parse(rawArgs); }, serialize(values: SerializableArgumentValues) { return definition.serialize(values); }, serializeSubcommand(invocation) { return definition.serializeSubcommand(invocation); }, formatUsage() { return definition.formatUsage(); }, formatHelp() { return definition.formatHelp(); }, dispose() { if (disposed) { return; } disposed = true; unregister(command); }, }); }