index.d.ts 891 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. declare function arg<T extends arg.Spec>(
  2. spec: T,
  3. options?: arg.Options
  4. ): arg.Result<T>;
  5. declare namespace arg {
  6. export const flagSymbol: unique symbol;
  7. export function flag<T>(fn: T): T & { [arg.flagSymbol]: true };
  8. export const COUNT: Handler<number> & { [arg.flagSymbol]: true };
  9. export type Handler<T = any> = (
  10. value: string,
  11. name: string,
  12. previousValue?: T
  13. ) => T;
  14. export class ArgError extends Error {
  15. constructor(message: string, code: string);
  16. code: string;
  17. }
  18. export interface Spec {
  19. [key: string]: string | Handler | [Handler];
  20. }
  21. export type Result<T extends Spec> = { _: string[] } & {
  22. [K in keyof T]?: T[K] extends Handler
  23. ? ReturnType<T[K]>
  24. : T[K] extends [Handler]
  25. ? Array<ReturnType<T[K][0]>>
  26. : never;
  27. };
  28. export interface Options {
  29. argv?: string[];
  30. permissive?: boolean;
  31. stopAtPositional?: boolean;
  32. }
  33. }
  34. export = arg;