index.d.ts 883 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. declare const flagSymbol: unique symbol;
  2. declare function arg<T extends arg.Spec>(
  3. spec: T,
  4. options?: arg.Options
  5. ): arg.Result<T>;
  6. declare namespace arg {
  7. export function flag<T>(fn: T): T & { [flagSymbol]: true };
  8. export const COUNT: Handler<number> & { [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;