Skip to main content
Module

x/cliffy/command/command.ts

Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Extremely Popular
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904
// deno-lint-ignore-file no-explicit-anyimport { UnknownType, ValidationError as FlagsValidationError,} from "../flags/_errors.ts";import { MissingRequiredEnvVar } from "./_errors.ts";import { parseFlags } from "../flags/flags.ts";import type { IDefaultValue } from "../flags/types.ts";import { getDescription, parseArgumentsDefinition, splitArguments,} from "./_utils.ts";import { blue, bold, red, yellow } from "./deps.ts";import { CommandExecutableNotFound, CommandNotFound, DefaultCommandNotFound, DuplicateCommandAlias, DuplicateCommandName, DuplicateCompletion, DuplicateEnvironmentVariable, DuplicateExample, DuplicateOptionName, DuplicateType, EnvironmentVariableOptionalValue, EnvironmentVariableSingleValue, EnvironmentVariableVariadicValue, MissingArgument, MissingArguments, MissingCommandName, NoArgumentsAllowed, TooManyArguments, UnknownCommand, ValidationError,} from "./_errors.ts";import { BooleanType } from "./types/boolean.ts";import { FileType } from "./types/file.ts";import { NumberType } from "./types/number.ts";import { StringType } from "./types/string.ts";import { Type } from "./type.ts";import { HelpGenerator } from "./help/_help_generator.ts";import type { HelpOptions } from "./help/_help_generator.ts";import type { IAction, IArgument, ICommandGlobalOption, ICommandOption, ICompleteHandler, ICompleteOptions, ICompletion, IDescription, IEnvVar, IEnvVarOptions, IEnvVarValueHandler, IExample, IFlagValueHandler, IGlobalEnvVarOptions, IHelpHandler, IOption, IParseResult, IType, ITypeInfo, ITypeOptions, IVersionHandler, MapTypes, TypeOrTypeHandler,} from "./types.ts";import { IntegerType } from "./types/integer.ts";import { underscoreToCamelCase } from "../flags/_utils.ts";
export class Command< CPG extends Record<string, unknown> | void = void, CPT extends Record<string, unknown> | void = CPG extends number ? any : void, CO extends Record<string, unknown> | void = CPG extends number ? any : void, CA extends Array<unknown> = CPG extends number ? any : [], CG extends Record<string, unknown> | void = CPG extends number ? any : void, CT extends Record<string, unknown> | void = CPG extends number ? any : { number: number; integer: number; string: string; boolean: boolean; file: string; }, CGT extends Record<string, unknown> | void = CPG extends number ? any : void, CP extends Command<any> | undefined = CPG extends number ? any : undefined,> { private types: Map<string, IType> = new Map(); private rawArgs: Array<string> = []; private literalArgs: Array<string> = []; // @TODO: get script name: https://github.com/denoland/deno/pull/5034 // private name: string = location.pathname.split( '/' ).pop() as string; private _name = "COMMAND"; private _parent?: CP; private _globalParent?: Command<any>; private ver?: IVersionHandler; private desc: IDescription = ""; private _usage?: string; private fn?: IAction; private options: Array<IOption> = []; private commands: Map<string, Command<any>> = new Map(); private examples: Array<IExample> = []; private envVars: Array<IEnvVar> = []; private aliases: Array<string> = []; private completions: Map<string, ICompletion> = new Map(); private cmd: Command<any> = this; private argsDefinition?: string; private isExecutable = false; private throwOnError = false; private _allowEmpty = false; private _stopEarly = false; private defaultCommand?: string; private _useRawArgs = false; private args: Array<IArgument> = []; private isHidden = false; private isGlobal = false; private hasDefaults = false; private _versionOptions?: IDefaultOption | false; private _helpOptions?: IDefaultOption | false; private _versionOption?: IOption; private _helpOption?: IOption; private _help?: IHelpHandler; private _shouldExit?: boolean; private _meta: Record<string, string> = {}; private _groupName?: string; private _noGlobals = false;
/** Disable version option. */ public versionOption(enable: false): this;
/** * Set global version option. * @param flags The flags of the version option. * @param desc The description of the version option. * @param opts Version option options. */ public versionOption( flags: string, desc?: string, opts?: ICommandOption<Partial<CO>, CA, CG, CPG, CT, CGT, CPT, CP> & { global: true; }, ): this;
/** * Set version option. * @param flags The flags of the version option. * @param desc The description of the version option. * @param opts Version option options. */ public versionOption( flags: string, desc?: string, opts?: ICommandOption<CO, CA, CG, CPG, CT, CGT, CPT, CP>, ): this;
/** * Set version option. * @param flags The flags of the version option. * @param desc The description of the version option. * @param opts The action of the version option. */ public versionOption( flags: string, desc?: string, opts?: IAction<CO, CA, CG, CPG, CT, CGT, CPT, CP>, ): this;
public versionOption( flags: string | false, desc?: string, opts?: | IAction<CO, CA, CG, CPG, CT, CGT, CPT, CP> | ICommandOption<CO, CA, CG, CPG, CT, CGT, CPT, CP> | ICommandOption<Partial<CO>, CA, CG, CPG, CT, CGT, CPT, CP> & { global: true; }, ): this { this._versionOptions = flags === false ? flags : { flags, desc, opts: typeof opts === "function" ? { action: opts } : opts, }; return this; }
/** Disable help option. */ public helpOption(enable: false): this;
/** * Set global help option. * @param flags The flags of the help option. * @param desc The description of the help option. * @param opts Help option options. */ public helpOption( flags: string, desc?: string, opts?: ICommandOption<Partial<CO>, CA, CG, CPG, CT, CGT, CPT, CP> & { global: true; }, ): this;
/** * Set help option. * @param flags The flags of the help option. * @param desc The description of the help option. * @param opts Help option options. */ public helpOption( flags: string, desc?: string, opts?: ICommandOption<CO, CA, CG, CPG, CT, CGT, CPT, CP>, ): this;
/** * Set help option. * @param flags The flags of the help option. * @param desc The description of the help option. * @param opts The action of the help option. */ public helpOption( flags: string, desc?: string, opts?: IAction<CO, CA, CG, CPG, CT, CGT, CPT, CP>, ): this;
public helpOption( flags: string | false, desc?: string, opts?: | IAction<CO, CA, CG, CPG, CT, CGT, CPT, CP> | ICommandOption<CO, CA, CG, CPG, CT, CGT, CPT, CP> | ICommandOption<Partial<CO>, CA, CG, CPG, CT, CGT, CPT, CP> & { global: true; }, ): this { this._helpOptions = flags === false ? flags : { flags, desc, opts: typeof opts === "function" ? { action: opts } : opts, }; return this; }
/** * Add new sub-command. * @param name Command definition. E.g: `my-command <input-file:string> <output-file:string>` * @param cmd The new child command to register. * @param override Override existing child command. */ public command< C extends Command< (G & Record<string, unknown>) | void | undefined, T | void | undefined, Record<string, unknown> | void, Array<unknown>, Record<string, unknown> | void, Record<string, unknown> | void, Record<string, unknown> | void, Command< G | void | undefined, T | void | undefined, Record<string, unknown> | void, Array<unknown>, Record<string, unknown> | void, Record<string, unknown> | void, Record<string, unknown> | void, undefined > >, G extends (CP extends Command<any> ? CPG : Merge<CPG, CG>), T extends (CP extends Command<any> ? CPT : Merge<CPT, CT>), >( name: string, cmd: C, override?: boolean, ): ReturnType<C["reset"]> extends Command< Record<string, unknown> | void, Record<string, unknown> | void, infer Options, infer Arguments, infer GlobalOptions, infer Types, infer GlobalTypes, undefined > ? Command< G, T, Options, Arguments, GlobalOptions, Types, GlobalTypes, OneOf<CP, this> > : never;
/** * Add new sub-command. * @param name Command definition. E.g: `my-command <input-file:string> <output-file:string>` * @param cmd The new child command to register. * @param override Override existing child command. */ public command< C extends Command< G | void | undefined, T | void | undefined, Record<string, unknown> | void, Array<unknown>, Record<string, unknown> | void, Record<string, unknown> | void, Record<string, unknown> | void, OneOf<CP, this> | undefined >, G extends (CP extends Command<any> ? CPG : Merge<CPG, CG>), T extends (CP extends Command<any> ? CPT : Merge<CPT, CT>), >( name: string, cmd: C, override?: boolean, ): C extends Command< Record<string, unknown> | void, Record<string, unknown> | void, infer Options, infer Arguments, infer GlobalOptions, infer Types, infer GlobalTypes, OneOf<CP, this> | undefined > ? Command< G, T, Options, Arguments, GlobalOptions, Types, GlobalTypes, OneOf<CP, this> > : never;
/** * Add new sub-command. * @param name Command definition. E.g: `my-command <input-file:string> <output-file:string>` * @param desc The description of the new child command. * @param override Override existing child command. */ public command< N extends string, A extends TypedCommandArguments< N, CP extends Command<any> ? CPT : Merge<CPT, CGT> >, >( name: N, desc?: string, override?: boolean, ): CPG extends number ? Command<any> : Command< CP extends Command<any> ? CPG : Merge<CPG, CG>, CP extends Command<any> ? CPT : Merge<CPT, CGT>, void, A, void, void, void, OneOf<CP, this> >;
/** * Add new sub-command. * @param nameAndArguments Command definition. E.g: `my-command <input-file:string> <output-file:string>` * @param cmdOrDescription The description of the new child command. * @param override Override existing child command. */ command( nameAndArguments: string, cmdOrDescription?: Command<any> | string, override?: boolean, ): Command<any> { this.reset();
const result = splitArguments(nameAndArguments);
const name: string | undefined = result.flags.shift(); const aliases: string[] = result.flags;
if (!name) { throw new MissingCommandName(); }
if (this.getBaseCommand(name, true)) { if (!override) { throw new DuplicateCommandName(name); } this.removeCommand(name); }
let description: string | undefined; let cmd: Command<any>;
if (typeof cmdOrDescription === "string") { description = cmdOrDescription; }
if (cmdOrDescription instanceof Command) { cmd = cmdOrDescription.reset(); } else { cmd = new Command(); }
cmd._name = name; cmd._parent = this;
if (description) { cmd.description(description); }
if (result.typeDefinition) { cmd.arguments(result.typeDefinition); }
aliases.forEach((alias: string) => cmd.alias(alias));
this.commands.set(name, cmd);
this.select(name);
return this; }
/** * Add new command alias. * @param alias Tha name of the alias. */ public alias(alias: string): this { if (this.cmd._name === alias || this.cmd.aliases.includes(alias)) { throw new DuplicateCommandAlias(alias); }
this.cmd.aliases.push(alias);
return this; }
/** Reset internal command reference to main command. */ public reset(): OneOf<CP, this> { this._groupName = undefined; this.cmd = this; return this as OneOf<CP, this>; }
/** * Set internal command pointer to child command with given name. * @param name The name of the command to select. */ public select< O extends Record<string, unknown> | void = any, A extends Array<unknown> = any, G extends Record<string, unknown> | void = any, >(name: string): Command<CPG, CPT, O, A, G, CT, CGT, CP> { const cmd = this.getBaseCommand(name, true);
if (!cmd) { throw new CommandNotFound(name, this.getBaseCommands(true)); }
this.cmd = cmd;
return this as Command<any>; }
/***************************************************************************** **** SUB HANDLER ************************************************************ *****************************************************************************/
/** Set command name. */ public name(name: string): this { this.cmd._name = name; return this; }
/** * Set command version. * @param version Semantic version string string or method that returns the version string. */ public version( version: | string | IVersionHandler<Partial<CO>, Partial<CA>, CG, CPG, CT, CGT, CPT, CP>, ): this { if (typeof version === "string") { this.cmd.ver = () => version; } else if (typeof version === "function") { this.cmd.ver = version; } return this; }
public meta(name: string, value: string): this { this.cmd._meta[name] = value; return this; }
public getMeta(): Record<string, string>; public getMeta(name: string): string; public getMeta(name?: string): Record<string, string> | string { return typeof name === "undefined" ? this._meta : this._meta[name]; }
/** * Set command help. * @param help Help string, method, or config for generator that returns the help string. */ public help( help: | string | IHelpHandler<Partial<CO>, Partial<CA>, CG, CPG> | HelpOptions, ): this { if (typeof help === "string") { this.cmd._help = () => help; } else if (typeof help === "function") { this.cmd._help = help; } else { this.cmd._help = (cmd: Command, options: HelpOptions): string => HelpGenerator.generate(cmd, { ...help, ...options }); } return this; }
/** * Set the long command description. * @param description The command description. */ public description( description: IDescription<CO, CA, CG, CPG, CT, CGT, CPT, CP>, ): this { this.cmd.desc = description; return this; }
/** * Set the command usage. Defaults to arguments. * @param usage The command usage. */ public usage(usage: string): this { this.cmd._usage = usage; return this; }
/** * Hide command from help, completions, etc. */ public hidden(): this { this.cmd.isHidden = true; return this; }
/** Make command globally available. */ public global(): this { this.cmd.isGlobal = true; return this; }
/** Make command executable. */ public executable(): this { this.cmd.isExecutable = true; return this; }
/** * Set command arguments: * * <requiredArg:string> [optionalArg: number] [...restArgs:string] */ public arguments< A extends TypedArguments<N, Merge<CPT, Merge<CGT, CT>>>, N extends string = string, >( args: N, ): Command<CPG, CPT, CO, A, CG, CT, CGT, CP> { this.cmd.argsDefinition = args; return this as Command<any>; }
/** * Set command callback method. * @param fn Command action handler. */ public action(fn: IAction<CO, CA, CG, CPG, CT, CGT, CPT, CP>): this { this.cmd.fn = fn; return this; }
/** * Don't throw an error if the command was called without arguments. * @param allowEmpty Enable/disable allow empty. */ public allowEmpty<T extends boolean | undefined = undefined>( allowEmpty?: T, ): false extends T ? this : Command<Partial<CPG>, CPT, Partial<CO>, CA, CG, CT, CGT, CP> { this.cmd._allowEmpty = allowEmpty !== false; return this; }
/** * Enable stop early. If enabled, all arguments starting from the first non * option argument will be passed as arguments with type string to the command * action handler. * * For example: * `command --debug-level warning server --port 80` * * Will result in: * - options: `{debugLevel: 'warning'}` * - args: `['server', '--port', '80']` * * @param stopEarly Enable/disable stop early. */ public stopEarly(stopEarly = true): this { this.cmd._stopEarly = stopEarly; return this; }
/** * Disable parsing arguments. If enabled the raw arguments will be passed to * the action handler. This has no effect for parent or child commands. Only * for the command on which this method was called. * @param useRawArgs Enable/disable raw arguments. */ public useRawArgs( useRawArgs = true, ): Command<void, void, void, Array<string>, void, void, void, CP> { this.cmd._useRawArgs = useRawArgs; return this as Command<any>; }
/** * Set default command. The default command is executed when the program * was called without any argument and if no action handler is registered. * @param name Name of the default command. */ public default(name: string): this { this.cmd.defaultCommand = name; return this; }
public globalType< H extends TypeOrTypeHandler<unknown>, N extends string = string, >( name: N, handler: H, options?: Omit<ITypeOptions, "global">, ): Command< CPG, CPT, CO, CA, CG, CT, Merge<CGT, TypedType<N, H>>, CP > { return this.type(name, handler, { ...options, global: true }); }
/** * Register custom type. * @param name The name of the type. * @param handler The callback method to parse the type. * @param options Type options. */ public type< H extends TypeOrTypeHandler<unknown>, N extends string = string, >( name: N, handler: H, options?: ITypeOptions, ): Command< CPG, CPT, CO, CA, CG, Merge<CT, TypedType<N, H>>, CGT, CP > { if (this.cmd.types.get(name) && !options?.override) { throw new DuplicateType(name); }
this.cmd.types.set(name, { ...options, name, handler });
if ( handler instanceof Type && (typeof handler.complete !== "undefined" || typeof handler.values !== "undefined") ) { const completeHandler: ICompleteHandler = ( cmd: Command, parent?: Command, ) => handler.complete?.(cmd, parent) || []; this.complete(name, completeHandler, options); }
return this as Command<any>; }
public globalComplete( name: string, complete: ICompleteHandler, options?: Omit<ICompleteOptions, "global">, ): this { return this.complete(name, complete, { ...options, global: true }); }
/** * Register command specific custom type. * @param name The name of the completion. * @param complete The callback method to complete the type. * @param options Complete options. */ public complete( name: string, complete: ICompleteHandler< Partial<CO>, Partial<CA>, CG, CPG, CT, CGT, CPT, any >, options: ICompleteOptions & { global: boolean }, ): this; public complete( name: string, complete: ICompleteHandler<CO, CA, CG, CPG, CT, CGT, CPT, CP>, options?: ICompleteOptions, ): this;
public complete( name: string, complete: | ICompleteHandler<CO, CA, CG, CPG, CT, CGT, CPT, CP> | ICompleteHandler< Partial<CO>, Partial<CA>, CG, CPG, CT, CGT, CPT, any >, options?: ICompleteOptions, ): this { if (this.cmd.completions.has(name) && !options?.override) { throw new DuplicateCompletion(name); }
this.cmd.completions.set(name, { name, complete, ...options, });
return this; }
/** * Throw validation errors instead of calling `Deno.exit()` to handle * validation errors manually. * * A validation error is thrown when the command is wrongly used by the user. * For example: If the user passes some invalid options or arguments to the * command. * * This has no effect for parent commands. Only for the command on which this * method was called and all child commands. * * **Example:** * * ``` * try { * cmd.parse(); * } catch(error) { * if (error instanceof ValidationError) { * cmd.showHelp(); * Deno.exit(1); * } * throw error; * } * ``` * * @see ValidationError */ public throwErrors(): this { this.cmd.throwOnError = true; return this; }
/** * Same as `.throwErrors()` but also prevents calling `Deno.exit` after * printing help or version with the --help and --version option. */ public noExit(): this { this.cmd._shouldExit = false; this.throwErrors(); return this; }
/** * Disable inheriting global commands, options and environment variables from * parent commands. */ public noGlobals(): this { this.cmd._noGlobals = true; return this; }
/** Check whether the command should throw errors or exit. */ protected shouldThrowErrors(): boolean { return this.throwOnError || !!this._parent?.shouldThrowErrors(); }
/** Check whether the command should exit after printing help or version. */ protected shouldExit(): boolean { return this._shouldExit ?? this._parent?.shouldExit() ?? true; }
public globalOption< F extends string, G extends TypedOption< F, CO, Merge<CPT, Merge<CGT, CT>>, undefined extends X ? R : false, D >, MG extends MapValue<G, V, C>, R extends ICommandOption["required"] = undefined, C extends ICommandOption["collect"] = undefined, X extends ICommandOption["conflicts"] = undefined, D = undefined, V = undefined, >( flags: F, desc: string, opts?: | Omit< ICommandGlobalOption< Partial<CO>, CA, MergeOptions<F, CG, G>, CPG, CT, CGT, CPT, CP >, "value" > & { default?: IDefaultValue<D>; required?: R; collect?: C; value?: IFlagValueHandler<MapTypes<ValueOf<G>>, V>; } | IFlagValueHandler<MapTypes<ValueOf<G>>, V>, ): Command< CPG, CPT, CO, CA, MergeOptions<F, CG, MG>, CT, CGT, CP > { if (typeof opts === "function") { return this.option( flags, desc, { value: opts, global: true } as ICommandOption, ) as Command<any>; } return this.option( flags, desc, { ...opts, global: true } as ICommandOption, ) as Command<any>; }
/** * Enable grouping of options and set the name of the group. * All option which are added after calling the `.group()` method will be * grouped in the help output. If the `.group()` method can be use multiple * times to create more groups. * * @param name The name of the option group. */ public group(name: string): this { this.cmd._groupName = name; return this; }
/** * Add a new option. * @param flags Flags string e.g: -h, --help, --manual <requiredArg:string> [optionalArg:number] [...restArgs:string] * @param desc Flag description. * @param opts Flag options or custom handler for processing flag value. */ public option< F extends string, G extends TypedOption< F, CO, Merge<CPT, Merge<CGT, CT>>, undefined extends X ? R : false, D >, MG extends MapValue<G, V, C>, R extends ICommandOption["required"] = undefined, C extends ICommandOption["collect"] = undefined, X extends ICommandOption["conflicts"] = undefined, D = undefined, V = undefined, >( flags: F, desc: string, opts: | Omit< ICommandOption< Partial<CO>, CA, MergeOptions<F, CG, G>, CPG, CT, CGT, CPT, CP >, "value" > & { global: true; default?: IDefaultValue<D>; required?: R; collect?: C; value?: IFlagValueHandler<MapTypes<ValueOf<G>>, V>; } | IFlagValueHandler<MapTypes<ValueOf<G>>, V>, ): Command< CPG, CPT, CO, CA, MergeOptions<F, CG, MG>, CT, CGT, CP >;
public option< F extends string, O extends TypedOption< F, CO, Merge<CPT, Merge<CGT, CT>>, undefined extends X ? R : false, D >, MO extends MapValue<O, V, C>, R extends ICommandOption["required"] = undefined, C extends ICommandOption["collect"] = undefined, X extends ICommandOption["conflicts"] = undefined, D = undefined, V = undefined, >( flags: F, desc: string, opts?: | Omit< ICommandOption<MergeOptions<F, CO, MO>, CA, CG, CPG, CT, CGT, CPT, CP>, "value" > & { default?: IDefaultValue<D>; required?: R; collect?: C; conflicts?: X; value?: IFlagValueHandler<MapTypes<ValueOf<O>>, V>; } | IFlagValueHandler<MapTypes<ValueOf<O>>, V>, ): Command< CPG, CPT, MergeOptions<F, CO, MO>, CA, CG, CT, CGT, CP >;
public option( flags: string, desc: string, opts?: ICommandOption | IFlagValueHandler, ): Command<any> { if (typeof opts === "function") { return this.option(flags, desc, { value: opts }); }
const result = splitArguments(flags);
const args: IArgument[] = result.typeDefinition ? parseArgumentsDefinition(result.typeDefinition) : [];
const option: IOption = { ...opts, name: "", description: desc, args, flags: result.flags, equalsSign: result.equalsSign, typeDefinition: result.typeDefinition, groupName: this._groupName, };
if (option.separator) { for (const arg of args) { if (arg.list) { arg.separator = option.separator; } } }
for (const part of option.flags) { const arg = part.trim(); const isLong = /^--/.test(arg); const name = isLong ? arg.slice(2) : arg.slice(1);
if (this.cmd.getBaseOption(name, true)) { if (opts?.override) { this.removeOption(name); } else { throw new DuplicateOptionName(name); } }
if (!option.name && isLong) { option.name = name; } else if (!option.aliases) { option.aliases = [name]; } else { option.aliases.push(name); } }
if (option.prepend) { this.cmd.options.unshift(option); } else { this.cmd.options.push(option); }
return this; }
/** * Add new command example. * @param name Name of the example. * @param description The content of the example. */ public example(name: string, description: string): this { if (this.cmd.hasExample(name)) { throw new DuplicateExample(name); }
this.cmd.examples.push({ name, description });
return this; }
public globalEnv< N extends string, G extends TypedEnv<N, P, CO, Merge<CPT, Merge<CGT, CT>>, R>, MG extends MapValue<G, V>, R extends IEnvVarOptions["required"] = undefined, P extends IEnvVarOptions["prefix"] = undefined, V = undefined, >( name: N, description: string, options?: Omit<IGlobalEnvVarOptions, "value"> & { required?: R; prefix?: P; value?: IEnvVarValueHandler<MapTypes<ValueOf<G>>, V>; }, ): Command<CPG, CPT, CO, CA, Merge<CG, MG>, CT, CGT, CP> { return this.env( name, description, { ...options, global: true } as IEnvVarOptions, ) as Command<any>; }
/** * Add new environment variable. * @param name Name of the environment variable. * @param description The description of the environment variable. * @param options Environment variable options. */ public env< N extends string, G extends TypedEnv<N, P, CO, Merge<CPT, Merge<CGT, CT>>, R>, MG extends MapValue<G, V>, R extends IEnvVarOptions["required"] = undefined, P extends IEnvVarOptions["prefix"] = undefined, V = undefined, >( name: N, description: string, options: Omit<IEnvVarOptions, "value"> & { global: true; required?: R; prefix?: P; value?: IEnvVarValueHandler<MapTypes<ValueOf<G>>, V>; }, ): Command<CPG, CPT, CO, CA, Merge<CG, MG>, CT, CGT, CP>;
public env< N extends string, O extends TypedEnv<N, P, CO, Merge<CPT, Merge<CGT, CT>>, R>, MO extends MapValue<O, V>, R extends IEnvVarOptions["required"] = undefined, P extends IEnvVarOptions["prefix"] = undefined, V = undefined, >( name: N, description: string, options?: Omit<IEnvVarOptions, "value"> & { required?: R; prefix?: P; value?: IEnvVarValueHandler<MapTypes<ValueOf<O>>, V>; }, ): Command<CPG, CPT, Merge<CO, MO>, CA, CG, CT, CGT, CP>;
public env( name: string, description: string, options?: IEnvVarOptions, ): Command<any> { const result = splitArguments(name);
if (!result.typeDefinition) { result.typeDefinition = "<value:boolean>"; }
if (result.flags.some((envName) => this.cmd.getBaseEnvVar(envName, true))) { throw new DuplicateEnvironmentVariable(name); }
const details: IArgument[] = parseArgumentsDefinition( result.typeDefinition, );
if (details.length > 1) { throw new EnvironmentVariableSingleValue(name); } else if (details.length && details[0].optionalValue) { throw new EnvironmentVariableOptionalValue(name); } else if (details.length && details[0].variadic) { throw new EnvironmentVariableVariadicValue(name); }
this.cmd.envVars.push({ name: result.flags[0], names: result.flags, description, type: details[0].type, details: details.shift() as IArgument, ...options, });
return this; }
/***************************************************************************** **** MAIN HANDLER *********************************************************** *****************************************************************************/
/** * Parse command line arguments and execute matched command. * @param args Command line args to parse. Ex: `cmd.parse( Deno.args )` */ public parse( args: string[] = Deno.args, ): Promise< CP extends Command<any> ? IParseResult< Record<string, unknown>, Array<unknown>, Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, undefined > : IParseResult< MapTypes<CO>, MapTypes<CA>, MapTypes<CG>, MapTypes<CPG>, CT, CGT, CPT, CP > > { return this.parseCommand({ args }) as any; }
private async parseCommand(ctx: ParseContext): Promise<IParseResult> { try { this.reset(); this.registerDefaults(); this.rawArgs = ctx.args;
if (this.isExecutable) { await this.executeExecutable(ctx.args); return { options: {}, args: [], cmd: this, literal: [] } as any; }
if (this._useRawArgs) { const env = await this.parseEnvVars(this.envVars); return this.execute(env, ...ctx.args) as any; }
let subCommand: Command<any> | undefined;
// Parse sub command. if (subCommand || ctx.args.length > 0) { if (!subCommand) { subCommand = this.getCommand(ctx.args[0], true);
if (subCommand) { ctx.args = ctx.args.slice(1); } }
if (subCommand) { subCommand._globalParent = this;
return subCommand.parseCommand(ctx); } }
// Parse rest options & env vars. ctx = await this.parseOptionsAndEnvVars(ctx);
this.literalArgs = ctx.literal ?? [];
// Merge env and global options. const options = { ...ctx.env, ...ctx.options };
// Parse arguments. const params = this.parseArguments(ctx.args, options);
// Execute option action. if (ctx.action) { await ctx.action.action.call(this, options, ...params);
if (ctx.action.standalone) { return { options, args: params, cmd: this, literal: this.literalArgs, } as any; } }
return this.execute(options, ...params) as any; } catch (error: unknown) { this.throw( error instanceof FlagsValidationError ? new ValidationError(error.message) : error instanceof Error ? error : new Error(`[non-error-thrown] ${error}`), ); } }
private async parseGlobalOptionsAndEnvVars( ctx: ParseContext, ): Promise<ParseContext> { // Parse global env vars. const envVars = [ ...this.envVars.filter((envVar) => envVar.global), ...this.getGlobalEnvVars(true), ];
const isHelpOption = this.getHelpOption()?.flags.includes(ctx.args[0]); const env = await this.parseEnvVars(envVars, !isHelpOption);
// Parse global options. const options = [ ...this.options.filter((option) => option.global), ...this.getGlobalOptions(true), ];
return this.parseOptions(ctx, options, env, true); }
private async parseOptionsAndEnvVars( ctx: ParseContext, ): Promise<ParseContext> { // Parse env vars. const envVars = this.getEnvVars(true);
const helpOption = this.getHelpOption(); const isVersionOption = this._versionOption?.flags.includes(ctx.args[0]); const isHelpOption = helpOption?.flags.includes(ctx.args[0]);
const env = { ...ctx.env, ...await this.parseEnvVars(envVars, !isHelpOption && !isVersionOption), };
// Parse options. const options = this.getOptions(true);
return this.parseOptions(ctx, options, env); }
/** Register default options like `--version` and `--help`. */ private registerDefaults(): this { if (this.hasDefaults || this.getParent()) { return this; } this.hasDefaults = true;
this.reset();
!this.types.has("string") && this.type("string", new StringType(), { global: true }); !this.types.has("number") && this.type("number", new NumberType(), { global: true }); !this.types.has("integer") && this.type("integer", new IntegerType(), { global: true }); !this.types.has("boolean") && this.type("boolean", new BooleanType(), { global: true }); !this.types.has("file") && this.type("file", new FileType(), { global: true });
if (!this._help) { this.help({ hints: true, types: false, }); }
if (this._versionOptions !== false && (this._versionOptions || this.ver)) { this.option( this._versionOptions?.flags || "-V, --version", this._versionOptions?.desc || "Show the version number for this program.", { standalone: true, prepend: true, action: async function () { const long = this.getRawArgs().includes( `--${this._versionOption?.name}`, ); if (long) { await this.checkVersion(); this.showLongVersion(); } else { this.showVersion(); } this.exit(); }, ...(this._versionOptions?.opts ?? {}), }, ); this._versionOption = this.options[0]; }
if (this._helpOptions !== false) { this.option( this._helpOptions?.flags || "-h, --help", this._helpOptions?.desc || "Show this help.", { standalone: true, global: true, prepend: true, action: async function () { const long = this.getRawArgs().includes( `--${this.getHelpOption()?.name}`, ); await this.checkVersion(); this.showHelp({ long }); this.exit(); }, ...(this._helpOptions?.opts ?? {}), }, ); this._helpOption = this.options[0]; }
return this; }
/** * Execute command. * @param options A map of options. * @param args Command arguments. */ protected async execute( options: Record<string, unknown>, ...args: Array<unknown> ): Promise<IParseResult> { if (this.fn) { await this.fn(options, ...args); } else if (this.defaultCommand) { const cmd = this.getCommand(this.defaultCommand, true);
if (!cmd) { throw new DefaultCommandNotFound( this.defaultCommand, this.getCommands(), ); }
cmd._globalParent = this; await cmd.execute(options, ...args); }
return { options, args, cmd: this, literal: this.literalArgs, }; }
/** * Execute external sub-command. * @param args Raw command line arguments. */ protected async executeExecutable(args: string[]) { const command = this.getPath().replace(/\s+/g, "-");
await Deno.permissions.request({ name: "run", command });
try { const process: Deno.Process = Deno.run({ cmd: [command, ...args], }); const status: Deno.ProcessStatus = await process.status();
if (!status.success) { Deno.exit(status.code); } } catch (error) { if (error instanceof Deno.errors.NotFound) { throw new CommandExecutableNotFound(command); } throw error; } }
/** * Parse raw command line arguments. * @param args Raw command line arguments. */ protected parseOptions( ctx: ParseContext, options: IOption[], env: Record<string, unknown>, stopEarly: boolean = this._stopEarly, ): ParseContext { let action: ActionOption | undefined;
const parseResult = parseFlags(ctx.args, { stopEarly, allowEmpty: this._allowEmpty, flags: options, ignoreDefaults: env, parse: (type: ITypeInfo) => this.parseType(type), option: (option: IOption) => { if (!action && option.action) { action = option as ActionOption; } }, });
// Merge context. return { args: parseResult.unknown, options: { ...ctx.options, ...parseResult.flags }, env: { ...ctx.env, ...env }, action: ctx.action ?? action, literal: parseResult.literal, }; }
/** Parse argument type. */ protected parseType(type: ITypeInfo): unknown { const typeSettings: IType | undefined = this.getType(type.type);
if (!typeSettings) { throw new UnknownType( type.type, this.getTypes().map((type) => type.name), ); }
return typeSettings.handler instanceof Type ? typeSettings.handler.parse(type) : typeSettings.handler(type); }
/** * Read and validate environment variables. * @param envVars env vars defined by the command * @param validate when true, throws an error if a required env var is missing */ protected async parseEnvVars( envVars: Array<IEnvVar>, validate = true, ): Promise<Record<string, unknown>> { const result: Record<string, unknown> = {};
for (const env of envVars) { const found = await this.findEnvVar(env.names);
if (found) { const { name, value } = found;
const propertyName = underscoreToCamelCase( env.prefix ? env.names[0].replace(new RegExp(`^${env.prefix}`), "") : env.names[0], );
if (env.details.list) { const values = value.split(env.details.separator ?? ",");
result[propertyName] = values.map((value) => this.parseType({ label: "Environment variable", type: env.type, name, value, }) ); } else { result[propertyName] = this.parseType({ label: "Environment variable", type: env.type, name, value, }); }
if (env.value && typeof result[propertyName] !== "undefined") { result[propertyName] = env.value(result[propertyName]); } } else if (env.required && validate) { throw new MissingRequiredEnvVar(env); } }
return result; }
protected async findEnvVar( names: readonly string[], ): Promise<{ name: string; value: string } | undefined> { for (const name of names) { const status = await Deno.permissions.query({ name: "env", variable: name, });
if (status.state === "granted") { const value = Deno.env.get(name);
if (value) { return { name, value }; } } }
return undefined; }
/** * Parse command-line arguments. * @param args Raw command line arguments. * @param options Parsed command line options. */ protected parseArguments( args: string[], options: Record<string, unknown>, ): CA { const params: Array<unknown> = [];
// remove array reference args = args.slice(0);
if (!this.hasArguments()) { if (args.length) { if (this.hasCommands(true)) { throw new UnknownCommand(args[0], this.getCommands()); } else { throw new NoArgumentsAllowed(this.getPath()); } } } else { if (!args.length) { const required = this.getArguments() .filter((expectedArg) => !expectedArg.optionalValue) .map((expectedArg) => expectedArg.name);
if (required.length) { const optionNames: string[] = Object.keys(options); const hasStandaloneOption = !!optionNames.find((name) => this.getOption(name, true)?.standalone );
if (!hasStandaloneOption) { throw new MissingArguments(required); } } } else { for (const expectedArg of this.getArguments()) { if (!args.length) { if (expectedArg.optionalValue) { break; } throw new MissingArgument(`Missing argument: ${expectedArg.name}`); }
let arg: unknown;
const parseArgValue = (value: string) => { return expectedArg.list ? value.split(",").map((value) => parseArgType(value)) : parseArgType(value); };
const parseArgType = (value: string) => { return this.parseType({ label: "Argument", type: expectedArg.type, name: expectedArg.name, value, }); };
if (expectedArg.variadic) { arg = args.splice(0, args.length).map((value) => parseArgValue(value) ); } else { arg = parseArgValue(args.shift() as string); }
if (expectedArg.variadic && Array.isArray(arg)) { params.push(...arg); } else if (typeof arg !== "undefined") { params.push(arg); } }
if (args.length) { throw new TooManyArguments(args); } } }
return params as CA; }
/** * Handle error. If `throwErrors` is enabled the error will be returned, * otherwise a formatted error message will be printed and `Deno.exit(1)` * will be called. * @param error Error to handle. */ protected throw(error: Error): never { if (this.shouldThrowErrors() || !(error instanceof ValidationError)) { throw error; } this.showHelp();
console.error(red(` ${bold("error")}: ${error.message}\n`));
Deno.exit(error instanceof ValidationError ? error.exitCode : 1); }
/***************************************************************************** **** GETTER ***************************************************************** *****************************************************************************/
/** Get command name. */ public getName(): string { return this._name; }
/** Get parent command. */ public getParent(): CP { return this._parent as CP; }
/** * Get parent command from global executed command. * Be sure, to call this method only inside an action handler. Unless this or any child command was executed, * this method returns always undefined. */ public getGlobalParent(): Command<any> | undefined { return this._globalParent; }
/** Get main command. */ public getMainCommand(): Command<any> { return this._parent?.getMainCommand() ?? this; }
/** Get command name aliases. */ public getAliases(): string[] { return this.aliases; }
/** Get full command path. */ public getPath(): string { return this._parent ? this._parent.getPath() + " " + this._name : this._name; }
/** Get arguments definition. E.g: <input-file:string> <output-file:string> */ public getArgsDefinition(): string | undefined { return this.argsDefinition; }
/** * Get argument by name. * @param name Name of the argument. */ public getArgument(name: string): IArgument | undefined { return this.getArguments().find((arg) => arg.name === name); }
/** Get arguments. */ public getArguments(): IArgument[] { if (!this.args.length && this.argsDefinition) { this.args = parseArgumentsDefinition(this.argsDefinition); }
return this.args; }
/** Check if command has arguments. */ public hasArguments() { return !!this.argsDefinition; }
/** Get command version. */ public getVersion(): string | undefined { return this.getVersionHandler()?.call(this, this); }
/** Get help handler method. */ private getVersionHandler(): IVersionHandler | undefined { return this.ver ?? this._parent?.getVersionHandler(); }
/** Get command description. */ public getDescription(): string { // call description method only once return typeof this.desc === "function" ? this.desc = this.desc() : this.desc; }
public getUsage() { return this._usage ?? this.getArgsDefinition(); }
/** Get short command description. This is the first line of the description. */ public getShortDescription(): string { return getDescription(this.getDescription(), true); }
/** Get original command-line arguments. */ public getRawArgs(): string[] { return this.rawArgs; }
/** Get all arguments defined after the double dash. */ public getLiteralArgs(): string[] { return this.literalArgs; }
/** Output generated help without exiting. */ public showVersion(): void { console.log(this.getVersion()); }
/** Returns command name, version and meta data. */ public getLongVersion(): string { return `${bold(this.getMainCommand().getName())} ${ blue(this.getVersion() ?? "") }` + Object.entries(this.getMeta()).map( ([k, v]) => `\n${bold(k)} ${blue(v)}`, ).join(""); }
/** Outputs command name, version and meta data. */ public showLongVersion(): void { console.log(this.getLongVersion()); }
/** Output generated help without exiting. */ public showHelp(options?: HelpOptions): void { console.log(this.getHelp(options)); }
/** Get generated help. */ public getHelp(options?: HelpOptions): string { this.registerDefaults(); return this.getHelpHandler().call(this, this, options ?? {}); }
/** Get help handler method. */ private getHelpHandler(): IHelpHandler { return this._help ?? this._parent?.getHelpHandler() as IHelpHandler; }
private exit(code = 0) { if (this.shouldExit()) { Deno.exit(code); } }
/** Check if new version is available and add hint to version. */ public async checkVersion(): Promise<void> { const mainCommand = this.getMainCommand(); const upgradeCommand = mainCommand.getCommand("upgrade");
if (!isUpgradeCommand(upgradeCommand)) { return; } const latestVersion = await upgradeCommand.getLatestVersion(); const currentVersion = mainCommand.getVersion();
if (currentVersion === latestVersion) { return; } const versionHelpText = `(New version available: ${latestVersion}. Run '${mainCommand.getName()} upgrade' to upgrade to the latest version!)`;
mainCommand.version(`${currentVersion} ${bold(yellow(versionHelpText))}`); }
/***************************************************************************** **** Options GETTER ********************************************************* *****************************************************************************/
/** * Checks whether the command has options or not. * @param hidden Include hidden options. */ public hasOptions(hidden?: boolean): boolean { return this.getOptions(hidden).length > 0; }
/** * Get options. * @param hidden Include hidden options. */ public getOptions(hidden?: boolean): IOption[] { return this.getGlobalOptions(hidden).concat(this.getBaseOptions(hidden)); }
/** * Get base options. * @param hidden Include hidden options. */ public getBaseOptions(hidden?: boolean): IOption[] { if (!this.options.length) { return []; }
return hidden ? this.options.slice(0) : this.options.filter((opt) => !opt.hidden); }
/** * Get global options. * @param hidden Include hidden options. */ public getGlobalOptions(hidden?: boolean): IOption[] { const helpOption = this.getHelpOption(); const getGlobals = ( cmd: Command<any>, noGlobals: boolean, options: IOption[] = [], names: string[] = [], ): IOption[] => { if (cmd.options.length) { for (const option of cmd.options) { if ( option.global && !this.options.find((opt) => opt.name === option.name) && names.indexOf(option.name) === -1 && (hidden || !option.hidden) ) { if (noGlobals && option !== helpOption) { continue; }
names.push(option.name); options.push(option); } } }
return cmd._parent ? getGlobals( cmd._parent, noGlobals || cmd._noGlobals, options, names, ) : options; };
return this._parent ? getGlobals(this._parent, this._noGlobals) : []; }
/** * Checks whether the command has an option with given name or not. * @param name Name of the option. Must be in param-case. * @param hidden Include hidden options. */ public hasOption(name: string, hidden?: boolean): boolean { return !!this.getOption(name, hidden); }
/** * Get option by name. * @param name Name of the option. Must be in param-case. * @param hidden Include hidden options. */ public getOption(name: string, hidden?: boolean): IOption | undefined { return this.getBaseOption(name, hidden) ?? this.getGlobalOption(name, hidden); }
/** * Get base option by name. * @param name Name of the option. Must be in param-case. * @param hidden Include hidden options. */ public getBaseOption(name: string, hidden?: boolean): IOption | undefined { const option = this.options.find((option) => option.name === name || option.aliases?.includes(name) );
return option && (hidden || !option.hidden) ? option : undefined; }
/** * Get global option from parent commands by name. * @param name Name of the option. Must be in param-case. * @param hidden Include hidden options. */ public getGlobalOption(name: string, hidden?: boolean): IOption | undefined { const helpOption = this.getHelpOption(); const getGlobalOption = ( parent: Command, noGlobals: boolean, ): IOption | undefined => { const option: IOption | undefined = parent.getBaseOption( name, hidden, );
if (!option?.global) { return parent._parent && getGlobalOption( parent._parent, noGlobals || parent._noGlobals, ); } if (noGlobals && option !== helpOption) { return; }
return option; };
return this._parent && getGlobalOption( this._parent, this._noGlobals, ); }
/** * Remove option by name. * @param name Name of the option. Must be in param-case. */ public removeOption(name: string): IOption | undefined { const index = this.options.findIndex((option) => option.name === name);
if (index === -1) { return; }
return this.options.splice(index, 1)[0]; }
/** * Checks whether the command has sub-commands or not. * @param hidden Include hidden commands. */ public hasCommands(hidden?: boolean): boolean { return this.getCommands(hidden).length > 0; }
/** * Get commands. * @param hidden Include hidden commands. */ public getCommands(hidden?: boolean): Array<Command<any>> { return this.getGlobalCommands(hidden).concat(this.getBaseCommands(hidden)); }
/** * Get base commands. * @param hidden Include hidden commands. */ public getBaseCommands(hidden?: boolean): Array<Command<any>> { const commands = Array.from(this.commands.values()); return hidden ? commands : commands.filter((cmd) => !cmd.isHidden); }
/** * Get global commands. * @param hidden Include hidden commands. */ public getGlobalCommands(hidden?: boolean): Array<Command<any>> { const getCommands = ( command: Command<any>, noGlobals: boolean, commands: Array<Command<any>> = [], names: string[] = [], ): Array<Command<any>> => { if (command.commands.size) { for (const [_, cmd] of command.commands) { if ( cmd.isGlobal && this !== cmd && !this.commands.has(cmd._name) && names.indexOf(cmd._name) === -1 && (hidden || !cmd.isHidden) ) { if (noGlobals && cmd?.getName() !== "help") { continue; }
names.push(cmd._name); commands.push(cmd); } } }
return command._parent ? getCommands( command._parent, noGlobals || command._noGlobals, commands, names, ) : commands; };
return this._parent ? getCommands(this._parent, this._noGlobals) : []; }
/** * Checks whether a child command exists by given name or alias. * @param name Name or alias of the command. * @param hidden Include hidden commands. */ public hasCommand(name: string, hidden?: boolean): boolean { return !!this.getCommand(name, hidden); }
/** * Get command by name or alias. * @param name Name or alias of the command. * @param hidden Include hidden commands. */ public getCommand<C extends Command<any>>( name: string, hidden?: boolean, ): C | undefined { return this.getBaseCommand(name, hidden) ?? this.getGlobalCommand(name, hidden); }
/** * Get base command by name or alias. * @param name Name or alias of the command. * @param hidden Include hidden commands. */ public getBaseCommand<C extends Command<any>>( name: string, hidden?: boolean, ): C | undefined { for (const cmd of this.commands.values()) { if (cmd._name === name || cmd.aliases.includes(name)) { return (cmd && (hidden || !cmd.isHidden) ? cmd : undefined) as | C | undefined; } } }
/** * Get global command by name or alias. * @param name Name or alias of the command. * @param hidden Include hidden commands. */ public getGlobalCommand<C extends Command<any>>( name: string, hidden?: boolean, ): C | undefined { const getGlobalCommand = ( parent: Command, noGlobals: boolean, ): Command | undefined => { const cmd: Command | undefined = parent.getBaseCommand(name, hidden);
if (!cmd?.isGlobal) { return parent._parent && getGlobalCommand(parent._parent, noGlobals || parent._noGlobals); } if (noGlobals && cmd.getName() !== "help") { return; }
return cmd; };
return this._parent && getGlobalCommand(this._parent, this._noGlobals) as C; }
/** * Remove sub-command by name or alias. * @param name Name or alias of the command. */ public removeCommand(name: string): Command<any> | undefined { const command = this.getBaseCommand(name, true);
if (command) { this.commands.delete(command._name); }
return command; }
/** Get types. */ public getTypes(): IType[] { return this.getGlobalTypes().concat(this.getBaseTypes()); }
/** Get base types. */ public getBaseTypes(): IType[] { return Array.from(this.types.values()); }
/** Get global types. */ public getGlobalTypes(): IType[] { const getTypes = ( cmd: Command<any> | undefined, types: IType[] = [], names: string[] = [], ): IType[] => { if (cmd) { if (cmd.types.size) { cmd.types.forEach((type: IType) => { if ( type.global && !this.types.has(type.name) && names.indexOf(type.name) === -1 ) { names.push(type.name); types.push(type); } }); }
return getTypes(cmd._parent, types, names); }
return types; };
return getTypes(this._parent); }
/** * Get type by name. * @param name Name of the type. */ public getType(name: string): IType | undefined { return this.getBaseType(name) ?? this.getGlobalType(name); }
/** * Get base type by name. * @param name Name of the type. */ public getBaseType(name: string): IType | undefined { return this.types.get(name); }
/** * Get global type by name. * @param name Name of the type. */ public getGlobalType(name: string): IType | undefined { if (!this._parent) { return; }
const cmd: IType | undefined = this._parent.getBaseType(name);
if (!cmd?.global) { return this._parent.getGlobalType(name); }
return cmd; }
/** Get completions. */ public getCompletions() { return this.getGlobalCompletions().concat(this.getBaseCompletions()); }
/** Get base completions. */ public getBaseCompletions(): ICompletion[] { return Array.from(this.completions.values()); }
/** Get global completions. */ public getGlobalCompletions(): ICompletion[] { const getCompletions = ( cmd: Command<any> | undefined, completions: ICompletion[] = [], names: string[] = [], ): ICompletion[] => { if (cmd) { if (cmd.completions.size) { cmd.completions.forEach((completion: ICompletion) => { if ( completion.global && !this.completions.has(completion.name) && names.indexOf(completion.name) === -1 ) { names.push(completion.name); completions.push(completion); } }); }
return getCompletions(cmd._parent, completions, names); }
return completions; };
return getCompletions(this._parent); }
/** * Get completion by name. * @param name Name of the completion. */ public getCompletion(name: string): ICompletion | undefined { return this.getBaseCompletion(name) ?? this.getGlobalCompletion(name); }
/** * Get base completion by name. * @param name Name of the completion. */ public getBaseCompletion(name: string): ICompletion | undefined { return this.completions.get(name); }
/** * Get global completions by name. * @param name Name of the completion. */ public getGlobalCompletion(name: string): ICompletion | undefined { if (!this._parent) { return; }
const completion: ICompletion | undefined = this._parent.getBaseCompletion( name, );
if (!completion?.global) { return this._parent.getGlobalCompletion(name); }
return completion; }
/** * Checks whether the command has environment variables or not. * @param hidden Include hidden environment variable. */ public hasEnvVars(hidden?: boolean): boolean { return this.getEnvVars(hidden).length > 0; }
/** * Get environment variables. * @param hidden Include hidden environment variable. */ public getEnvVars(hidden?: boolean): IEnvVar[] { return this.getGlobalEnvVars(hidden).concat(this.getBaseEnvVars(hidden)); }
/** * Get base environment variables. * @param hidden Include hidden environment variable. */ public getBaseEnvVars(hidden?: boolean): IEnvVar[] { if (!this.envVars.length) { return []; }
return hidden ? this.envVars.slice(0) : this.envVars.filter((env) => !env.hidden); }
/** * Get global environment variables. * @param hidden Include hidden environment variable. */ public getGlobalEnvVars(hidden?: boolean): IEnvVar[] { if (this._noGlobals) { return []; }
const getEnvVars = ( cmd: Command<any> | undefined, envVars: IEnvVar[] = [], names: string[] = [], ): IEnvVar[] => { if (cmd) { if (cmd.envVars.length) { cmd.envVars.forEach((envVar: IEnvVar) => { if ( envVar.global && !this.envVars.find((env) => env.names[0] === envVar.names[0]) && names.indexOf(envVar.names[0]) === -1 && (hidden || !envVar.hidden) ) { names.push(envVar.names[0]); envVars.push(envVar); } }); }
return getEnvVars(cmd._parent, envVars, names); }
return envVars; };
return getEnvVars(this._parent); }
/** * Checks whether the command has an environment variable with given name or not. * @param name Name of the environment variable. * @param hidden Include hidden environment variable. */ public hasEnvVar(name: string, hidden?: boolean): boolean { return !!this.getEnvVar(name, hidden); }
/** * Get environment variable by name. * @param name Name of the environment variable. * @param hidden Include hidden environment variable. */ public getEnvVar(name: string, hidden?: boolean): IEnvVar | undefined { return this.getBaseEnvVar(name, hidden) ?? this.getGlobalEnvVar(name, hidden); }
/** * Get base environment variable by name. * @param name Name of the environment variable. * @param hidden Include hidden environment variable. */ public getBaseEnvVar(name: string, hidden?: boolean): IEnvVar | undefined { const envVar: IEnvVar | undefined = this.envVars.find((env) => env.names.indexOf(name) !== -1 );
return envVar && (hidden || !envVar.hidden) ? envVar : undefined; }
/** * Get global environment variable by name. * @param name Name of the environment variable. * @param hidden Include hidden environment variable. */ public getGlobalEnvVar(name: string, hidden?: boolean): IEnvVar | undefined { if (!this._parent || this._noGlobals) { return; }
const envVar: IEnvVar | undefined = this._parent.getBaseEnvVar( name, hidden, );
if (!envVar?.global) { return this._parent.getGlobalEnvVar(name, hidden); }
return envVar; }
/** Checks whether the command has examples or not. */ public hasExamples(): boolean { return this.examples.length > 0; }
/** Get all examples. */ public getExamples(): IExample[] { return this.examples; }
/** Checks whether the command has an example with given name or not. */ public hasExample(name: string): boolean { return !!this.getExample(name); }
/** Get example with given name. */ public getExample(name: string): IExample | undefined { return this.examples.find((example) => example.name === name); }
private getHelpOption(): IOption | undefined { return this._helpOption ?? this._parent?.getHelpOption(); }}
function isUpgradeCommand(command: unknown): command is UpgradeCommandImpl { return command instanceof Command && "getLatestVersion" in command;}
interface UpgradeCommandImpl { getLatestVersion(): Promise<string>;}
interface IDefaultOption { flags: string; desc?: string; opts?: ICommandOption;}
type ActionOption = IOption & { action: IAction };
interface ParseContext { args: string[]; options?: Record<string, unknown>; env?: Record<string, unknown>; action?: ActionOption; literal?: string[];}
type TrimLeft<T extends string, V extends string | undefined> = T extends `${V}${infer U}` ? U : T;
type TrimRight<T extends string, V extends string> = T extends `${infer U}${V}` ? U : T;
type Lower<V extends string> = V extends Uppercase<V> ? Lowercase<V> : Uncapitalize<V>;
type CamelCase<T extends string> = T extends `${infer V}_${infer Rest}` ? `${Lower<V>}${Capitalize<CamelCase<Rest>>}` : T extends `${infer V}-${infer Rest}` ? `${Lower<V>}${Capitalize<CamelCase<Rest>>}` : Lower<T>;
type OneOf<T, V> = T extends void ? V : T;
type Merge<L, R> = L extends void ? R : R extends void ? L : L & R;
// type Merge<L, R> = L extends void ? R// : R extends void ? L// : Omit<L, keyof R> & R;
type MergeRecursive<L, R> = L extends void ? R : R extends void ? L : L & R;
type OptionalOrRequiredValue<T extends string> = `[${T}]` | `<${T}>`;type RestValue = `...${string}` | `${string}...`;
/** * Rest args with list type and completions. * * - `[...name:type[]:completion]` * - `<...name:type[]:completion>` * - `[name...:type[]:completion]` * - `<name...:type[]:completion>` */type RestArgsListTypeCompletion<T extends string> = OptionalOrRequiredValue< `${RestValue}:${T}[]:${string}`>;
/** * Rest args with list type. * * - `[...name:type[]]` * - `<...name:type[]>` * - `[name...:type[]]` * - `<name...:type[]>` */type RestArgsListType<T extends string> = OptionalOrRequiredValue< `${RestValue}:${T}[]`>;
/** * Rest args with type and completions. * * - `[...name:type:completion]` * - `<...name:type:completion>` * - `[name...:type:completion]` * - `<name...:type:completion>` */type RestArgsTypeCompletion<T extends string> = OptionalOrRequiredValue< `${RestValue}:${T}:${string}`>;
/** * Rest args with type. * * - `[...name:type]` * - `<...name:type>` * - `[name...:type]` * - `<name...:type>` */type RestArgsType<T extends string> = OptionalOrRequiredValue< `${RestValue}:${T}`>;
/** * Rest args. * - `[...name]` * - `<...name>` * - `[name...]` * - `<name...>` */type RestArgs = OptionalOrRequiredValue< `${RestValue}`>;
/** * Single arg with list type and completions. * * - `[name:type[]:completion]` * - `<name:type[]:completion>` */type SingleArgListTypeCompletion<T extends string> = OptionalOrRequiredValue< `${string}:${T}[]:${string}`>;
/** * Single arg with list type. * * - `[name:type[]]` * - `<name:type[]>` */type SingleArgListType<T extends string> = OptionalOrRequiredValue< `${string}:${T}[]`>;
/** * Single arg with type and completion. * * - `[name:type:completion]` * - `<name:type:completion>` */type SingleArgTypeCompletion<T extends string> = OptionalOrRequiredValue< `${string}:${T}:${string}`>;
/** * Single arg with type. * * - `[name:type]` * - `<name:type>` */type SingleArgType<T extends string> = OptionalOrRequiredValue< `${string}:${T}`>;
/** * Single arg. * * - `[name]` * - `<name>` */type SingleArg = OptionalOrRequiredValue< `${string}`>;
type DefaultTypes = { number: NumberType; integer: IntegerType; string: StringType; boolean: BooleanType; file: FileType;};
type ArgumentType<A extends string, U, T = Merge<DefaultTypes, U>> = A extends RestArgsListTypeCompletion<infer Type> ? T extends Record<Type, infer R> ? Array<Array<R>> : unknown : A extends RestArgsListType<infer Type> ? T extends Record<Type, infer R> ? Array<Array<R>> : unknown : A extends RestArgsTypeCompletion<infer Type> ? T extends Record<Type, infer R> ? Array<R> : unknown : A extends RestArgsType<infer Type> ? T extends Record<Type, infer R> ? Array<R> : unknown : A extends RestArgs ? Array<string> : A extends SingleArgListTypeCompletion<infer Type> ? T extends Record<Type, infer R> ? Array<R> : unknown : A extends SingleArgListType<infer Type> ? T extends Record<Type, infer R> ? Array<R> : unknown : A extends SingleArgTypeCompletion<infer Type> ? T extends Record<Type, infer R> ? R : unknown : A extends SingleArgType<infer Type> ? T extends Record<Type, infer R> ? R : unknown : A extends SingleArg ? string : unknown;
type ArgumentTypes<A extends string, T> = A extends `${string} ${string}` ? TypedArguments<A, T> : ArgumentType<A, T>;
type GetArguments<A extends string> = A extends `-${string}=${infer Rest}` ? GetArguments<Rest> : A extends `-${string} ${infer Rest}` ? GetArguments<Rest> : A;
type OptionName<Name extends string> = Name extends "*" ? string : CamelCase<TrimRight<Name, ",">>;
type IsRequired<R extends boolean | undefined, D> = R extends true ? true : D extends undefined ? false : true;
type NegatableOption< F extends string, CO, D, N extends string = OptionName<F>,> = D extends undefined ? N extends keyof CO ? { [K in N]?: false } : { [K in N]: boolean } : { [K in N]: NonNullable<D> | false };
type BooleanOption< N extends string, CO, R extends boolean | undefined = undefined, D = undefined,> = N extends `no-${infer Name}` ? NegatableOption<Name, CO, D> : N extends `${infer Name}.${infer Rest}` ? (R extends true ? { [K in OptionName<Name>]: BooleanOption<Rest, CO, R, D> } : { [K in OptionName<Name>]?: BooleanOption<Rest, CO, R, D> }) : (R extends true ? { [K in OptionName<N>]: true | D } : { [K in OptionName<N>]?: true | D });
type ValueOption< N extends string, F extends string, V, R extends boolean | undefined = undefined, D = undefined,> = N extends `${infer Name}.${infer RestName}` ? (R extends true ? { [K in OptionName<Name>]: ValueOption<RestName, F, V, R, D>; } : { [K in OptionName<Name>]?: ValueOption<RestName, F, V, R, D>; }) : (R extends true ? { [K in OptionName<N>]: GetArguments<F> extends `[${string}]` ? NonNullable<D> | true | ArgumentType<GetArguments<F>, V> : NonNullable<D> | ArgumentType<GetArguments<F>, V>; } : { [K in OptionName<N>]?: GetArguments<F> extends `[${string}]` ? NonNullable<D> | true | ArgumentType<GetArguments<F>, V> : NonNullable<D> | ArgumentType<GetArguments<F>, V>; });
type ValuesOption< T extends string, Rest extends string, V, R extends boolean | undefined = undefined, D = undefined,> = T extends `${infer Name}.${infer RestName}` ? (R extends true ? { [N in OptionName<Name>]: ValuesOption<RestName, Rest, V, R, D>; } : { [N in OptionName<Name>]?: ValuesOption<RestName, Rest, V, R, D>; }) : (R extends true ? { [N in OptionName<T>]: GetArguments<Rest> extends `[${string}]` ? NonNullable<D> | true | ArgumentTypes<GetArguments<Rest>, V> : NonNullable<D> | ArgumentTypes<GetArguments<Rest>, V>; } : { [N in OptionName<T>]?: GetArguments<Rest> extends `[${string}]` ? NonNullable<D> | true | ArgumentTypes<GetArguments<Rest>, V> : NonNullable<D> | ArgumentTypes<GetArguments<Rest>, V>; });
type MapValue<O, V, C = undefined> = V extends undefined ? C extends true ? { [K in keyof O]: O[K] extends (Record<string, unknown> | undefined) ? MapValue<O[K], V> : Array<NonNullable<O[K]>>; } : O : { [K in keyof O]: O[K] extends (Record<string, unknown> | undefined) ? MapValue<O[K], V> : V; };
type GetOptionName<T> = T extends `${string}--${infer Name}=${string}` ? TrimRight<Name, ","> : T extends `${string}--${infer Name} ${string}` ? TrimRight<Name, ","> : T extends `${string}--${infer Name}` ? Name : T extends `-${infer Name}=${string}` ? TrimRight<Name, ","> : T extends `-${infer Name} ${string}` ? TrimRight<Name, ","> : T extends `-${infer Name}` ? Name : unknown;
type MergeOptions<T, CO, O, N = GetOptionName<T>> = N extends `no-${string}` ? Spread<CO, O> : N extends `${string}.${string}` ? MergeRecursive<CO, O> : Merge<CO, O>;
// type MergeOptions<T, CO, O, N = GetOptionName<T>> = N extends `no-${string}`// ? Spread<CO, O>// : N extends `${infer Name}.${infer Child}`// ? (OptionName<Name> extends keyof Merge<CO, O>// ? OptionName<Child> extends// keyof NonNullable<Merge<CO, O>[OptionName<Name>]> ? SpreadTwo<CO, O>// : MergeRecursive<CO, O>// : MergeRecursive<CO, O>)// : Merge<CO, O>;
type TypedOption< F extends string, CO, T, R extends boolean | undefined = undefined, D = undefined,> = number extends T ? any : F extends `${string}--${infer Name}=${infer Rest}` ? ValuesOption<Name, Rest, T, IsRequired<R, D>, D> : F extends `${string}--${infer Name} ${infer Rest}` ? ValuesOption<Name, Rest, T, IsRequired<R, D>, D> : F extends `${string}--${infer Name}` ? BooleanOption<Name, CO, IsRequired<R, D>, D> : F extends `-${infer Name}=${infer Rest}` ? ValuesOption<Name, Rest, T, IsRequired<R, D>, D> : F extends `-${infer Name} ${infer Rest}` ? ValuesOption<Name, Rest, T, IsRequired<R, D>, D> : F extends `-${infer Name}` ? BooleanOption<Name, CO, IsRequired<R, D>, D> : Record<string, unknown>;
type TypedArguments<A extends string, T extends Record<string, any> | void> = number extends T ? any : A extends `${infer Arg} ${infer Rest}` ? Arg extends `[${string}]` ? [ArgumentType<Arg, T>?, ...TypedArguments<Rest, T>] : [ArgumentType<Arg, T>, ...TypedArguments<Rest, T>] : A extends `${string}...${string}` ? [ ...ArgumentType<A, T> extends Array<infer U> ? A extends `[${string}]` ? Array<U> : [U, ...Array<U>] : never, ] : A extends `[${string}]` ? [ArgumentType<A, T>?] : [ArgumentType<A, T>];
type TypedCommandArguments<N extends string, T> = number extends T ? any : N extends `${string} ${infer Args}` ? TypedArguments<Args, T> : [];
type TypedEnv< N extends string, P extends string | undefined, CO, T, R extends boolean | undefined = undefined, D = undefined,> = number extends T ? any : N extends `${infer Name}=${infer Rest}` ? ValueOption<TrimLeft<Name, P>, Rest, T, R, D> : N extends `${infer Name} ${infer Rest}` ? ValueOption<TrimLeft<Name, P>, Rest, T, R, D> : N extends `${infer Name}` ? BooleanOption<TrimLeft<Name, P>, CO, R, D> : Record<string, unknown>;
type TypedType< Name extends string, Handler extends TypeOrTypeHandler<unknown>,> = { [N in Name]: Handler };
type RequiredKeys<T> = { // deno-lint-ignore ban-types [K in keyof T]-?: {} extends Pick<T, K> ? never : K;}[keyof T];
type OptionalKeys<T> = { // deno-lint-ignore ban-types [K in keyof T]-?: {} extends Pick<T, K> ? K : never;}[keyof T];
type SpreadRequiredProperties< L, R, K extends keyof L & keyof R,> = { [P in K]: Exclude<L[P], undefined> | Exclude<R[P], undefined>;};
type SpreadOptionalProperties< L, R, K extends keyof L & keyof R,> = { [P in K]?: L[P] | R[P];};
/** Merge types of two objects. */type Spread<L, R> = L extends void ? R : R extends void ? L // Properties in L that don't exist in R.: & Omit<L, keyof R> // Properties in R that don't exist in L. & Omit<R, keyof L> // Required properties in R that exist in L. & SpreadRequiredProperties<L, R, RequiredKeys<R> & keyof L> // Required properties in L that exist in R. & SpreadRequiredProperties<L, R, RequiredKeys<L> & keyof R> // Optional properties in L and R. & SpreadOptionalProperties< L, R, OptionalKeys<L> & OptionalKeys<R> >;
type ValueOf<T> = T extends Record<string, infer V> ? ValueOf<V> : T;