Skip to main content
Module

x/deno/cli/flags.rs

A modern runtime for JavaScript and TypeScript.
Go to Latest
File
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use clap::Arg;use clap::ArgMatches;use clap::ColorChoice;use clap::Command;use clap::ValueHint;use deno_core::serde::Deserialize;use deno_core::serde::Serialize;use deno_core::url::Url;use deno_runtime::permissions::PermissionsOptions;use log::debug;use log::Level;use once_cell::sync::Lazy;use std::env;use std::net::SocketAddr;use std::num::NonZeroU32;use std::num::NonZeroU8;use std::num::NonZeroUsize;use std::path::PathBuf;use std::str::FromStr;
static LONG_VERSION: Lazy<String> = Lazy::new(|| { format!( "{} ({}, {})\nv8 {}\ntypescript {}", crate::version::deno(), if crate::version::is_canary() { "canary" } else { env!("PROFILE") }, env!("TARGET"), deno_core::v8_version(), crate::version::TYPESCRIPT )});
static SHORT_VERSION: Lazy<String> = Lazy::new(|| { crate::version::deno() .split('+') .next() .unwrap() .to_string()});
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct BenchFlags { pub ignore: Vec<PathBuf>, pub include: Option<Vec<String>>, pub filter: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct BundleFlags { pub source_file: String, pub out_file: Option<PathBuf>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct CacheFlags { pub files: Vec<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct CheckFlags { pub files: Vec<String>, pub remote: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct CompileFlags { pub source_file: String, pub output: Option<PathBuf>, pub args: Vec<String>, pub target: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct CompletionsFlags { pub buf: Box<[u8]>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct CoverageFlags { pub files: Vec<PathBuf>, pub output: Option<PathBuf>, pub ignore: Vec<PathBuf>, pub include: Vec<String>, pub exclude: Vec<String>, pub lcov: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct DocFlags { pub private: bool, pub json: bool, pub source_file: Option<String>, pub filter: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct EvalFlags { pub print: bool, pub code: String, pub ext: String,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct FmtFlags { pub check: bool, pub files: Vec<PathBuf>, pub ignore: Vec<PathBuf>, pub ext: String, pub use_tabs: Option<bool>, pub line_width: Option<NonZeroU32>, pub indent_width: Option<NonZeroU8>, pub single_quote: Option<bool>, pub prose_wrap: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct InfoFlags { pub json: bool, pub file: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct InstallFlags { pub module_url: String, pub args: Vec<String>, pub name: Option<String>, pub root: Option<PathBuf>, pub force: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct UninstallFlags { pub name: String, pub root: Option<PathBuf>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct LintFlags { pub files: Vec<PathBuf>, pub ignore: Vec<PathBuf>, pub rules: bool, pub maybe_rules_tags: Option<Vec<String>>, pub maybe_rules_include: Option<Vec<String>>, pub maybe_rules_exclude: Option<Vec<String>>, pub json: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct ReplFlags { pub eval_files: Option<Vec<String>>, pub eval: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct RunFlags { pub script: String,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct TaskFlags { pub task: String,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct TestFlags { pub ignore: Vec<PathBuf>, pub doc: bool, pub no_run: bool, pub fail_fast: Option<NonZeroUsize>, pub allow_none: bool, pub include: Option<Vec<String>>, pub filter: Option<String>, pub shuffle: Option<u64>, pub concurrent_jobs: NonZeroUsize, pub trace_ops: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct UpgradeFlags { pub dry_run: bool, pub force: bool, pub canary: bool, pub version: Option<String>, pub output: Option<PathBuf>, pub ca_file: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct VendorFlags { pub specifiers: Vec<String>, pub output_path: Option<PathBuf>, pub force: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub enum DenoSubcommand { Bench(BenchFlags), Bundle(BundleFlags), Cache(CacheFlags), Check(CheckFlags), Compile(CompileFlags), Completions(CompletionsFlags), Coverage(CoverageFlags), Doc(DocFlags), Eval(EvalFlags), Fmt(FmtFlags), Info(InfoFlags), Install(InstallFlags), Uninstall(UninstallFlags), Lsp, Lint(LintFlags), Repl(ReplFlags), Run(RunFlags), Task(TaskFlags), Test(TestFlags), Types, Upgrade(UpgradeFlags), Vendor(VendorFlags),}
impl Default for DenoSubcommand { fn default() -> DenoSubcommand { DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: None, }) }}
#[derive(Debug, Clone, PartialEq)]pub enum TypeCheckMode { /// Type check all modules. The default value. All, /// Skip type checking of all modules. Represents `--no-check` on the command /// line. None, /// Only type check local modules. Represents `--no-check=remote` on the /// command line. Local,}
impl Default for TypeCheckMode { fn default() -> Self { Self::All }}
// TODO(bartlomieju): remove once type checking is skipped by default (probably// in 1.23)#[derive(Debug, Clone, PartialEq)]pub enum FutureTypeCheckMode { /// Type check all modules. The default value. All, /// Skip type checking of all modules. Represents `--no-check` on the command /// line. None, /// Only type check local modules. Represents `--no-check=remote` on the /// command line. Local,}
impl Default for FutureTypeCheckMode { fn default() -> Self { Self::None }}
#[derive(Clone, Debug, PartialEq, Default)]pub struct Flags { /// Vector of CLI arguments - these are user script arguments, all Deno /// specific flags are removed. pub argv: Vec<String>, pub subcommand: DenoSubcommand, pub allow_all: bool, pub allow_env: Option<Vec<String>>, pub allow_hrtime: bool, pub allow_net: Option<Vec<String>>, pub allow_ffi: Option<Vec<PathBuf>>, pub allow_read: Option<Vec<PathBuf>>, pub allow_run: Option<Vec<String>>, pub allow_write: Option<Vec<PathBuf>>, pub ca_stores: Option<Vec<String>>, pub ca_file: Option<String>, pub cache_blocklist: Vec<String>, /// This is not exposed as an option in the CLI, it is used internally when /// the language server is configured with an explicit cache option. pub cache_path: Option<PathBuf>, pub cached_only: bool, pub type_check_mode: TypeCheckMode, // TODO(bartlomieju): should be removed in favor of `check` // once type checking is skipped by default pub future_type_check_mode: FutureTypeCheckMode, pub has_check_flag: bool, pub config_path: Option<String>, pub coverage_dir: Option<String>, pub enable_testing_features: bool, pub ignore: Vec<PathBuf>, pub import_map_path: Option<String>, pub inspect_brk: Option<SocketAddr>, pub inspect: Option<SocketAddr>, pub location: Option<Url>, pub lock_write: bool, pub lock: Option<PathBuf>, pub log_level: Option<Level>, pub no_remote: bool, /// If true, a list of Node built-in modules will be injected into /// the import map. pub compat: bool, pub no_prompt: bool, pub reload: bool, pub repl: bool, pub seed: Option<u64>, pub unstable: bool, pub unsafely_ignore_certificate_errors: Option<Vec<String>>, pub v8_flags: Vec<String>, pub version: bool, pub watch: Option<Vec<PathBuf>>, pub no_clear_screen: bool,}
fn join_paths(allowlist: &[PathBuf], d: &str) -> String { allowlist .iter() .map(|path| path.to_str().unwrap().to_string()) .collect::<Vec<String>>() .join(d)}
impl Flags { /// Return list of permission arguments that are equivalent /// to the ones used to create `self`. pub fn to_permission_args(&self) -> Vec<String> { let mut args = vec![]; if self.allow_all { args.push("--allow-all".to_string()); return args; } match &self.allow_read { Some(read_allowlist) if read_allowlist.is_empty() => { args.push("--allow-read".to_string()); } Some(read_allowlist) => { let s = format!("--allow-read={}", join_paths(read_allowlist, ",")); args.push(s); } _ => {} } match &self.allow_write { Some(write_allowlist) if write_allowlist.is_empty() => { args.push("--allow-write".to_string()); } Some(write_allowlist) => { let s = format!("--allow-write={}", join_paths(write_allowlist, ",")); args.push(s); } _ => {} } match &self.allow_net { Some(net_allowlist) if net_allowlist.is_empty() => { args.push("--allow-net".to_string()); } Some(net_allowlist) => { let s = format!("--allow-net={}", net_allowlist.join(",")); args.push(s); } _ => {} } match &self.unsafely_ignore_certificate_errors { Some(ic_allowlist) if ic_allowlist.is_empty() => { args.push("--unsafely-ignore-certificate-errors".to_string()); } Some(ic_allowlist) => { let s = format!( "--unsafely-ignore-certificate-errors={}", ic_allowlist.join(",") ); args.push(s); } _ => {} } match &self.allow_env { Some(env_allowlist) if env_allowlist.is_empty() => { args.push("--allow-env".to_string()); } Some(env_allowlist) => { let s = format!("--allow-env={}", env_allowlist.join(",")); args.push(s); } _ => {} } match &self.allow_run { Some(run_allowlist) if run_allowlist.is_empty() => { args.push("--allow-run".to_string()); } Some(run_allowlist) => { let s = format!("--allow-run={}", run_allowlist.join(",")); args.push(s); } _ => {} } match &self.allow_ffi { Some(ffi_allowlist) if ffi_allowlist.is_empty() => { args.push("--allow-ffi".to_string()); } Some(ffi_allowlist) => { let s = format!("--allow-ffi={}", join_paths(ffi_allowlist, ",")); args.push(s); } _ => {} } if self.allow_hrtime { args.push("--allow-hrtime".to_string()); } args } /// Extract path arguments for config search paths. /// If it returns Some(vec), the config should be discovered /// from the current dir after trying to discover from each entry in vec. /// If it returns None, the config file shouldn't be discovered at all. pub fn config_path_args(&self) -> Option<Vec<PathBuf>> { use DenoSubcommand::*; if let Fmt(FmtFlags { files, .. }) = &self.subcommand { Some(files.clone()) } else if let Lint(LintFlags { files, .. }) = &self.subcommand { Some(files.clone()) } else if let Run(RunFlags { script }) = &self.subcommand { if let Ok(module_specifier) = deno_core::resolve_url_or_path(script) { if module_specifier.scheme() == "file" { if let Ok(p) = module_specifier.to_file_path() { Some(vec![p]) } else { Some(vec![]) } } else { // When the entrypoint doesn't have file: scheme (it's the remote // script), then we don't auto discover config file. None } } else { Some(vec![]) } } else { Some(vec![]) } } pub fn permissions_options(&self) -> PermissionsOptions { PermissionsOptions { allow_env: self.allow_env.clone(), allow_hrtime: self.allow_hrtime, allow_net: self.allow_net.clone(), allow_ffi: self.allow_ffi.clone(), allow_read: self.allow_read.clone(), allow_run: self.allow_run.clone(), allow_write: self.allow_write.clone(), prompt: !self.no_prompt, } }}
static ENV_VARIABLES_HELP: &str = r#"ENVIRONMENT VARIABLES: DENO_AUTH_TOKENS A semi-colon separated list of bearer tokens and hostnames to use when fetching remote modules from private repositories (e.g. "abcde12345@deno.land;54321edcba@github.com") DENO_TLS_CA_STORE Comma-separated list of order dependent certificate stores. Possible values: "system", "mozilla". Defaults to "mozilla". DENO_CERT Load certificate authority from PEM encoded file DENO_DIR Set the cache directory DENO_INSTALL_ROOT Set deno install's output directory (defaults to $HOME/.deno/bin) DENO_NO_PROMPT Set to disable permission prompts on access (alternative to passing --no-prompt on invocation) DENO_FUTURE_CHECK Opt-in to the upcoming behavior of the `deno run` subcommand that doesn't perform type-checking by default. DENO_WEBGPU_TRACE Directory to use for wgpu traces HTTP_PROXY Proxy address for HTTP requests (module downloads, fetch) HTTPS_PROXY Proxy address for HTTPS requests (module downloads, fetch) NO_COLOR Set to disable color NO_PROXY Comma-separated list of hosts which do not use a proxy (module downloads, fetch)"#;static DENO_HELP: Lazy<String> = Lazy::new(|| { format!( "A modern JavaScript and TypeScript runtimeDocs: https://deno.land/manual@v{}Modules: https://deno.land/std/ https://deno.land/x/Bugs: https://github.com/denoland/deno/issues
To start the REPL:
denoTo execute a script:
deno run https://deno.land/std/examples/welcome.tsTo evaluate code in the shell:
deno eval \"console.log(30933 + 404)\"", SHORT_VERSION.as_str() )});
/// Main entry point for parsing deno's command line flags.pub fn flags_from_vec(args: Vec<String>) -> clap::Result<Flags> { let version = crate::version::deno(); let app = clap_root(&version); let matches = app.clone().try_get_matches_from(&args)?; let mut flags = Flags::default(); if matches.is_present("unstable") { flags.unstable = true; } if matches.is_present("log-level") { flags.log_level = match matches.value_of("log-level").unwrap() { "debug" => Some(Level::Debug), "info" => Some(Level::Info), _ => unreachable!(), }; } if matches.is_present("quiet") { flags.log_level = Some(Level::Error); } match matches.subcommand() { Some(("bench", m)) => bench_parse(&mut flags, m), Some(("bundle", m)) => bundle_parse(&mut flags, m), Some(("cache", m)) => cache_parse(&mut flags, m), Some(("check", m)) => check_parse(&mut flags, m), Some(("compile", m)) => compile_parse(&mut flags, m), Some(("completions", m)) => completions_parse(&mut flags, m, app), Some(("coverage", m)) => coverage_parse(&mut flags, m), Some(("doc", m)) => doc_parse(&mut flags, m), Some(("eval", m)) => eval_parse(&mut flags, m), Some(("fmt", m)) => fmt_parse(&mut flags, m), Some(("info", m)) => info_parse(&mut flags, m), Some(("install", m)) => install_parse(&mut flags, m), Some(("lint", m)) => lint_parse(&mut flags, m), Some(("lsp", m)) => lsp_parse(&mut flags, m), Some(("repl", m)) => repl_parse(&mut flags, m), Some(("run", m)) => run_parse(&mut flags, m), Some(("task", m)) => task_parse(&mut flags, m, &args), Some(("test", m)) => test_parse(&mut flags, m), Some(("types", m)) => types_parse(&mut flags, m), Some(("uninstall", m)) => uninstall_parse(&mut flags, m), Some(("upgrade", m)) => upgrade_parse(&mut flags, m), Some(("vendor", m)) => vendor_parse(&mut flags, m), _ => handle_repl_flags( &mut flags, ReplFlags { eval_files: None, eval: None, }, ), } Ok(flags)}
fn handle_repl_flags(flags: &mut Flags, repl_flags: ReplFlags) { flags.repl = true; flags.subcommand = DenoSubcommand::Repl(repl_flags); flags.allow_net = Some(vec![]); flags.allow_env = Some(vec![]); flags.allow_run = Some(vec![]); flags.allow_read = Some(vec![]); flags.allow_write = Some(vec![]); flags.allow_ffi = Some(vec![]); flags.allow_hrtime = true;}
fn clap_root(version: &str) -> Command { clap::Command::new("deno") .bin_name("deno") .color(ColorChoice::Never) .max_term_width(80) .version(version) .long_version(LONG_VERSION.as_str()) .arg( Arg::new("unstable") .long("unstable") .help("Enable unstable features and APIs") .global(true), ) .arg( Arg::new("log-level") .short('L') .long("log-level") .help("Set log level") .hide(true) .takes_value(true) .possible_values(&["debug", "info"]) .global(true), ) .arg( Arg::new("quiet") .short('q') .long("quiet") .help("Suppress diagnostic output") .global(true), ) .subcommand(bench_subcommand()) .subcommand(bundle_subcommand()) .subcommand(cache_subcommand()) .subcommand(check_subcommand()) .subcommand(compile_subcommand()) .subcommand(completions_subcommand()) .subcommand(coverage_subcommand()) .subcommand(doc_subcommand()) .subcommand(eval_subcommand()) .subcommand(fmt_subcommand()) .subcommand(info_subcommand()) .subcommand(install_subcommand()) .subcommand(uninstall_subcommand()) .subcommand(lsp_subcommand()) .subcommand(lint_subcommand()) .subcommand(repl_subcommand()) .subcommand(run_subcommand()) .subcommand(task_subcommand()) .subcommand(test_subcommand()) .subcommand(types_subcommand()) .subcommand(upgrade_subcommand()) .subcommand(vendor_subcommand()) .long_about(DENO_HELP.as_str()) .after_help(ENV_VARIABLES_HELP)}
fn bench_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("bench"), true, false) .trailing_var_arg(true) .arg( Arg::new("ignore") .long("ignore") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Ignore files"), ) .arg( Arg::new("filter") .allow_hyphen_values(true) .long("filter") .takes_value(true) .help("Run benchmarks with this string or pattern in the bench name"), ) .arg( Arg::new("files") .help("List of file names to run") .takes_value(true) .multiple_values(true) .multiple_occurrences(true), ) .arg(watch_arg(false)) .arg(no_clear_screen_arg()) .arg(script_arg().last(true)) .about("Run benchmarks") .long_about( "Run benchmarks using Deno's built-in bench tool.Evaluate the given modules, run all benches declared with 'Deno.bench()' \and report results to standard output:
deno bench src/fetch_bench.ts src/signal_bench.tsDirectory arguments are expanded to all contained files matching the \glob {*_,*.,}bench.{js,mjs,ts,jsx,tsx}:
deno bench src/", )}
fn bundle_subcommand<'a>() -> Command<'a> { compile_args(Command::new("bundle")) .arg( Arg::new("source_file") .takes_value(true) .required(true) .value_hint(ValueHint::FilePath), ) .arg( Arg::new("out_file") .takes_value(true) .required(false) .value_hint(ValueHint::FilePath), ) .arg(watch_arg(false)) .arg(no_clear_screen_arg()) .about("Bundle module and dependencies into single file") .long_about( "Output a single JavaScript file with all dependencies. deno bundle https://deno.land/std/examples/colors.ts colors.bundle.jsIf no output file is given, the output is written to standard output:
deno bundle https://deno.land/std/examples/colors.ts", )}
fn cache_subcommand<'a>() -> Command<'a> { compile_args(Command::new("cache")) .arg( Arg::new("file") .takes_value(true) .required(true) .min_values(1) .value_hint(ValueHint::FilePath), ) .about("Cache the dependencies") .long_about( "Cache and compile remote dependencies recursively.Download and compile a module with all of its static dependencies and save \them in the local cache, without running any code:
deno cache https://deno.land/std/http/file_server.tsFuture runs of this module will trigger no downloads or compilation unless \--reload is specified.", )}
fn check_subcommand<'a>() -> Command<'a> { compile_args_without_no_check(Command::new("check")) .arg( Arg::new("remote") .long("remote") .help("Type-check all modules, including remote") ) .arg( Arg::new("file") .takes_value(true) .required(true) .min_values(1) .value_hint(ValueHint::FilePath), ) .about("Type-check the dependencies") .long_about( "Download and type-check without execution. deno check https://deno.land/std/http/file_server.tsUnless --reload is specified, this command will not re-download already cached dependencies.", )}
fn compile_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("compile"), true, false) .trailing_var_arg(true) .arg(script_arg().required(true)) .arg( Arg::new("output") .long("output") .short('o') .help("Output file (defaults to $PWD/<inferred-name>)") .takes_value(true) .value_hint(ValueHint::FilePath), ) .arg( Arg::new("target") .long("target") .help("Target OS architecture") .takes_value(true) .possible_values(&[ "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin", ]), ) .about("UNSTABLE: Compile the script into a self contained executable") .long_about( "UNSTABLE: Compiles the given script into a self contained executable. deno compile -A https://deno.land/std/http/file_server.ts deno compile --output color_util https://deno.land/std/examples/colors.tsAny flags passed which affect runtime behavior, such as '--unstable', \'--allow-*', '--v8-flags', etc. are encoded into the output executable and \used at runtime as if they were passed to a similar 'deno run' command.
The executable name is inferred by default: Attempt to take the file stem of \the URL path. The above example would become 'file_server'. If the file stem \is something generic like 'main', 'mod', 'index' or 'cli', and the path has no \parent, take the file name of the parent path. Otherwise settle with the \generic name. If the resulting name has an '@...' suffix, strip it.
Cross-compiling to different target architectures is supported using the \`--target` flag. On the first invocation with deno will download proper \binary and cache it in $DENO_DIR. The aarch64-apple-darwin target is not \supported in canary.", )}
fn completions_subcommand<'a>() -> Command<'a> { Command::new("completions") .disable_help_subcommand(true) .arg( Arg::new("shell") .possible_values(&["bash", "fish", "powershell", "zsh", "fig"]) .required(true), ) .about("Generate shell completions") .long_about( "Output shell completion script to standard output. deno completions bash > /usr/local/etc/bash_completion.d/deno.bash source /usr/local/etc/bash_completion.d/deno.bash", )}
fn coverage_subcommand<'a>() -> Command<'a> { Command::new("coverage") .about("Print coverage reports") .long_about( "Print coverage reports from coverage profiles.Collect a coverage profile with deno test:
deno test --coverage=cov_profilePrint a report to stdout:
deno coverage cov_profileInclude urls that start with the file schema:
deno coverage --include=\"^file:\" cov_profileExclude urls ending with test.ts and test.js:
deno coverage --exclude=\"test\\.(ts|js)\" cov_profileInclude urls that start with the file schema and exclude files ending with \test.ts and test.js, for an url to match it must match the include pattern and \not match the exclude pattern:
deno coverage --include=\"^file:\" --exclude=\"test\\.(ts|js)\" cov_profileWrite a report using the lcov format:
deno coverage --lcov --output=cov.lcov cov_profile/Generate html reports from lcov:
genhtml -o html_cov cov.lcov", ) .arg( Arg::new("ignore") .long("ignore") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Ignore coverage files") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("include") .long("include") .takes_value(true) .value_name("regex") .multiple_values(true) .multiple_occurrences(true) .require_equals(true) .default_value(r"^file:") .help("Include source files in the report"), ) .arg( Arg::new("exclude") .long("exclude") .takes_value(true) .value_name("regex") .multiple_values(true) .multiple_occurrences(true) .require_equals(true) .default_value(r"test\.(js|mjs|ts|jsx|tsx)$") .help("Exclude source files from the report"), ) .arg( Arg::new("lcov") .long("lcov") .help("Output coverage report in lcov format") .takes_value(false), ) .arg( Arg::new("output") .requires("lcov") .long("output") .help("Output file (defaults to stdout) for lcov") .long_help( "Exports the coverage report in lcov format to the given file. \ Filename should be passed along with '=' For example '--output=foo.lcov' \ If no --output arg is specified then the report is written to stdout.", ) .takes_value(true) .require_equals(true) .value_hint(ValueHint::FilePath), ) .arg( Arg::new("files") .takes_value(true) .multiple_values(true) .multiple_occurrences(true) .required(true) .value_hint(ValueHint::AnyPath), )}
fn doc_subcommand<'a>() -> Command<'a> { Command::new("doc") .about("Show documentation for a module") .long_about( "Show documentation for a module.Output documentation to standard output:
deno doc ./path/to/module.tsOutput private documentation to standard output:
deno doc --private ./path/to/module.tsOutput documentation in JSON format:
deno doc --json ./path/to/module.tsTarget a specific symbol:
deno doc ./path/to/module.ts MyClass.someFieldShow documentation for runtime built-ins:
deno doc deno doc --builtin Deno.Listener", ) .arg(import_map_arg()) .arg(reload_arg()) .arg( Arg::new("json") .long("json") .help("Output documentation in JSON format") .takes_value(false), ) .arg( Arg::new("private") .long("private") .help("Output private documentation") .takes_value(false), ) // TODO(nayeemrmn): Make `--builtin` a proper option. Blocked by // https://github.com/clap-rs/clap/issues/1794. Currently `--builtin` is // just a possible value of `source_file` so leading hyphens must be // enabled. .allow_hyphen_values(true) .arg( Arg::new("source_file") .takes_value(true) .value_hint(ValueHint::FilePath), ) .arg( Arg::new("filter") .help("Dot separated path to symbol") .takes_value(true) .required(false) .conflicts_with("json"), )}
fn eval_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("eval"), false, true) .about("Eval script") .long_about( "Evaluate JavaScript from the command line. deno eval \"console.log('hello world')\"To evaluate as TypeScript:
deno eval --ext=ts \"const v: string = 'hello'; console.log(v)\"This command has implicit access to all permissions (--allow-all).", ) .arg( // TODO(@satyarohith): remove this argument in 2.0. Arg::new("ts") .long("ts") .short('T') .help("Treat eval input as TypeScript") .takes_value(false) .multiple_occurrences(false) .multiple_values(false) .hide(true), ) .arg( Arg::new("ext") .long("ext") .help("Set standard input (stdin) content type") .takes_value(true) .default_value("js") .possible_values(&["ts", "tsx", "js", "jsx"]), ) .arg( Arg::new("print") .long("print") .short('p') .help("print result to stdout") .takes_value(false) .multiple_occurrences(false) .multiple_values(false), ) .arg( Arg::new("code_arg") .multiple_values(true) .multiple_occurrences(true) .help("Code arg") .value_name("CODE_ARG") .required(true), )}
fn fmt_subcommand<'a>() -> Command<'a> { Command::new("fmt") .about("Format source files") .long_about( "Auto-format JavaScript, TypeScript, Markdown, and JSON files. deno fmt deno fmt myfile1.ts myfile2.ts deno fmt --checkFormat stdin and write to stdout:
cat file.ts | deno fmt -Ignore formatting code by preceding it with an ignore comment:
// deno-fmt-ignoreIgnore formatting a file by adding an ignore comment at the top of the file:
// deno-fmt-ignore-file", ) .arg(config_arg()) .arg( Arg::new("check") .long("check") .help("Check if the source files are formatted") .takes_value(false), ) .arg( Arg::new("ext") .long("ext") .help("Set standard input (stdin) content type") .takes_value(true) .default_value("ts") .possible_values(&["ts", "tsx", "js", "jsx", "md", "json", "jsonc"]), ) .arg( Arg::new("ignore") .long("ignore") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Ignore formatting particular source files") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("files") .takes_value(true) .multiple_values(true) .multiple_occurrences(true) .required(false) .value_hint(ValueHint::AnyPath), ) .arg(watch_arg(false)) .arg(no_clear_screen_arg()) .arg( Arg::new("options-use-tabs") .long("options-use-tabs") .help("Use tabs instead of spaces for indentation. Defaults to false."), ) .arg( Arg::new("options-line-width") .long("options-line-width") .help("Define maximum line width. Defaults to 80.") .takes_value(true) .validator(|val: &str| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => { Err("options-line-width should be a non zero integer".to_string()) } }), ) .arg( Arg::new("options-indent-width") .long("options-indent-width") .help("Define indentation width. Defaults to 2.") .takes_value(true) .validator(|val: &str| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => { Err("options-indent-width should be a non zero integer".to_string()) } }), ) .arg( Arg::new("options-single-quote") .long("options-single-quote") .help("Use single quotes. Defaults to false."), ) .arg( Arg::new("options-prose-wrap") .long("options-prose-wrap") .takes_value(true) .possible_values(&["always", "never", "preserve"]) .help("Define how prose should be wrapped. Defaults to always."), )}
fn info_subcommand<'a>() -> Command<'a> { Command::new("info") .about("Show info about cache or info related to source file") .long_about( "Information about a module or the cache directories.Get information about a module:
deno info https://deno.land/std/http/file_server.tsThe following information is shown:
local: Local path of the file.type: JavaScript, TypeScript, or JSON.emit: Local path of compiled source code. (TypeScript only.)dependencies: Dependency tree of the source file.
Without any additional arguments, 'deno info' shows:
DENO_DIR: Directory containing Deno-managed files.Remote modules cache: Subdirectory containing downloaded remote modules.TypeScript compiler cache: Subdirectory containing TS compiler output.", ) .arg(Arg::new("file").takes_value(true).required(false).value_hint(ValueHint::FilePath)) .arg(reload_arg().requires("file")) .arg(ca_file_arg()) .arg( location_arg() .conflicts_with("file") .help("Show files used for origin bound APIs like the Web Storage API when running a script with '--location=<HREF>'") ) // TODO(lucacasonato): remove for 2.0 .arg(no_check_arg().hide(true)) .arg(import_map_arg()) .arg( Arg::new("json") .long("json") .help("UNSTABLE: Outputs the information in JSON format") .takes_value(false), )}
fn install_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("install"), true, true) .trailing_var_arg(true) .arg(Arg::new("cmd").required(true).multiple_values(true).value_hint(ValueHint::FilePath)) .arg( Arg::new("name") .long("name") .short('n') .help("Executable file name") .takes_value(true) .required(false)) .arg( Arg::new("root") .long("root") .help("Installation root") .takes_value(true) .multiple_occurrences(false) .multiple_values(false) .value_hint(ValueHint::DirPath)) .arg( Arg::new("force") .long("force") .short('f') .help("Forcefully overwrite existing installation") .takes_value(false)) .about("Install script as an executable") .long_about( "Installs a script as an executable in the installation root's bin directory. deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts deno install https://deno.land/std/examples/colors.tsTo change the executable name, use -n/--name:
deno install --allow-net --allow-read -n serve https://deno.land/std/http/file_server.tsThe executable name is inferred by default: - Attempt to take the file stem of the URL path. The above example would become 'file_server'. - If the file stem is something generic like 'main', 'mod', 'index' or 'cli', and the path has no parent, take the file name of the parent path. Otherwise settle with the generic name. - If the resulting name has an '@...' suffix, strip it.To change the installation root, use --root:
deno install --allow-net --allow-read --root /usr/local https://deno.land/std/http/file_server.tsThe installation root is determined, in order of precedence: - --root option - DENO_INSTALL_ROOT environment variable - $HOME/.denoThese must be added to the path manually if required.")}
fn uninstall_subcommand<'a>() -> Command<'a> { Command::new("uninstall") .trailing_var_arg(true) .arg( Arg::new("name") .required(true) .multiple_occurrences(false) .allow_hyphen_values(true)) .arg( Arg::new("root") .long("root") .help("Installation root") .takes_value(true) .multiple_occurrences(false) .value_hint(ValueHint::DirPath)) .about("Uninstall a script previously installed with deno install") .long_about( "Uninstalls an executable script in the installation root's bin directory. deno uninstall serveTo change the installation root, use --root:
deno uninstall --root /usr/local serveThe installation root is determined, in order of precedence: - --root option - DENO_INSTALL_ROOT environment variable - $HOME/.deno")}
static LSP_HELP: Lazy<String> = Lazy::new(|| { format!( "The 'deno lsp' subcommand provides a way for code editors and IDEs tointeract with Deno using the Language Server Protocol. Usually humans do notuse this subcommand directly. For example, 'deno lsp' can provide IDEs withgo-to-definition support and automatic code formatting.
How to connect various editors and IDEs to 'deno lsp':https://deno.land/manual@v{}/getting_started/setup_your_environment#editors-and-ides", SHORT_VERSION.as_str() )});
fn lsp_subcommand<'a>() -> Command<'a> { Command::new("lsp") .about("Start the language server") .long_about(LSP_HELP.as_str())}
fn lint_subcommand<'a>() -> Command<'a> { Command::new("lint") .about("Lint source files") .long_about( "Lint JavaScript/TypeScript source code. deno lint deno lint myfile1.ts myfile2.jsPrint result as JSON:
deno lint --jsonRead from stdin:
cat file.ts | deno lint - cat file.ts | deno lint --json -List available rules:
deno lint --rulesIgnore diagnostics on the next line by preceding it with an ignore comment andrule name:
// deno-lint-ignore no-explicit-any // deno-lint-ignore require-await no-emptyNames of rules to ignore must be specified after ignore comment.
Ignore linting a file by adding an ignore comment at the top of the file:
// deno-lint-ignore-file", ) .arg(Arg::new("rules").long("rules").help("List available rules")) .arg( Arg::new("rules-tags") .long("rules-tags") .require_equals(true) .takes_value(true) .use_value_delimiter(true) .conflicts_with("rules") .help("Use set of rules with a tag"), ) .arg( Arg::new("rules-include") .long("rules-include") .require_equals(true) .takes_value(true) .use_value_delimiter(true) .conflicts_with("rules") .help("Include lint rules"), ) .arg( Arg::new("rules-exclude") .long("rules-exclude") .require_equals(true) .takes_value(true) .use_value_delimiter(true) .conflicts_with("rules") .help("Exclude lint rules"), ) .arg(config_arg()) .arg( Arg::new("ignore") .long("ignore") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Ignore linting particular source files") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("json") .long("json") .help("Output lint result in JSON format") .takes_value(false), ) .arg( Arg::new("files") .takes_value(true) .multiple_values(true) .multiple_occurrences(true) .required(false) .value_hint(ValueHint::AnyPath), ) .arg(watch_arg(false)) .arg(no_clear_screen_arg())}
fn repl_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("repl"), false, true) .about("Read Eval Print Loop") .arg( Arg::new("eval-file") .long("eval-file") .min_values(1) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Evaluates the provided file(s) as scripts when the REPL starts. Accepts file paths and URLs.") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("eval") .long("eval") .help("Evaluates the provided code when the REPL starts.") .takes_value(true) .value_name("code"), ) .arg(unsafely_ignore_certificate_errors_arg())}
fn run_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("run"), true, true) .arg( watch_arg(true) .conflicts_with("inspect") .conflicts_with("inspect-brk"), ) .arg(no_clear_screen_arg()) .arg(check_arg()) .trailing_var_arg(true) .arg(script_arg().required(true)) .about("Run a JavaScript or TypeScript program") .long_about( "Run a JavaScript or TypeScript programBy default all programs are run in sandbox without access to disk, network orability to spawn subprocesses.
deno run https://deno.land/std/examples/welcome.tsGrant all permissions:
deno run -A https://deno.land/std/http/file_server.tsGrant permission to read from disk and listen to network:
deno run --allow-read --allow-net https://deno.land/std/http/file_server.tsGrant permission to read allow-listed files from disk:
deno run --allow-read=/etc https://deno.land/std/http/file_server.tsSpecifying the filename '-' to read the file from stdin.
curl https://deno.land/std/examples/welcome.ts | deno run -", )}
fn task_subcommand<'a>() -> Command<'a> { Command::new("task") .trailing_var_arg(true) .arg(config_arg()) // Ideally the task name and trailing arguments should be two separate clap // arguments, but there is a bug in clap that's preventing us from doing // this (https://github.com/clap-rs/clap/issues/1538). Once that's fixed, // then we can revert this back to what it used to be. .arg(Arg::new("task_name_and_args") .multiple_values(true) .multiple_occurrences(true) .allow_hyphen_values(true) .help("Task to be executed with any additional arguments passed to the task")) .about("Run a task defined in the configuration file") .long_about( "Run a task defined in the configuration file deno task build", )}
fn test_subcommand<'a>() -> Command<'a> { runtime_args(Command::new("test"), true, true) .trailing_var_arg(true) .arg( Arg::new("ignore") .long("ignore") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Ignore files") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("no-run") .long("no-run") .help("Cache test modules, but don't run tests") .takes_value(false), ) .arg( Arg::new("trace-ops") .long("trace-ops") .help("Enable tracing of async ops. Useful when debugging leaking ops in test, but impacts test execution time.") .takes_value(false), ) .arg( Arg::new("doc") .long("doc") .help("UNSTABLE: type check code blocks") .takes_value(false), ) .arg( Arg::new("fail-fast") .long("fail-fast") .alias("failfast") .help("Stop after N errors. Defaults to stopping after first failure.") .min_values(0) .required(false) .takes_value(true) .require_equals(true) .value_name("N") .validator(|val: &str| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => Err("fail-fast should be a non zero integer".to_string()), }), ) .arg( Arg::new("allow-none") .long("allow-none") .help("Don't return error code if no test files are found") .takes_value(false), ) .arg( Arg::new("filter") .allow_hyphen_values(true) .long("filter") .takes_value(true) .help("Run tests with this string or pattern in the test name"), ) .arg( Arg::new("shuffle") .long("shuffle") .value_name("NUMBER") .help("(UNSTABLE): Shuffle the order in which the tests are run") .min_values(0) .max_values(1) .require_equals(true) .takes_value(true) .validator(|val: &str| match val.parse::<u64>() { Ok(_) => Ok(()), Err(_) => Err("Shuffle seed should be a number".to_string()), }), ) .arg( Arg::new("coverage") .long("coverage") .require_equals(true) .takes_value(true) .value_name("DIR") .conflicts_with("inspect") .conflicts_with("inspect-brk") .help("UNSTABLE: Collect coverage profile data into DIR"), ) .arg( Arg::new("jobs") .short('j') .long("jobs") .help("Number of parallel workers, defaults to # of CPUs when no value is provided. Defaults to 1 when the option is not present.") .min_values(0) .max_values(1) .takes_value(true) .validator(|val: &str| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => Err("jobs should be a non zero unsigned integer".to_string()), }), ) .arg( Arg::new("files") .help("List of file names to run") .takes_value(true) .multiple_values(true) .multiple_occurrences(true) .value_hint(ValueHint::AnyPath), ) .arg( watch_arg(false) .conflicts_with("no-run") .conflicts_with("coverage"), ) .arg(no_clear_screen_arg()) .arg(script_arg().last(true)) .about("Run tests") .long_about( "Run tests using Deno's built-in test runner.Evaluate the given modules, run all tests declared with 'Deno.test()' andreport results to standard output:
deno test src/fetch_test.ts src/signal_test.tsDirectory arguments are expanded to all contained files matching the glob{*_,*.,}test.{js,mjs,ts,jsx,tsx}:
deno test src/", )}
fn types_subcommand<'a>() -> Command<'a> { Command::new("types") .about("Print runtime TypeScript declarations") .long_about( "Print runtime TypeScript declarations. deno types > lib.deno.d.tsThe declaration file could be saved and used for typing information.", )}
fn upgrade_subcommand<'a>() -> Command<'a> { Command::new("upgrade") .about("Upgrade deno executable to given version") .long_about( "Upgrade deno executable to the given version.Defaults to latest.
The version is downloaded fromhttps://github.com/denoland/deno/releasesand is used to replace the current executable.
If you want to not replace the current Deno executable but instead download anupdate to a different location, use the --output flag
deno upgrade --output $HOME/my_deno", ) .arg( Arg::new("version") .long("version") .help("The version to upgrade to") .takes_value(true), ) .arg( Arg::new("output") .long("output") .help("The path to output the updated version to") .takes_value(true) .value_hint(ValueHint::FilePath), ) .arg( Arg::new("dry-run") .long("dry-run") .help("Perform all checks without replacing old exe"), ) .arg( Arg::new("force") .long("force") .short('f') .help("Replace current exe even if not out-of-date"), ) .arg( Arg::new("canary") .long("canary") .help("Upgrade to canary builds"), ) .arg(ca_file_arg())}
fn vendor_subcommand<'a>() -> Command<'a> { Command::new("vendor") .about("Vendor remote modules into a local directory") .long_about( "Vendor remote modules into a local directory.Analyzes the provided modules along with their dependencies, downloadsremote modules to the output directory, and produces an import map thatmaps remote specifiers to the downloaded files.
deno vendor main.ts deno run --import-map vendor/import_map.json main.tsRemote modules and multiple modules may also be specified:
deno vendor main.ts test.deps.ts https://deno.land/std/path/mod.ts", ) .arg( Arg::new("specifiers") .takes_value(true) .multiple_values(true) .multiple_occurrences(true) .required(true), ) .arg( Arg::new("output") .long("output") .help("The directory to output the vendored modules to") .takes_value(true) .value_hint(ValueHint::DirPath), ) .arg( Arg::new("force") .long("force") .short('f') .help( "Forcefully overwrite conflicting files in existing output directory", ) .takes_value(false), ) .arg(config_arg()) .arg(import_map_arg()) .arg(lock_arg()) .arg(reload_arg()) .arg(ca_file_arg())}
fn compile_args(app: Command) -> Command { app .arg(import_map_arg()) .arg(no_remote_arg()) .arg(config_arg()) .arg(no_check_arg()) .arg(reload_arg()) .arg(lock_arg()) .arg(lock_write_arg()) .arg(ca_file_arg())}
fn compile_args_without_no_check(app: Command) -> Command { app .arg(import_map_arg()) .arg(no_remote_arg()) .arg(config_arg()) .arg(reload_arg()) .arg(lock_arg()) .arg(lock_write_arg()) .arg(ca_file_arg())}
fn permission_args(app: Command) -> Command { app .arg( Arg::new("allow-read") .long("allow-read") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow file system read access") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("allow-write") .long("allow-write") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow file system write access") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("allow-net") .long("allow-net") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow network access") .validator(crate::flags_allow_net::validator), ) .arg(unsafely_ignore_certificate_errors_arg()) .arg( Arg::new("allow-env") .long("allow-env") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow environment access") .validator(|keys| { for key in keys.split(',') { if key.is_empty() || key.contains(&['=', '\0'] as &[char]) { return Err(format!("invalid key \"{}\"", key)); } } Ok(()) }), ) .arg( Arg::new("allow-run") .long("allow-run") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow running subprocesses"), ) .arg( Arg::new("allow-ffi") .long("allow-ffi") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Allow loading dynamic libraries") .value_hint(ValueHint::AnyPath), ) .arg( Arg::new("allow-hrtime") .long("allow-hrtime") .help("Allow high resolution time measurement"), ) .arg( Arg::new("allow-all") .short('A') .long("allow-all") .help("Allow all permissions"), ) .arg(Arg::new("prompt").long("prompt").hide(true).help( "deprecated: Fallback to prompt if required permission wasn't passed", )) .arg( Arg::new("no-prompt") .long("no-prompt") .help("Always throw if required permission wasn't passed"), )}
fn runtime_args( app: Command, include_perms: bool, include_inspector: bool,) -> Command { let app = compile_args(app); let app = if include_perms { permission_args(app) } else { app }; let app = if include_inspector { inspect_args(app) } else { app }; app .arg(cached_only_arg()) .arg(location_arg()) .arg(v8_flags_arg()) .arg(seed_arg()) .arg(enable_testing_features_arg()) .arg(compat_arg())}
fn inspect_args(app: Command) -> Command { app .arg( Arg::new("inspect") .long("inspect") .value_name("HOST:PORT") .help("Activate inspector on host:port (default: 127.0.0.1:9229)") .min_values(0) .max_values(1) .require_equals(true) .takes_value(true) .validator(inspect_arg_validate), ) .arg( Arg::new("inspect-brk") .long("inspect-brk") .value_name("HOST:PORT") .help( "Activate inspector on host:port and break at start of user script", ) .min_values(0) .max_values(1) .require_equals(true) .takes_value(true) .validator(inspect_arg_validate), )}
static IMPORT_MAP_HELP: Lazy<String> = Lazy::new(|| { format!( "Load import map file from local file or remote URL. Docs: https://deno.land/manual@v{}/linking_to_external_code/import_maps Specification: https://wicg.github.io/import-maps/ Examples: https://github.com/WICG/import-maps#the-import-map", SHORT_VERSION.as_str() )});
fn import_map_arg<'a>() -> Arg<'a> { Arg::new("import-map") .long("import-map") .alias("importmap") .value_name("FILE") .help("Load import map file") .long_help(IMPORT_MAP_HELP.as_str()) .takes_value(true) .value_hint(ValueHint::FilePath)}
fn reload_arg<'a>() -> Arg<'a> { Arg::new("reload") .short('r') .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .long("reload") .help("Reload source code cache (recompile TypeScript)") .value_name("CACHE_BLOCKLIST") .long_help( "Reload source code cache (recompile TypeScript)--reload Reload everything--reload=https://deno.land/std Reload only standard modules--reload=https://deno.land/std/fs/utils.ts,https://deno.land/std/fmt/colors.ts Reloads specific modules", ) .value_hint(ValueHint::FilePath)}
fn ca_file_arg<'a>() -> Arg<'a> { Arg::new("cert") .long("cert") .value_name("FILE") .help("Load certificate authority from PEM encoded file") .takes_value(true) .value_hint(ValueHint::FilePath)}
fn cached_only_arg<'a>() -> Arg<'a> { Arg::new("cached-only") .long("cached-only") .help("Require that remote dependencies are already cached")}
fn location_arg<'a>() -> Arg<'a> { Arg::new("location") .long("location") .takes_value(true) .value_name("HREF") .validator(|href| { let url = Url::parse(href); if url.is_err() { return Err("Failed to parse URL".to_string()); } let mut url = url.unwrap(); if !["http", "https"].contains(&url.scheme()) { return Err("Expected protocol \"http\" or \"https\"".to_string()); } url.set_username("").unwrap(); url.set_password(None).unwrap(); Ok(()) }) .help("Value of 'globalThis.location' used by some web APIs") .value_hint(ValueHint::Url)}
fn enable_testing_features_arg<'a>() -> Arg<'a> { Arg::new("enable-testing-features-do-not-use") .long("enable-testing-features-do-not-use") .help("INTERNAL: Enable internal features used during integration testing") .hide(true)}
fn v8_flags_arg<'a>() -> Arg<'a> { Arg::new("v8-flags") .long("v8-flags") .takes_value(true) .use_value_delimiter(true) .require_equals(true) .help("Set V8 command line options") .long_help("To see a list of all available flags use --v8-flags=--help.")}
fn seed_arg<'a>() -> Arg<'a> { Arg::new("seed") .long("seed") .value_name("NUMBER") .help("Set the random number generator seed") .takes_value(true) .validator(|val| match val.parse::<u64>() { Ok(_) => Ok(()), Err(_) => Err("Seed should be a number".to_string()), })}
static COMPAT_HELP: Lazy<String> = Lazy::new(|| { format!( "See https://deno.land/manual@v{}/node/compatibility_mode", SHORT_VERSION.as_str() )});
fn compat_arg<'a>() -> Arg<'a> { Arg::new("compat") .long("compat") .requires("unstable") .help("UNSTABLE: Node compatibility mode.") .long_help(COMPAT_HELP.as_str())}
fn watch_arg<'a>(takes_files: bool) -> Arg<'a> { let arg = Arg::new("watch") .long("watch") .help("UNSTABLE: Watch for file changes and restart automatically"); if takes_files { arg .value_name("FILES") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .long_help( "UNSTABLE: Watch for file changes and restart process automatically.Local files from entry point module graph are watched by default.Additional paths might be watched by passing them as arguments to this flag.", ) .value_hint(ValueHint::AnyPath) } else { arg.long_help( "UNSTABLE: Watch for file changes and restart process automatically. \ Only local files from entry point module graph are watched.", ) }}
fn no_clear_screen_arg<'a>() -> Arg<'a> { Arg::new("no-clear-screen") .requires("watch") .long("no-clear-screen") .help("Do not clear terminal screen when under watch mode")}
fn no_check_arg<'a>() -> Arg<'a> { Arg::new("no-check") .takes_value(true) .require_equals(true) .min_values(0) .value_name("NO_CHECK_TYPE") .long("no-check") .help("Skip type checking modules") .long_help( "Skip type-checking. If the value of '--no-check=remote' is supplied, \ diagnostic errors from remote modules will be ignored.", )}
fn check_arg<'a>() -> Arg<'a> { Arg::new("check") .conflicts_with("no-check") .long("check") .takes_value(true) .require_equals(true) .min_values(0) .value_name("CHECK_TYPE") .help("Type check modules") .long_help( "Type check modules.Currently this is a default behavior to type check modules, but in future releasesDeno will not automatically type check without the --check flag.
If the value of '--check=all' is supplied, diagnostic errors from remote moduleswill be included.", )}
fn script_arg<'a>() -> Arg<'a> { Arg::new("script_arg") .multiple_values(true) .multiple_occurrences(true) // NOTE: these defaults are provided // so `deno run --v8-flags=--help` works // without specifying file to run. .default_value_ifs(&[ ("v8-flags", Some("--help"), Some("_")), ("v8-flags", Some("-help"), Some("_")), ]) .help("Script arg") .value_name("SCRIPT_ARG") .value_hint(ValueHint::FilePath)}
fn lock_arg<'a>() -> Arg<'a> { Arg::new("lock") .long("lock") .value_name("FILE") .help("Check the specified lock file") .takes_value(true) .value_hint(ValueHint::FilePath)}
fn lock_write_arg<'a>() -> Arg<'a> { Arg::new("lock-write") .long("lock-write") .requires("lock") .help("Write lock file (use with --lock)")}
static CONFIG_HELP: Lazy<String> = Lazy::new(|| { format!( "The configuration file can be used to configure different aspects of \ deno including TypeScript, linting, and code formatting. Typically the \ configuration file will be called `deno.json` or `deno.jsonc` and \ automatically detected; in that case this flag is not necessary. \ See https://deno.land/manual@v{}/getting_started/configuration_file", SHORT_VERSION.as_str() )});
fn config_arg<'a>() -> Arg<'a> { Arg::new("config") .short('c') .long("config") .value_name("FILE") .help("Specify the configuration file") .long_help(CONFIG_HELP.as_str()) .takes_value(true) .value_hint(ValueHint::FilePath)}
fn no_remote_arg<'a>() -> Arg<'a> { Arg::new("no-remote") .long("no-remote") .help("Do not resolve remote modules")}
fn unsafely_ignore_certificate_errors_arg<'a>() -> Arg<'a> { Arg::new("unsafely-ignore-certificate-errors") .long("unsafely-ignore-certificate-errors") .min_values(0) .takes_value(true) .use_value_delimiter(true) .require_equals(true) .value_name("HOSTNAMES") .help("DANGER: Disables verification of TLS certificates") .validator(crate::flags_allow_net::validator)}
fn bench_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, false); // NOTE: `deno bench` always uses `--no-prompt`, tests shouldn't ever do // interactive prompts, unless done by user code flags.no_prompt = true; let ignore = match matches.values_of("ignore") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let filter = matches.value_of("filter").map(String::from); if matches.is_present("script_arg") { let script_arg: Vec<String> = matches .values_of("script_arg") .unwrap() .map(String::from) .collect(); for v in script_arg { flags.argv.push(v); } } let include = if matches.is_present("files") { let files: Vec<String> = matches .values_of("files") .unwrap() .map(String::from) .collect(); Some(files) } else { None }; watch_arg_parse(flags, matches, false); flags.subcommand = DenoSubcommand::Bench(BenchFlags { include, ignore, filter, });}
fn bundle_parse(flags: &mut Flags, matches: &clap::ArgMatches) { compile_args_parse(flags, matches); let source_file = matches.value_of("source_file").unwrap().to_string(); let out_file = if let Some(out_file) = matches.value_of("out_file") { flags.allow_write = Some(vec![]); Some(PathBuf::from(out_file)) } else { None }; watch_arg_parse(flags, matches, false); flags.subcommand = DenoSubcommand::Bundle(BundleFlags { source_file, out_file, });}
fn cache_parse(flags: &mut Flags, matches: &clap::ArgMatches) { compile_args_parse(flags, matches); let files = matches .values_of("file") .unwrap() .map(String::from) .collect(); flags.subcommand = DenoSubcommand::Cache(CacheFlags { files });}
fn check_parse(flags: &mut Flags, matches: &clap::ArgMatches) { compile_args_without_no_check_parse(flags, matches); let files = matches .values_of("file") .unwrap() .map(String::from) .collect(); let remote = matches.is_present("remote"); flags.subcommand = DenoSubcommand::Check(CheckFlags { files, remote });}
fn compile_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, false); let mut script: Vec<String> = matches .values_of("script_arg") .unwrap() .map(String::from) .collect(); assert!(!script.is_empty()); let args = script.split_off(1); let source_file = script[0].to_string(); let output = matches.value_of("output").map(PathBuf::from); let target = matches.value_of("target").map(String::from); flags.subcommand = DenoSubcommand::Compile(CompileFlags { source_file, output, args, target, });}
fn completions_parse( flags: &mut Flags, matches: &clap::ArgMatches, mut app: clap::Command,) { use clap_complete::generate; use clap_complete::shells::{Bash, Fish, PowerShell, Zsh}; use clap_complete_fig::Fig; let mut buf: Vec<u8> = vec![]; let name = "deno"; match matches.value_of("shell").unwrap() { "bash" => generate(Bash, &mut app, name, &mut buf), "fish" => generate(Fish, &mut app, name, &mut buf), "powershell" => generate(PowerShell, &mut app, name, &mut buf), "zsh" => generate(Zsh, &mut app, name, &mut buf), "fig" => generate(Fig, &mut app, name, &mut buf), _ => unreachable!(), } flags.subcommand = DenoSubcommand::Completions(CompletionsFlags { buf: buf.into_boxed_slice(), });}
fn coverage_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let files = match matches.values_of("files") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ignore = match matches.values_of("ignore") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let include = match matches.values_of("include") { Some(f) => f.map(String::from).collect(), None => vec![], }; let exclude = match matches.values_of("exclude") { Some(f) => f.map(String::from).collect(), None => vec![], }; let lcov = matches.is_present("lcov"); let output = matches.value_of("output").map(PathBuf::from); flags.subcommand = DenoSubcommand::Coverage(CoverageFlags { files, output, ignore, include, exclude, lcov, });}
fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); reload_arg_parse(flags, matches); let source_file = matches.value_of("source_file").map(String::from); let private = matches.is_present("private"); let json = matches.is_present("json"); let filter = matches.value_of("filter").map(String::from); flags.subcommand = DenoSubcommand::Doc(DocFlags { source_file, json, filter, private, });}
fn eval_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, false, true); flags.allow_net = Some(vec![]); flags.allow_env = Some(vec![]); flags.allow_run = Some(vec![]); flags.allow_read = Some(vec![]); flags.allow_write = Some(vec![]); flags.allow_ffi = Some(vec![]); flags.allow_hrtime = true; // TODO(@satyarohith): remove this flag in 2.0. let as_typescript = matches.is_present("ts"); let ext = if as_typescript { "ts".to_string() } else { matches.value_of("ext").unwrap().to_string() }; let print = matches.is_present("print"); let mut code: Vec<String> = matches .values_of("code_arg") .unwrap() .map(String::from) .collect(); assert!(!code.is_empty()); let code_args = code.split_off(1); let code = code[0].to_string(); for v in code_args { flags.argv.push(v); } flags.subcommand = DenoSubcommand::Eval(EvalFlags { print, code, ext });}
fn fmt_parse(flags: &mut Flags, matches: &clap::ArgMatches) { config_arg_parse(flags, matches); watch_arg_parse(flags, matches, false); let files = match matches.values_of("files") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ignore = match matches.values_of("ignore") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ext = matches.value_of("ext").unwrap().to_string(); let use_tabs = if matches.is_present("options-use-tabs") { Some(true) } else { None }; let line_width = if matches.is_present("options-line-width") { Some( matches .value_of("options-line-width") .unwrap() .parse() .unwrap(), ) } else { None }; let indent_width = if matches.is_present("options-indent-width") { Some( matches .value_of("options-indent-width") .unwrap() .parse() .unwrap(), ) } else { None }; let single_quote = if matches.is_present("options-single-quote") { Some(true) } else { None }; let prose_wrap = if matches.is_present("options-prose-wrap") { Some(matches.value_of("options-prose-wrap").unwrap().to_string()) } else { None }; flags.subcommand = DenoSubcommand::Fmt(FmtFlags { check: matches.is_present("check"), ext, files, ignore, use_tabs, line_width, indent_width, single_quote, prose_wrap, });}
fn info_parse(flags: &mut Flags, matches: &clap::ArgMatches) { reload_arg_parse(flags, matches); import_map_arg_parse(flags, matches); location_arg_parse(flags, matches); ca_file_arg_parse(flags, matches); let json = matches.is_present("json"); flags.subcommand = DenoSubcommand::Info(InfoFlags { file: matches.value_of("file").map(|f| f.to_string()), json, });}
fn install_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); let root = if matches.is_present("root") { let install_root = matches.value_of("root").unwrap(); Some(PathBuf::from(install_root)) } else { None }; let force = matches.is_present("force"); let name = matches.value_of("name").map(|s| s.to_string()); let cmd_values = matches.values_of("cmd").unwrap(); let mut cmd = vec![]; for value in cmd_values { cmd.push(value.to_string()); } let module_url = cmd[0].to_string(); let args = cmd[1..].to_vec(); flags.subcommand = DenoSubcommand::Install(InstallFlags { name, module_url, args, root, force, });}
fn uninstall_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let root = if matches.is_present("root") { let install_root = matches.value_of("root").unwrap(); Some(PathBuf::from(install_root)) } else { None }; let name = matches.value_of("name").unwrap().to_string(); flags.subcommand = DenoSubcommand::Uninstall(UninstallFlags { name, root });}
fn lsp_parse(flags: &mut Flags, _matches: &clap::ArgMatches) { flags.subcommand = DenoSubcommand::Lsp;}
fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) { config_arg_parse(flags, matches); watch_arg_parse(flags, matches, false); let files = match matches.values_of("files") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let ignore = match matches.values_of("ignore") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let rules = matches.is_present("rules"); let maybe_rules_tags = matches .values_of("rules-tags") .map(|f| f.map(String::from).collect()); let maybe_rules_include = matches .values_of("rules-include") .map(|f| f.map(String::from).collect()); let maybe_rules_exclude = matches .values_of("rules-exclude") .map(|f| f.map(String::from).collect()); let json = matches.is_present("json"); flags.subcommand = DenoSubcommand::Lint(LintFlags { files, rules, maybe_rules_tags, maybe_rules_include, maybe_rules_exclude, ignore, json, });}
fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) { // Use no-check by default for the REPL flags.type_check_mode = TypeCheckMode::None; runtime_args_parse(flags, matches, false, true); unsafely_ignore_certificate_errors_parse(flags, matches); let eval_files: Option<Vec<String>> = matches .values_of("eval-file") .map(|values| values.map(String::from).collect()); handle_repl_flags( flags, ReplFlags { eval_files, eval: matches.value_of("eval").map(ToOwned::to_owned), }, );}
fn run_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); check_arg_parse(flags, matches); let mut script: Vec<String> = matches .values_of("script_arg") .unwrap() .map(String::from) .collect(); assert!(!script.is_empty()); let script_args = script.split_off(1); let script = script[0].to_string(); for v in script_args { flags.argv.push(v); } watch_arg_parse(flags, matches, true); flags.subcommand = DenoSubcommand::Run(RunFlags { script });}
fn task_parse( flags: &mut Flags, matches: &clap::ArgMatches, raw_args: &[String],) { config_arg_parse(flags, matches); let mut task_name = "".to_string(); if let Some(mut index) = matches.index_of("task_name_and_args") { index += 1; // skip `task` // temporary workaround until https://github.com/clap-rs/clap/issues/1538 is fixed while index < raw_args.len() { match raw_args[index].as_str() { "-c" | "--config" => { flags.config_path = Some(raw_args[index + 1].to_string()); index += 2; } "-q" | "--quiet" => { flags.log_level = Some(Level::Error); index += 1; } _ => break, } } if index < raw_args.len() { task_name = raw_args[index].to_string(); index += 1; if index < raw_args.len() { flags .argv .extend(raw_args[index..].iter().map(String::from)); } } } flags.subcommand = DenoSubcommand::Task(TaskFlags { task: task_name });}
fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); // NOTE: `deno test` always uses `--no-prompt`, tests shouldn't ever do // interactive prompts, unless done by user code flags.no_prompt = true; let ignore = match matches.values_of("ignore") { Some(f) => f.map(PathBuf::from).collect(), None => vec![], }; let no_run = matches.is_present("no-run"); let trace_ops = matches.is_present("trace-ops"); let doc = matches.is_present("doc"); let allow_none = matches.is_present("allow-none"); let filter = matches.value_of("filter").map(String::from); let fail_fast = if matches.is_present("fail-fast") { if let Some(value) = matches.value_of("fail-fast") { Some(value.parse().unwrap()) } else { Some(NonZeroUsize::new(1).unwrap()) } } else { None }; let shuffle = if matches.is_present("shuffle") { let value = if let Some(value) = matches.value_of("shuffle") { value.parse::<u64>().unwrap() } else { rand::random::<u64>() }; Some(value) } else { None }; if matches.is_present("script_arg") { let script_arg: Vec<String> = matches .values_of("script_arg") .unwrap() .map(String::from) .collect(); for v in script_arg { flags.argv.push(v); } } let concurrent_jobs = if matches.is_present("jobs") { if let Some(value) = matches.value_of("jobs") { value.parse().unwrap() } else { std::thread::available_parallelism() .unwrap_or(NonZeroUsize::new(1).unwrap()) } } else { NonZeroUsize::new(1).unwrap() }; let include = if matches.is_present("files") { let files: Vec<String> = matches .values_of("files") .unwrap() .map(String::from) .collect(); Some(files) } else { None }; flags.coverage_dir = matches.value_of("coverage").map(String::from); watch_arg_parse(flags, matches, false); flags.subcommand = DenoSubcommand::Test(TestFlags { no_run, doc, fail_fast, include, ignore, filter, shuffle, allow_none, concurrent_jobs, trace_ops, });}
fn types_parse(flags: &mut Flags, _matches: &clap::ArgMatches) { flags.subcommand = DenoSubcommand::Types;}
fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) { ca_file_arg_parse(flags, matches); let dry_run = matches.is_present("dry-run"); let force = matches.is_present("force"); let canary = matches.is_present("canary"); let version = matches.value_of("version").map(|s| s.to_string()); let output = if matches.is_present("output") { let install_root = matches.value_of("output").unwrap(); Some(PathBuf::from(install_root)) } else { None }; let ca_file = matches.value_of("cert").map(|s| s.to_string()); flags.subcommand = DenoSubcommand::Upgrade(UpgradeFlags { dry_run, force, canary, version, output, ca_file, });}
fn vendor_parse(flags: &mut Flags, matches: &clap::ArgMatches) { ca_file_arg_parse(flags, matches); config_arg_parse(flags, matches); import_map_arg_parse(flags, matches); lock_arg_parse(flags, matches); reload_arg_parse(flags, matches); flags.subcommand = DenoSubcommand::Vendor(VendorFlags { specifiers: matches .values_of("specifiers") .map(|p| p.map(ToString::to_string).collect()) .unwrap_or_default(), output_path: matches.value_of("output").map(PathBuf::from), force: matches.is_present("force"), });}
fn compile_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); no_remote_arg_parse(flags, matches); config_arg_parse(flags, matches); no_check_arg_parse(flags, matches); reload_arg_parse(flags, matches); lock_args_parse(flags, matches); ca_file_arg_parse(flags, matches);}
fn compile_args_without_no_check_parse( flags: &mut Flags, matches: &clap::ArgMatches,) { import_map_arg_parse(flags, matches); no_remote_arg_parse(flags, matches); config_arg_parse(flags, matches); reload_arg_parse(flags, matches); lock_args_parse(flags, matches); ca_file_arg_parse(flags, matches);}
fn permission_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { unsafely_ignore_certificate_errors_parse(flags, matches); if let Some(read_wl) = matches.values_of("allow-read") { let read_allowlist: Vec<PathBuf> = read_wl.map(PathBuf::from).collect(); flags.allow_read = Some(read_allowlist); } if let Some(write_wl) = matches.values_of("allow-write") { let write_allowlist: Vec<PathBuf> = write_wl.map(PathBuf::from).collect(); flags.allow_write = Some(write_allowlist); } if let Some(net_wl) = matches.values_of("allow-net") { let net_allowlist: Vec<String> = crate::flags_allow_net::parse(net_wl.map(ToString::to_string).collect()) .unwrap(); flags.allow_net = Some(net_allowlist); } if let Some(env_wl) = matches.values_of("allow-env") { let env_allowlist: Vec<String> = env_wl .map(|env: &str| { if cfg!(windows) { env.to_uppercase() } else { env.to_string() } }) .collect(); flags.allow_env = Some(env_allowlist); debug!("env allowlist: {:#?}", &flags.allow_env); } if let Some(run_wl) = matches.values_of("allow-run") { let run_allowlist: Vec<String> = run_wl.map(ToString::to_string).collect(); flags.allow_run = Some(run_allowlist); debug!("run allowlist: {:#?}", &flags.allow_run); } if let Some(ffi_wl) = matches.values_of("allow-ffi") { let ffi_allowlist: Vec<PathBuf> = ffi_wl.map(PathBuf::from).collect(); flags.allow_ffi = Some(ffi_allowlist); debug!("ffi allowlist: {:#?}", &flags.allow_ffi); } if matches.is_present("allow-hrtime") { flags.allow_hrtime = true; } if matches.is_present("allow-all") { flags.allow_all = true; flags.allow_read = Some(vec![]); flags.allow_env = Some(vec![]); flags.allow_net = Some(vec![]); flags.allow_run = Some(vec![]); flags.allow_write = Some(vec![]); flags.allow_ffi = Some(vec![]); flags.allow_hrtime = true; } #[cfg(not(test))] let has_no_prompt_env = env::var("DENO_NO_PROMPT") == Ok("1".to_string()); #[cfg(test)] let has_no_prompt_env = false; if has_no_prompt_env || matches.is_present("no-prompt") { flags.no_prompt = true; }}fn unsafely_ignore_certificate_errors_parse( flags: &mut Flags, matches: &clap::ArgMatches,) { if let Some(ic_wl) = matches.values_of("unsafely-ignore-certificate-errors") { let ic_allowlist: Vec<String> = crate::flags_allow_net::parse(ic_wl.map(ToString::to_string).collect()) .unwrap(); flags.unsafely_ignore_certificate_errors = Some(ic_allowlist); }}fn runtime_args_parse( flags: &mut Flags, matches: &clap::ArgMatches, include_perms: bool, include_inspector: bool,) { compile_args_parse(flags, matches); cached_only_arg_parse(flags, matches); if include_perms { permission_args_parse(flags, matches); } if include_inspector { inspect_arg_parse(flags, matches); } location_arg_parse(flags, matches); v8_flags_arg_parse(flags, matches); seed_arg_parse(flags, matches); compat_arg_parse(flags, matches); enable_testing_features_arg_parse(flags, matches);}
fn inspect_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { let default = || "127.0.0.1:9229".parse::<SocketAddr>().unwrap(); flags.inspect = if matches.is_present("inspect") { if let Some(host) = matches.value_of("inspect") { Some(host.parse().unwrap()) } else { Some(default()) } } else { None }; flags.inspect_brk = if matches.is_present("inspect-brk") { if let Some(host) = matches.value_of("inspect-brk") { Some(host.parse().unwrap()) } else { Some(default()) } } else { None };}
fn import_map_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.import_map_path = matches.value_of("import-map").map(ToOwned::to_owned);}
fn reload_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if let Some(cache_bl) = matches.values_of("reload") { let raw_cache_blocklist: Vec<String> = cache_bl.map(ToString::to_string).collect(); if raw_cache_blocklist.is_empty() { flags.reload = true; } else { flags.cache_blocklist = resolve_urls(raw_cache_blocklist); debug!("cache blocklist: {:#?}", &flags.cache_blocklist); flags.reload = false; } }}
fn ca_file_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.ca_file = matches.value_of("cert").map(ToOwned::to_owned);}
fn enable_testing_features_arg_parse( flags: &mut Flags, matches: &clap::ArgMatches,) { if matches.is_present("enable-testing-features-do-not-use") { flags.enable_testing_features = true }}
fn cached_only_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if matches.is_present("cached-only") { flags.cached_only = true; }}
fn location_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.location = matches .value_of("location") .map(|href| Url::parse(href).unwrap());}
fn v8_flags_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if let Some(v8_flags) = matches.values_of("v8-flags") { flags.v8_flags = v8_flags.map(String::from).collect(); }}
fn seed_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if matches.is_present("seed") { let seed_string = matches.value_of("seed").unwrap(); let seed = seed_string.parse::<u64>().unwrap(); flags.seed = Some(seed); flags.v8_flags.push(format!("--random-seed={}", seed)); }}
fn compat_arg_parse(flags: &mut Flags, matches: &ArgMatches) { if matches.is_present("compat") { flags.compat = true; }}
fn no_check_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { if let Some(cache_type) = matches.value_of("no-check") { match cache_type { "remote" => flags.type_check_mode = TypeCheckMode::Local, _ => debug!( "invalid value for 'no-check' of '{}' using default", cache_type ), } } else if matches.is_present("no-check") { flags.type_check_mode = TypeCheckMode::None; }}
fn check_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { flags.has_check_flag = matches.is_present("check"); if let Some(cache_type) = matches.value_of("check") { match cache_type { "all" => flags.future_type_check_mode = FutureTypeCheckMode::All, _ => debug!( "invalid value for 'check' of '{}' using default", cache_type ), } } else if matches.is_present("check") { flags.future_type_check_mode = FutureTypeCheckMode::Local; }}
fn lock_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { lock_arg_parse(flags, matches); if matches.is_present("lock-write") { flags.lock_write = true; }}
fn lock_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { if matches.is_present("lock") { let lockfile = matches.value_of("lock").unwrap(); flags.lock = Some(PathBuf::from(lockfile)); }}
fn config_arg_parse(flags: &mut Flags, matches: &ArgMatches) { flags.config_path = matches.value_of("config").map(ToOwned::to_owned);}
fn no_remote_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { if matches.is_present("no-remote") { flags.no_remote = true; }}
fn inspect_arg_validate(val: &str) -> Result<(), String> { match val.parse::<SocketAddr>() { Ok(_) => Ok(()), Err(e) => Err(e.to_string()), }}
fn watch_arg_parse( flags: &mut Flags, matches: &clap::ArgMatches, allow_extra: bool,) { if allow_extra { if let Some(f) = matches.values_of("watch") { flags.watch = Some(f.map(PathBuf::from).collect()); } } else if matches.is_present("watch") { flags.watch = Some(vec![]); } if matches.is_present("no-clear-screen") { flags.no_clear_screen = true; }}
// TODO(ry) move this to utility module and add test./// Strips fragment part of URL. Panics on bad URL.pub fn resolve_urls(urls: Vec<String>) -> Vec<String> { let mut out: Vec<String> = vec![]; for urlstr in urls.iter() { if let Ok(mut url) = Url::from_str(urlstr) { url.set_fragment(None); let mut full_url = String::from(url.as_str()); if full_url.len() > 1 && full_url.ends_with('/') { full_url.pop(); } out.push(full_url); } else { panic!("Bad Url: {}", urlstr); } } out}
#[cfg(test)]mod tests { use super::*; use pretty_assertions::assert_eq; /// Creates vector of strings, Vec<String> macro_rules! svec { ($($x:expr),* $(,)?) => (vec![$($x.to_string()),*]); } #[test] fn global_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "--unstable", "--log-level", "debug", "--quiet", "run", "script.ts"]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), unstable: true, log_level: Some(Level::Error), ..Flags::default() } ); #[rustfmt::skip] let r2 = flags_from_vec(svec!["deno", "run", "--unstable", "--log-level", "debug", "--quiet", "script.ts"]); let flags2 = r2.unwrap(); assert_eq!(flags2, flags); } #[test] fn upgrade() { let r = flags_from_vec(svec!["deno", "upgrade", "--dry-run", "--force"]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Upgrade(UpgradeFlags { force: true, dry_run: true, canary: false, version: None, output: None, ca_file: None, }), ..Flags::default() } ); } #[test] fn version() { let r = flags_from_vec(svec!["deno", "--version"]); assert_eq!(r.unwrap_err().kind(), clap::ErrorKind::DisplayVersion); let r = flags_from_vec(svec!["deno", "-V"]); assert_eq!(r.unwrap_err().kind(), clap::ErrorKind::DisplayVersion); } #[test] fn run_reload() { let r = flags_from_vec(svec!["deno", "run", "-r", "script.ts"]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), reload: true, ..Flags::default() } ); } #[test] fn run_watch() { let r = flags_from_vec(svec!["deno", "run", "--watch", "script.ts"]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), watch: Some(vec![]), ..Flags::default() } ); } #[test] fn run_watch_with_external() { let r = flags_from_vec(svec!["deno", "run", "--watch=file1,file2", "script.ts"]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), watch: Some(vec![PathBuf::from("file1"), PathBuf::from("file2")]), ..Flags::default() } ); } #[test] fn run_watch_with_no_clear_screen() { let r = flags_from_vec(svec![ "deno", "run", "--watch", "--no-clear-screen", "script.ts" ]); let flags = r.unwrap(); assert_eq!( flags, Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), watch: Some(vec![]), no_clear_screen: true, ..Flags::default() } ); } #[test] fn run_reload_allow_write() { let r = flags_from_vec(svec!["deno", "run", "-r", "--allow-write", "script.ts"]); assert_eq!( r.unwrap(), Flags { reload: true, subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_write: Some(vec![]), ..Flags::default() } ); } #[test] fn run_v8_flags() { let r = flags_from_vec(svec!["deno", "run", "--v8-flags=--help"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "_".to_string(), }), v8_flags: svec!["--help"], ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "run", "--v8-flags=--expose-gc,--gc-stats=1", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), v8_flags: svec!["--expose-gc", "--gc-stats=1"], ..Flags::default() } ); } #[test] fn script_args() { let r = flags_from_vec(svec![ "deno", "run", "--allow-net", "gist.ts", "--title", "X" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), }), argv: svec!["--title", "X"], allow_net: Some(vec![]), ..Flags::default() } ); } #[test] fn allow_all() { let r = flags_from_vec(svec!["deno", "run", "--allow-all", "gist.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), }), allow_all: true, allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn allow_read() { let r = flags_from_vec(svec!["deno", "run", "--allow-read", "gist.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), }), allow_read: Some(vec![]), ..Flags::default() } ); } #[test] fn allow_hrtime() { let r = flags_from_vec(svec!["deno", "run", "--allow-hrtime", "gist.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "gist.ts".to_string(), }), allow_hrtime: true, ..Flags::default() } ); } #[test] fn double_hyphen() { // notice that flags passed after double dash will not // be parsed to Flags but instead forwarded to // script args as Deno.args let r = flags_from_vec(svec![ "deno", "run", "--allow-write", "script.ts", "--", "-D", "--allow-net" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), argv: svec!["--", "-D", "--allow-net"], allow_write: Some(vec![]), ..Flags::default() } ); } #[test] fn fmt() { let r = flags_from_vec(svec!["deno", "fmt", "script_1.ts", "script_2.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "fmt", "--check"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: true, files: vec![], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "fmt"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "fmt", "--watch"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), watch: Some(vec![]), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "fmt", "--watch", "--no-clear-screen"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), watch: Some(vec![]), no_clear_screen: true, ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "fmt", "--check", "--watch", "foo.ts", "--ignore=bar.js" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![PathBuf::from("bar.js")], check: true, files: vec![PathBuf::from("foo.ts")], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), watch: Some(vec![]), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "fmt", "--config", "deno.jsonc"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), config_path: Some("deno.jsonc".to_string()), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "fmt", "--config", "deno.jsonc", "--watch", "foo.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![PathBuf::from("foo.ts")], ext: "ts".to_string(), use_tabs: None, line_width: None, indent_width: None, single_quote: None, prose_wrap: None, }), config_path: Some("deno.jsonc".to_string()), watch: Some(vec![]), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "fmt", "--options-use-tabs", "--options-line-width", "60", "--options-indent-width", "4", "--options-single-quote", "--options-prose-wrap", "never" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Fmt(FmtFlags { ignore: vec![], check: false, files: vec![], ext: "ts".to_string(), use_tabs: Some(true), line_width: Some(NonZeroU32::new(60).unwrap()), indent_width: Some(NonZeroU8::new(4).unwrap()), single_quote: Some(true), prose_wrap: Some("never".to_string()), }), ..Flags::default() } ); } #[test] fn lint() { let r = flags_from_vec(svec!["deno", "lint", "script_1.ts", "script_2.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: false, ignore: vec![], }), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "lint", "--watch", "script_1.ts", "script_2.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: false, ignore: vec![], }), watch: Some(vec![]), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "lint", "--watch", "--no-clear-screen", "script_1.ts", "script_2.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: false, ignore: vec![], }), watch: Some(vec![]), no_clear_screen: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "lint", "--ignore=script_1.ts,script_2.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: false, ignore: vec![ PathBuf::from("script_1.ts"), PathBuf::from("script_2.ts") ], }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "lint", "--rules"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: true, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: false, ignore: vec![], }), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "lint", "--rules-tags=", "--rules-include=ban-untagged-todo,no-undef", "--rules-exclude=no-const-assign" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![], rules: false, maybe_rules_tags: Some(svec![""]), maybe_rules_include: Some(svec!["ban-untagged-todo", "no-undef"]), maybe_rules_exclude: Some(svec!["no-const-assign"]), json: false, ignore: vec![], }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "lint", "--json", "script_1.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![PathBuf::from("script_1.ts")], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: true, ignore: vec![], }), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "lint", "--config", "Deno.jsonc", "--json", "script_1.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint(LintFlags { files: vec![PathBuf::from("script_1.ts")], rules: false, maybe_rules_tags: None, maybe_rules_include: None, maybe_rules_exclude: None, json: true, ignore: vec![], }), config_path: Some("Deno.jsonc".to_string()), ..Flags::default() } ); } #[test] fn types() { let r = flags_from_vec(svec!["deno", "types"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Types, ..Flags::default() } ); } #[test] fn cache() { let r = flags_from_vec(svec!["deno", "cache", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts"], }), ..Flags::default() } ); } #[test] fn check() { let r = flags_from_vec(svec!["deno", "check", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Check(CheckFlags { files: svec!["script.ts"], remote: false, }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "check", "--remote", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Check(CheckFlags { files: svec!["script.ts"], remote: true, }), ..Flags::default() } ); } #[test] fn info() { let r = flags_from_vec(svec!["deno", "info", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("script.ts".to_string()), }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "info", "--reload", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("script.ts".to_string()), }), reload: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "info", "--json", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: true, file: Some("script.ts".to_string()), }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "info"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: None }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "info", "--json"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: true, file: None }), ..Flags::default() } ); } #[test] fn tsconfig() { let r = flags_from_vec(svec!["deno", "run", "-c", "tsconfig.json", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), config_path: Some("tsconfig.json".to_owned()), ..Flags::default() } ); } #[test] fn eval() { let r = flags_from_vec(svec!["deno", "eval", "'console.log(\"hello\")'"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "'console.log(\"hello\")'".to_string(), ext: "js".to_string(), }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn eval_p() { let r = flags_from_vec(svec!["deno", "eval", "-p", "1+2"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Eval(EvalFlags { print: true, code: "1+2".to_string(), ext: "js".to_string(), }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn eval_typescript() { let r = flags_from_vec(svec!["deno", "eval", "-T", "'console.log(\"hello\")'"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "'console.log(\"hello\")'".to_string(), ext: "ts".to_string(), }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn eval_with_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "eval", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--location", "https:foo", "--v8-flags=--help", "--seed", "1", "--inspect=127.0.0.1:9229", "42"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "42".to_string(), ext: "js".to_string(), }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), type_check_mode: TypeCheckMode::None, reload: true, lock: Some(PathBuf::from("lock.json")), lock_write: true, ca_file: Some("example.crt".to_string()), cached_only: true, location: Some(Url::parse("https://foo/").unwrap()), v8_flags: svec!["--help", "--random-seed=1"], seed: Some(1), inspect: Some("127.0.0.1:9229".parse().unwrap()), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn eval_args() { let r = flags_from_vec(svec![ "deno", "eval", "console.log(Deno.args)", "arg1", "arg2" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Eval(EvalFlags { print: false, code: "console.log(Deno.args)".to_string(), ext: "js".to_string(), }), argv: svec!["arg1", "arg2"], allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn repl() { let r = flags_from_vec(svec!["deno"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: None }), allow_net: Some(vec![]), unsafely_ignore_certificate_errors: None, allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, ..Flags::default() } ); } #[test] fn repl_with_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "repl", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--location", "https:foo", "--v8-flags=--help", "--seed", "1", "--inspect=127.0.0.1:9229", "--unsafely-ignore-certificate-errors"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: None }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), type_check_mode: TypeCheckMode::None, reload: true, lock: Some(PathBuf::from("lock.json")), lock_write: true, ca_file: Some("example.crt".to_string()), cached_only: true, location: Some(Url::parse("https://foo/").unwrap()), v8_flags: svec!["--help", "--random-seed=1"], seed: Some(1), inspect: Some("127.0.0.1:9229".parse().unwrap()), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, unsafely_ignore_certificate_errors: Some(vec![]), ..Flags::default() } ); } #[test] fn repl_with_eval_flag() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "repl", "--eval", "console.log('hello');"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: Some("console.log('hello');".to_string()), }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn repl_with_eval_file_flag() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "repl", "--eval-file=./a.js,./b.ts,https://examples.deno.land/hello-world.ts"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: Some(vec![ "./a.js".to_string(), "./b.ts".to_string(), "https://examples.deno.land/hello-world.ts".to_string() ]), eval: None, }), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn allow_read_allowlist() { use test_util::TempDir; let temp_dir_guard = TempDir::new(); let temp_dir = temp_dir_guard.path().to_path_buf(); let r = flags_from_vec(svec![ "deno", "run", format!("--allow-read=.,{}", temp_dir.to_str().unwrap()), "script.ts" ]); assert_eq!( r.unwrap(), Flags { allow_read: Some(vec![PathBuf::from("."), temp_dir]), subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), ..Flags::default() } ); } #[test] fn allow_write_allowlist() { use test_util::TempDir; let temp_dir_guard = TempDir::new(); let temp_dir = temp_dir_guard.path().to_path_buf(); let r = flags_from_vec(svec![ "deno", "run", format!("--allow-write=.,{}", temp_dir.to_str().unwrap()), "script.ts" ]); assert_eq!( r.unwrap(), Flags { allow_write: Some(vec![PathBuf::from("."), temp_dir]), subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), ..Flags::default() } ); } #[test] fn allow_net_allowlist() { let r = flags_from_vec(svec![ "deno", "run", "--allow-net=127.0.0.1", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_net: Some(svec!["127.0.0.1"]), ..Flags::default() } ); } #[test] fn allow_env_allowlist() { let r = flags_from_vec(svec!["deno", "run", "--allow-env=HOME", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_env: Some(svec!["HOME"]), ..Flags::default() } ); } #[test] fn allow_env_allowlist_multiple() { let r = flags_from_vec(svec![ "deno", "run", "--allow-env=HOME,PATH", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_env: Some(svec!["HOME", "PATH"]), ..Flags::default() } ); } #[test] fn allow_env_allowlist_validator() { let r = flags_from_vec(svec!["deno", "run", "--allow-env=HOME", "script.ts"]); assert!(r.is_ok()); let r = flags_from_vec(svec!["deno", "run", "--allow-env=H=ME", "script.ts"]); assert!(r.is_err()); let r = flags_from_vec(svec!["deno", "run", "--allow-env=H\0ME", "script.ts"]); assert!(r.is_err()); } #[test] fn bundle() { let r = flags_from_vec(svec!["deno", "bundle", "source.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), ..Flags::default() } ); } #[test] fn bundle_with_config() { let r = flags_from_vec(svec![ "deno", "bundle", "--no-remote", "--config", "tsconfig.json", "source.ts", "bundle.js" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: Some(PathBuf::from("bundle.js")), }), allow_write: Some(vec![]), no_remote: true, config_path: Some("tsconfig.json".to_owned()), ..Flags::default() } ); } #[test] fn bundle_with_output() { let r = flags_from_vec(svec!["deno", "bundle", "source.ts", "bundle.js"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: Some(PathBuf::from("bundle.js")), }), allow_write: Some(vec![]), ..Flags::default() } ); } #[test] fn bundle_with_lock() { let r = flags_from_vec(svec![ "deno", "bundle", "--lock-write", "--lock=lock.json", "source.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), lock_write: true, lock: Some(PathBuf::from("lock.json")), ..Flags::default() } ); } #[test] fn bundle_with_reload() { let r = flags_from_vec(svec!["deno", "bundle", "--reload", "source.ts"]); assert_eq!( r.unwrap(), Flags { reload: true, subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), ..Flags::default() } ); } #[test] fn bundle_nocheck() { let r = flags_from_vec(svec!["deno", "bundle", "--no-check", "script.ts"]) .unwrap(); assert_eq!( r, Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "script.ts".to_string(), out_file: None, }), type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn bundle_watch() { let r = flags_from_vec(svec!["deno", "bundle", "--watch", "source.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), watch: Some(vec![]), ..Flags::default() } ) } #[test] fn bundle_watch_with_no_clear_screen() { let r = flags_from_vec(svec![ "deno", "bundle", "--watch", "--no-clear-screen", "source.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), watch: Some(vec![]), no_clear_screen: true, ..Flags::default() } ) } #[test] fn run_import_map() { let r = flags_from_vec(svec![ "deno", "run", "--import-map=import_map.json", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } ); } #[test] fn info_import_map() { let r = flags_from_vec(svec![ "deno", "info", "--import-map=import_map.json", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { file: Some("script.ts".to_string()), json: false, }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } ); } #[test] fn cache_import_map() { let r = flags_from_vec(svec![ "deno", "cache", "--import-map=import_map.json", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts"], }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } ); } #[test] fn doc_import_map() { let r = flags_from_vec(svec![ "deno", "doc", "--import-map=import_map.json", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { source_file: Some("script.ts".to_owned()), private: false, json: false, filter: None, }), import_map_path: Some("import_map.json".to_owned()), ..Flags::default() } ); } #[test] fn cache_multiple() { let r = flags_from_vec(svec!["deno", "cache", "script.ts", "script_two.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts", "script_two.ts"], }), ..Flags::default() } ); } #[test] fn run_seed() { let r = flags_from_vec(svec!["deno", "run", "--seed", "250", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), seed: Some(250_u64), v8_flags: svec!["--random-seed=250"], ..Flags::default() } ); } #[test] fn run_seed_with_v8_flags() { let r = flags_from_vec(svec![ "deno", "run", "--seed", "250", "--v8-flags=--expose-gc", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), seed: Some(250_u64), v8_flags: svec!["--expose-gc", "--random-seed=250"], ..Flags::default() } ); } #[test] fn install() { let r = flags_from_vec(svec![ "deno", "install", "https://deno.land/std/examples/colors.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Install(InstallFlags { name: None, module_url: "https://deno.land/std/examples/colors.ts".to_string(), args: vec![], root: None, force: false, }), ..Flags::default() } ); } #[test] fn install_with_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "install", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--unsafely-ignore-certificate-errors", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--allow-read", "--allow-net", "--v8-flags=--help", "--seed", "1", "--inspect=127.0.0.1:9229", "--name", "file_server", "--root", "/foo", "--force", "https://deno.land/std/http/file_server.ts", "foo", "bar"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Install(InstallFlags { name: Some("file_server".to_string()), module_url: "https://deno.land/std/http/file_server.ts".to_string(), args: svec!["foo", "bar"], root: Some(PathBuf::from("/foo")), force: true, }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), type_check_mode: TypeCheckMode::None, reload: true, lock: Some(PathBuf::from("lock.json")), lock_write: true, ca_file: Some("example.crt".to_string()), cached_only: true, v8_flags: svec!["--help", "--random-seed=1"], seed: Some(1), inspect: Some("127.0.0.1:9229".parse().unwrap()), allow_net: Some(vec![]), unsafely_ignore_certificate_errors: Some(vec![]), allow_read: Some(vec![]), ..Flags::default() } ); } #[test] fn log_level() { let r = flags_from_vec(svec!["deno", "run", "--log-level=debug", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), log_level: Some(Level::Debug), ..Flags::default() } ); } #[test] fn quiet() { let r = flags_from_vec(svec!["deno", "run", "-q", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), log_level: Some(Level::Error), ..Flags::default() } ); } #[test] fn completions() { let r = flags_from_vec(svec!["deno", "completions", "zsh"]).unwrap(); match r.subcommand { DenoSubcommand::Completions(CompletionsFlags { buf }) => { assert!(!buf.is_empty()) } _ => unreachable!(), } } #[test] fn run_with_args() { let r = flags_from_vec(svec![ "deno", "run", "script.ts", "--allow-read", "--allow-net" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), argv: svec!["--allow-read", "--allow-net"], ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "run", "--location", "https:foo", "--allow-read", "script.ts", "--allow-net", "-r", "--help", "--foo", "bar" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), location: Some(Url::parse("https://foo/").unwrap()), allow_read: Some(vec![]), argv: svec!["--allow-net", "-r", "--help", "--foo", "bar"], ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "run", "script.ts", "foo", "bar"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), argv: svec!["foo", "bar"], ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "run", "script.ts", "-"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), argv: svec!["-"], ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "run", "script.ts", "-", "foo", "bar"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), argv: svec!["-", "foo", "bar"], ..Flags::default() } ); } #[test] fn no_check() { let r = flags_from_vec(svec!["deno", "run", "--no-check", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn no_check_remote() { let r = flags_from_vec(svec!["deno", "run", "--no-check=remote", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), type_check_mode: TypeCheckMode::Local, ..Flags::default() } ); } #[test] fn repl_with_unsafely_ignore_certificate_errors() { let r = flags_from_vec(svec![ "deno", "repl", "--eval", "console.log('hello');", "--unsafely-ignore-certificate-errors" ]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: Some("console.log('hello');".to_string()), }), unsafely_ignore_certificate_errors: Some(vec![]), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn run_with_unsafely_ignore_certificate_errors() { let r = flags_from_vec(svec![ "deno", "run", "--unsafely-ignore-certificate-errors", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), unsafely_ignore_certificate_errors: Some(vec![]), ..Flags::default() } ); } #[test] fn run_with_unsafely_treat_insecure_origin_as_secure_with_ipv6_address() { let r = flags_from_vec(svec![ "deno", "run", "--unsafely-ignore-certificate-errors=deno.land,localhost,::,127.0.0.1,[::1],1.2.3.4", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), unsafely_ignore_certificate_errors: Some(svec![ "deno.land", "localhost", "::", "127.0.0.1", "[::1]", "1.2.3.4" ]), ..Flags::default() } ); } #[test] fn repl_with_unsafely_treat_insecure_origin_as_secure_with_ipv6_address() { let r = flags_from_vec(svec![ "deno", "repl", "--unsafely-ignore-certificate-errors=deno.land,localhost,::,127.0.0.1,[::1],1.2.3.4"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval_files: None, eval: None }), unsafely_ignore_certificate_errors: Some(svec![ "deno.land", "localhost", "::", "127.0.0.1", "[::1]", "1.2.3.4" ]), allow_net: Some(vec![]), allow_env: Some(vec![]), allow_run: Some(vec![]), allow_read: Some(vec![]), allow_write: Some(vec![]), allow_ffi: Some(vec![]), allow_hrtime: true, type_check_mode: TypeCheckMode::None, ..Flags::default() } ); } #[test] fn no_remote() { let r = flags_from_vec(svec!["deno", "run", "--no-remote", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), no_remote: true, ..Flags::default() } ); } #[test] fn cached_only() { let r = flags_from_vec(svec!["deno", "run", "--cached-only", "script.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), cached_only: true, ..Flags::default() } ); } #[test] fn allow_net_allowlist_with_ports() { let r = flags_from_vec(svec![ "deno", "run", "--allow-net=deno.land,:8000,:4545", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_net: Some(svec![ "deno.land", "0.0.0.0:8000", "127.0.0.1:8000", "localhost:8000", "0.0.0.0:4545", "127.0.0.1:4545", "localhost:4545" ]), ..Flags::default() } ); } #[test] fn allow_net_allowlist_with_ipv6_address() { let r = flags_from_vec(svec![ "deno", "run", "--allow-net=deno.land,deno.land:80,::,127.0.0.1,[::1],1.2.3.4:5678,:5678,[::1]:8080", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), allow_net: Some(svec![ "deno.land", "deno.land:80", "::", "127.0.0.1", "[::1]", "1.2.3.4:5678", "0.0.0.0:5678", "127.0.0.1:5678", "localhost:5678", "[::1]:8080" ]), ..Flags::default() } ); } #[test] fn lock_write() { let r = flags_from_vec(svec![ "deno", "run", "--lock-write", "--lock=lock.json", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), lock_write: true, lock: Some(PathBuf::from("lock.json")), ..Flags::default() } ); } #[test] fn test_with_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "test", "--unstable", "--trace-ops", "--no-run", "--filter", "- foo", "--coverage=cov", "--location", "https:foo", "--allow-net", "--allow-none", "dir1/", "dir2/", "--", "arg1", "arg2"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: true, doc: false, fail_fast: None, filter: Some("- foo".to_string()), allow_none: true, include: Some(svec!["dir1/", "dir2/"]), ignore: vec![], shuffle: None, concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: true, }), unstable: true, no_prompt: true, coverage_dir: Some("cov".to_string()), location: Some(Url::parse("https://foo/").unwrap()), allow_net: Some(vec![]), argv: svec!["arg1", "arg2"], ..Flags::default() } ); } #[test] fn run_with_cafile() { let r = flags_from_vec(svec![ "deno", "run", "--cert", "example.crt", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } ); } #[test] fn run_with_enable_testing_features() { let r = flags_from_vec(svec![ "deno", "run", "--enable-testing-features-do-not-use", "script.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), enable_testing_features: true, ..Flags::default() } ); } #[test] fn test_with_concurrent_jobs() { let r = flags_from_vec(svec!["deno", "test", "--jobs=4"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, filter: None, allow_none: false, shuffle: None, include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(4).unwrap(), trace_ops: false, }), no_prompt: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "test", "--jobs=0"]); assert!(r.is_err()); } #[test] fn test_with_fail_fast() { let r = flags_from_vec(svec!["deno", "test", "--fail-fast=3"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: Some(NonZeroUsize::new(3).unwrap()), filter: None, allow_none: false, shuffle: None, include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: false, }), no_prompt: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "test", "--fail-fast=0"]); assert!(r.is_err()); } #[test] fn test_with_enable_testing_features() { let r = flags_from_vec(svec![ "deno", "test", "--enable-testing-features-do-not-use" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, filter: None, allow_none: false, shuffle: None, include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: false, }), no_prompt: true, enable_testing_features: true, ..Flags::default() } ); } #[test] fn test_shuffle() { let r = flags_from_vec(svec!["deno", "test", "--shuffle=1"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, filter: None, allow_none: false, shuffle: Some(1), include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: false, }), no_prompt: true, watch: None, ..Flags::default() } ); } #[test] fn test_watch() { let r = flags_from_vec(svec!["deno", "test", "--watch"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, filter: None, allow_none: false, shuffle: None, include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: false, }), no_prompt: true, watch: Some(vec![]), ..Flags::default() } ); } #[test] fn test_watch_with_no_clear_screen() { let r = flags_from_vec(svec!["deno", "test", "--watch", "--no-clear-screen"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Test(TestFlags { no_run: false, doc: false, fail_fast: None, filter: None, allow_none: false, shuffle: None, include: None, ignore: vec![], concurrent_jobs: NonZeroUsize::new(1).unwrap(), trace_ops: false, }), watch: Some(vec![]), no_clear_screen: true, no_prompt: true, ..Flags::default() } ); } #[test] fn bundle_with_cafile() { let r = flags_from_vec(svec![ "deno", "bundle", "--cert", "example.crt", "source.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bundle(BundleFlags { source_file: "source.ts".to_string(), out_file: None, }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } ); } #[test] fn upgrade_with_ca_file() { let r = flags_from_vec(svec!["deno", "upgrade", "--cert", "example.crt"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Upgrade(UpgradeFlags { force: false, dry_run: false, canary: false, version: None, output: None, ca_file: Some("example.crt".to_owned()), }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } ); } #[test] fn cache_with_cafile() { let r = flags_from_vec(svec![ "deno", "cache", "--cert", "example.crt", "script.ts", "script_two.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Cache(CacheFlags { files: svec!["script.ts", "script_two.ts"], }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } ); } #[test] fn info_with_cafile() { let r = flags_from_vec(svec![ "deno", "info", "--cert", "example.crt", "https://example.com" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Info(InfoFlags { json: false, file: Some("https://example.com".to_string()), }), ca_file: Some("example.crt".to_owned()), ..Flags::default() } ); } #[test] fn doc() { let r = flags_from_vec(svec!["deno", "doc", "--json", "path/to/module.ts"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: true, source_file: Some("path/to/module.ts".to_string()), filter: None, }), ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "doc", "path/to/module.ts", "SomeClass.someField" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: Some("path/to/module.ts".to_string()), filter: Some("SomeClass.someField".to_string()), }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "doc"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: None, filter: None, }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "doc", "--builtin", "Deno.Listener"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, source_file: Some("--builtin".to_string()), filter: Some("Deno.Listener".to_string()), }), ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "doc", "--private", "path/to/module.js"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { private: true, json: false, source_file: Some("path/to/module.js".to_string()), filter: None, }), ..Flags::default() } ); } #[test] fn inspect_default_host() { let r = flags_from_vec(svec!["deno", "run", "--inspect", "foo.js"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "foo.js".to_string(), }), inspect: Some("127.0.0.1:9229".parse().unwrap()), ..Flags::default() } ); } #[test] fn compile() { let r = flags_from_vec(svec![ "deno", "compile", "https://deno.land/std/examples/colors.ts" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Compile(CompileFlags { source_file: "https://deno.land/std/examples/colors.ts".to_string(), output: None, args: vec![], target: None, }), ..Flags::default() } ); } #[test] fn compile_with_flags() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "compile", "--import-map", "import_map.json", "--no-remote", "--config", "tsconfig.json", "--no-check", "--unsafely-ignore-certificate-errors", "--reload", "--lock", "lock.json", "--lock-write", "--cert", "example.crt", "--cached-only", "--location", "https:foo", "--allow-read", "--allow-net", "--v8-flags=--help", "--seed", "1", "--output", "colors", "https://deno.land/std/examples/colors.ts", "foo", "bar"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Compile(CompileFlags { source_file: "https://deno.land/std/examples/colors.ts".to_string(), output: Some(PathBuf::from("colors")), args: svec!["foo", "bar"], target: None, }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), type_check_mode: TypeCheckMode::None, reload: true, lock: Some(PathBuf::from("lock.json")), lock_write: true, ca_file: Some("example.crt".to_string()), cached_only: true, location: Some(Url::parse("https://foo/").unwrap()), allow_read: Some(vec![]), unsafely_ignore_certificate_errors: Some(vec![]), allow_net: Some(vec![]), v8_flags: svec!["--help", "--random-seed=1"], seed: Some(1), ..Flags::default() } ); } #[test] fn coverage() { let r = flags_from_vec(svec!["deno", "coverage", "foo.json"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Coverage(CoverageFlags { files: vec![PathBuf::from("foo.json")], output: None, ignore: vec![], include: vec![r"^file:".to_string()], exclude: vec![r"test\.(js|mjs|ts|jsx|tsx)$".to_string()], lcov: false, }), ..Flags::default() } ); } #[test] fn coverage_with_lcov_and_out_file() { let r = flags_from_vec(svec![ "deno", "coverage", "--lcov", "--output=foo.lcov", "foo.json" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Coverage(CoverageFlags { files: vec![PathBuf::from("foo.json")], ignore: vec![], include: vec![r"^file:".to_string()], exclude: vec![r"test\.(js|mjs|ts|jsx|tsx)$".to_string()], lcov: true, output: Some(PathBuf::from("foo.lcov")), }), ..Flags::default() } ); } #[test] fn location_with_bad_scheme() { #[rustfmt::skip] let r = flags_from_vec(svec!["deno", "run", "--location", "foo:", "mod.ts"]); assert!(r.is_err()); assert!(r .unwrap_err() .to_string() .contains("Expected protocol \"http\" or \"https\"")); } #[test] fn compat() { let r = flags_from_vec(svec!["deno", "run", "--compat", "--unstable", "foo.js"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "foo.js".to_string(), }), compat: true, unstable: true, ..Flags::default() } ); } #[test] fn test_config_path_args() { let flags = flags_from_vec(svec!["deno", "run", "foo.js"]).unwrap(); assert_eq!( flags.config_path_args(), Some(vec![std::env::current_dir().unwrap().join("foo.js")]) ); let flags = flags_from_vec(svec!["deno", "run", "https://example.com/foo.js"]) .unwrap(); assert_eq!(flags.config_path_args(), None); let flags = flags_from_vec(svec!["deno", "lint", "dir/a.js", "dir/b.js"]).unwrap(); assert_eq!( flags.config_path_args(), Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]) ); let flags = flags_from_vec(svec!["deno", "lint"]).unwrap(); assert!(flags.config_path_args().unwrap().is_empty()); let flags = flags_from_vec(svec!["deno", "fmt", "dir/a.js", "dir/b.js"]).unwrap(); assert_eq!( flags.config_path_args(), Some(vec![PathBuf::from("dir/a.js"), PathBuf::from("dir/b.js")]) ); } #[test] fn test_no_clear_watch_flag_without_watch_flag() { let r = flags_from_vec(svec!["deno", "run", "--no-clear-screen", "foo.js"]); assert!(r.is_err()); let error_message = r.unwrap_err().to_string(); assert!(&error_message .contains("error: The following required arguments were not provided:")); assert!(&error_message.contains("--watch[=<FILES>...]")); } #[test] fn vendor_minimal() { let r = flags_from_vec(svec!["deno", "vendor", "mod.ts",]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Vendor(VendorFlags { specifiers: svec!["mod.ts"], force: false, output_path: None, }), ..Flags::default() } ); } #[test] fn vendor_all() { let r = flags_from_vec(svec![ "deno", "vendor", "--config", "deno.json", "--import-map", "import_map.json", "--lock", "lock.json", "--force", "--output", "out_dir", "--reload", "mod.ts", "deps.test.ts", ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Vendor(VendorFlags { specifiers: svec!["mod.ts", "deps.test.ts"], force: true, output_path: Some(PathBuf::from("out_dir")), }), config_path: Some("deno.json".to_string()), import_map_path: Some("import_map.json".to_string()), lock: Some(PathBuf::from("lock.json")), reload: true, ..Flags::default() } ); } #[test] fn task_subcommand() { let r = flags_from_vec(svec!["deno", "task", "build", "hello", "world",]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), argv: svec!["hello", "world"], ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "task", "build"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), ..Flags::default() } ); } #[test] fn task_subcommand_double_hyphen() { let r = flags_from_vec(svec![ "deno", "task", "-c", "deno.json", "build", "--", "hello", "world", ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), argv: svec!["--", "hello", "world"], config_path: Some("deno.json".to_string()), ..Flags::default() } ); } #[test] fn task_subcommand_double_hyphen_only() { // edge case, but it should forward let r = flags_from_vec(svec!["deno", "task", "build", "--"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), argv: svec!["--"], ..Flags::default() } ); } #[test] fn task_following_arg() { let r = flags_from_vec(svec!["deno", "task", "build", "-1", "--test"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), argv: svec!["-1", "--test"], ..Flags::default() } ); } #[test] fn task_following_double_hyphen_arg() { let r = flags_from_vec(svec!["deno", "task", "build", "--test"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "build".to_string(), }), argv: svec!["--test"], ..Flags::default() } ); } #[test] fn task_subcommand_empty() { let r = flags_from_vec(svec!["deno", "task"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "".to_string(), }), ..Flags::default() } ); } #[test] fn task_subcommand_config() { let r = flags_from_vec(svec!["deno", "task", "--config", "deno.jsonc"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "".to_string(), }), config_path: Some("deno.jsonc".to_string()), ..Flags::default() } ); } #[test] fn task_subcommand_config_short() { let r = flags_from_vec(svec!["deno", "task", "-c", "deno.jsonc"]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Task(TaskFlags { task: "".to_string(), }), config_path: Some("deno.jsonc".to_string()), ..Flags::default() } ); } #[test] fn bench_with_flags() { let r = flags_from_vec(svec![ "deno", "bench", "--unstable", "--filter", "- foo", "--location", "https:foo", "--allow-net", "dir1/", "dir2/", "--", "arg1", "arg2" ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Bench(BenchFlags { filter: Some("- foo".to_string()), include: Some(svec!["dir1/", "dir2/"]), ignore: vec![], }), unstable: true, location: Some(Url::parse("https://foo/").unwrap()), allow_net: Some(vec![]), no_prompt: true, argv: svec!["arg1", "arg2"], ..Flags::default() } ); } #[test] fn run_with_check() { let r = flags_from_vec(svec!["deno", "run", "--check", "script.ts",]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), future_type_check_mode: FutureTypeCheckMode::Local, has_check_flag: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "run", "--check=all", "script.ts",]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), future_type_check_mode: FutureTypeCheckMode::All, has_check_flag: true, ..Flags::default() } ); let r = flags_from_vec(svec!["deno", "run", "--check=foo", "script.ts",]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Run(RunFlags { script: "script.ts".to_string(), }), future_type_check_mode: FutureTypeCheckMode::None, has_check_flag: true, ..Flags::default() } ); let r = flags_from_vec(svec![ "deno", "run", "--no-check", "--check", "script.ts", ]); assert!(r.is_err()); }}