Skip to main content
Module

x/deno/cli/flags.rs

A modern runtime for JavaScript and TypeScript.
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use clap::App;use clap::AppSettings;use clap::Arg;use clap::ArgMatches;use clap::ArgSettings;use clap::SubCommand;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 std::net::SocketAddr;use std::num::NonZeroU32;use std::num::NonZeroU8;use std::num::NonZeroUsize;use std::path::PathBuf;use std::str::FromStr;
lazy_static::lazy_static! { static ref LONG_VERSION: String = format!( "{} ({}, {})\nv8 {}\ntypescript {}", crate::version::deno(), if crate::version::is_canary() { "canary" } else { env!("PROFILE") }, env!("TARGET"), deno_core::v8_version(), crate::version::TYPESCRIPT );}
#[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 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 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 LintFlags { pub files: Vec<PathBuf>, pub ignore: Vec<PathBuf>, pub rules: bool, pub rules_tags: Vec<String>, pub rules_include: Vec<String>, pub rules_exclude: Vec<String>, pub json: bool,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct ReplFlags { pub eval: Option<String>,}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]pub struct RunFlags { pub script: 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,}
#[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 enum DenoSubcommand { Bundle(BundleFlags), Cache(CacheFlags), Compile(CompileFlags), Completions(CompletionsFlags), Coverage(CoverageFlags), Doc(DocFlags), Eval(EvalFlags), Fmt(FmtFlags), Info(InfoFlags), Install(InstallFlags), Lsp, Lint(LintFlags), Repl(ReplFlags), Run(RunFlags), Test(TestFlags), Types, Upgrade(UpgradeFlags),}
impl Default for DenoSubcommand { fn default() -> DenoSubcommand { DenoSubcommand::Repl(ReplFlags { eval: 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_env: Option<Vec<String>>, pub allow_hrtime: bool, pub allow_net: Option<Vec<String>>, pub allow_ffi: Option<Vec<String>>, 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 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_check: bool, pub no_remote: bool, pub 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: 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![]; 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={}", ffi_allowlist.join(",")); args.push(s); } _ => {} } if self.allow_hrtime { args.push("--allow-hrtime".to_string()); } args }}
impl From<Flags> for PermissionsOptions { fn from(flags: Flags) -> Self { Self { allow_env: flags.allow_env, allow_hrtime: flags.allow_hrtime, allow_net: flags.allow_net, allow_ffi: flags.allow_ffi, allow_read: flags.allow_read, allow_run: flags.allow_run, allow_write: flags.allow_write, prompt: flags.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-seperated list of order dependent certificate stores (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_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: &str = "A modern JavaScript and TypeScript runtime
Docs: https://deno.land/manualModules: 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)\"";
/// 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.get_matches_from_safe(args).map_err(|e| clap::Error { message: e.message.trim_start_matches("error: ").to_string(), ..e })?; 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); } if let Some(m) = matches.subcommand_matches("run") { run_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("fmt") { fmt_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("types") { types_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("cache") { cache_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("coverage") { coverage_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("info") { info_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("eval") { eval_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("repl") { repl_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("bundle") { bundle_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("install") { install_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("completions") { completions_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("test") { test_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("upgrade") { upgrade_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("doc") { doc_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("lint") { lint_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("compile") { compile_parse(&mut flags, m); } else if let Some(m) = matches.subcommand_matches("lsp") { lsp_parse(&mut flags, m); } else { repl_parse(&mut flags, &matches); } Ok(flags)}
fn clap_root<'a, 'b>(version: &'b str) -> App<'a, 'b> { clap::App::new("deno") .bin_name("deno") .global_settings(&[ AppSettings::UnifiedHelpMessage, AppSettings::ColorNever, AppSettings::VersionlessSubcommands, ]) // Disable clap's auto-detection of terminal width .set_term_width(0) // Disable each subcommand having its own version. .version(version) .long_version(LONG_VERSION.as_str()) .arg( Arg::with_name("unstable") .long("unstable") .help("Enable unstable features and APIs") .global(true), ) .arg( Arg::with_name("log-level") .short("L") .long("log-level") .help("Set log level") .takes_value(true) .possible_values(&["debug", "info"]) .global(true), ) .arg( Arg::with_name("quiet") .short("q") .long("quiet") .help("Suppress diagnostic output") .long_help( "Suppress diagnostic outputBy default, subcommands print human-readable diagnostic messages to stderr.If the flag is set, restrict these messages to errors.", ) .global(true), ) .subcommand(bundle_subcommand()) .subcommand(cache_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(lsp_subcommand()) .subcommand(lint_subcommand()) .subcommand(repl_subcommand()) .subcommand(run_subcommand()) .subcommand(test_subcommand()) .subcommand(types_subcommand()) .subcommand(upgrade_subcommand()) .long_about(DENO_HELP) .after_help(ENV_VARIABLES_HELP)}
fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> { compile_args(SubCommand::with_name("bundle")) .arg( Arg::with_name("source_file") .takes_value(true) .required(true), ) .arg(Arg::with_name("out_file").takes_value(true).required(false)) .arg(watch_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, 'b>() -> App<'a, 'b> { compile_args(SubCommand::with_name("cache")) .arg( Arg::with_name("file") .takes_value(true) .required(true) .min_values(1), ) .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 themin 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 compile_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("compile"), true, false) .setting(AppSettings::TrailingVarArg) .arg( script_arg().required(true), ) .arg( Arg::with_name("output") .long("output") .short("o") .help("Output file (defaults to $PWD/<inferred-name>)") .takes_value(true) ) .arg( Arg::with_name("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 /usr/local/bin/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 usedat 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.This commands supports cross-compiling to different target architectures using `--target` flag.On the first invocation with deno will download proper binary and cache it in $DENO_DIR. Theaarch64-apple-darwin target is not supported in canary.", )}
fn completions_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("completions") .setting(AppSettings::DisableHelpSubcommand) .arg( Arg::with_name("shell") .possible_values(&clap::Shell::variants()) .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, 'b>() -> App<'a, 'b> { SubCommand::with_name("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, foran 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 cov_profile > cov.lcovGenerate html reports from lcov:
genhtml -o html_cov cov.lcov", ) .arg( Arg::with_name("ignore") .long("ignore") .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Ignore coverage files"), ) .arg( Arg::with_name("include") .long("include") .takes_value(true) .value_name("regex") .multiple(true) .require_equals(true) .default_value(r"^file:") .help("Include source files in the report"), ) .arg( Arg::with_name("exclude") .long("exclude") .takes_value(true) .value_name("regex") .multiple(true) .require_equals(true) .default_value(r"test\.(js|mjs|ts|jsx|tsx)$") .help("Exclude source files from the report"), ) .arg( Arg::with_name("lcov") .long("lcov") .help("Output coverage report in lcov format") .takes_value(false), ) .arg( Arg::with_name("files") .takes_value(true) .multiple(true) .required(true), )}
fn doc_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("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::with_name("json") .long("json") .help("Output documentation in JSON format") .takes_value(false), ) .arg( Arg::with_name("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. .setting(clap::AppSettings::AllowLeadingHyphen) .arg(Arg::with_name("source_file").takes_value(true)) .arg( Arg::with_name("filter") .help("Dot separated path to symbol") .takes_value(true) .required(false) .conflicts_with("json") .conflicts_with("pretty"), )}
fn eval_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("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::with_name("ts") .long("ts") .short("T") .help("Treat eval input as TypeScript") .takes_value(false) .multiple(false) .hidden(true), ) .arg( Arg::with_name("ext") .long("ext") .help("Set standard input (stdin) content type") .takes_value(true) .default_value("js") .possible_values(&["ts", "tsx", "js", "jsx"]), ) .arg( Arg::with_name("print") .long("print") .short("p") .help("print result to stdout") .takes_value(false) .multiple(false), ) .arg( Arg::with_name("code_arg") .multiple(true) .help("Code arg") .value_name("CODE_ARG") .required(true), )}
fn fmt_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("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::with_name("check") .long("check") .help("Check if the source files are formatted") .takes_value(false), ) .arg( Arg::with_name("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::with_name("ignore") .long("ignore") .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Ignore formatting particular source files"), ) .arg( Arg::with_name("files") .takes_value(true) .multiple(true) .required(false), ) .arg(watch_arg()) .arg( Arg::with_name("options-use-tabs") .long("options-use-tabs") .help("Use tabs instead of spaces for indentation. Defaults to false."), ) .arg( Arg::with_name("options-line-width") .long("options-line-width") .help("Define maximum line width. Defaults to 80.") .takes_value(true) .validator(|val: String| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => { Err("options-line-width should be a non zero integer".to_string()) } }), ) .arg( Arg::with_name("options-indent-width") .long("options-indent-width") .help("Define indentation width. Defaults to 2.") .takes_value(true) .validator(|val: String| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => { Err("options-indent-width should be a non zero integer".to_string()) } }), ) .arg( Arg::with_name("options-single-quote") .long("options-single-quote") .help("Use single quotes. Defaults to false."), ) .arg( Arg::with_name("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, 'b>() -> App<'a, 'b> { SubCommand::with_name("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::with_name("file").takes_value(true).required(false)) .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().hidden(true)) .arg(import_map_arg()) .arg( Arg::with_name("json") .long("json") .help("UNSTABLE: Outputs the information in JSON format") .takes_value(false), )}
fn install_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("install"), true, true) .setting(AppSettings::TrailingVarArg) .arg( Arg::with_name("cmd") .required(true) .multiple(true) .allow_hyphen_values(true)) .arg( Arg::with_name("name") .long("name") .short("n") .help("Executable file name") .takes_value(true) .required(false)) .arg( Arg::with_name("root") .long("root") .help("Installation root") .takes_value(true) .multiple(false)) .arg( Arg::with_name("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 lsp_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("lsp") .about("Start the language server") .long_about( "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/getting_started/setup_your_environment#editors-and-ides")}
fn lint_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("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::with_name("rules") .long("rules") .help("List available rules"), ) .arg( Arg::with_name("rules-tags") .long("rules-tags") .require_equals(true) .takes_value(true) .use_delimiter(true) .empty_values(true) .conflicts_with("rules") .help("Use set of rules with a tag"), ) .arg( Arg::with_name("rules-include") .long("rules-include") .require_equals(true) .takes_value(true) .use_delimiter(true) .conflicts_with("rules") .help("Include lint rules"), ) .arg( Arg::with_name("rules-exclude") .long("rules-exclude") .require_equals(true) .takes_value(true) .use_delimiter(true) .conflicts_with("rules") .help("Exclude lint rules"), ) .arg(config_arg()) .arg( Arg::with_name("ignore") .long("ignore") .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Ignore linting particular source files"), ) .arg( Arg::with_name("json") .long("json") .help("Output lint result in JSON format") .takes_value(false), ) .arg( Arg::with_name("files") .takes_value(true) .multiple(true) .required(false), )}
fn repl_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("repl"), false, true) .about("Read Eval Print Loop") .arg( Arg::with_name("eval") .long("eval") .help("Evaluates the provided code when the REPL starts.") .takes_value(true) .value_name("code"), )}
fn run_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("run"), true, true) .arg( watch_arg() .conflicts_with("inspect") .conflicts_with("inspect-brk"), ) .setting(AppSettings::TrailingVarArg) .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.tsDeno allows specifying the filename '-' to read the file from stdin.
curl https://deno.land/std/examples/welcome.ts | deno run -", )}
fn test_subcommand<'a, 'b>() -> App<'a, 'b> { runtime_args(SubCommand::with_name("test"), true, true) .setting(AppSettings::TrailingVarArg) .arg( Arg::with_name("ignore") .long("ignore") .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Ignore files"), ) .arg( Arg::with_name("no-run") .long("no-run") .help("Cache test modules, but don't run tests") .takes_value(false), ) .arg( Arg::with_name("doc") .long("doc") .help("UNSTABLE: type check code blocks") .takes_value(false), ) .arg( Arg::with_name("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: String| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => Err("fail-fast should be a non zero integer".to_string()), }), ) .arg( Arg::with_name("allow-none") .long("allow-none") .help("Don't return error code if no test files are found") .takes_value(false), ) .arg( Arg::with_name("filter") .set(ArgSettings::AllowLeadingHyphen) .long("filter") .takes_value(true) .help("Run tests with this string or pattern in the test name"), ) .arg( Arg::with_name("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: String| match val.parse::<u64>() { Ok(_) => Ok(()), Err(_) => Err("Shuffle seed should be a number".to_string()), }), ) .arg( Arg::with_name("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::with_name("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: String| match val.parse::<NonZeroUsize>() { Ok(_) => Ok(()), Err(_) => Err("jobs should be a non zero unsigned integer".to_string()), }), ) .arg( Arg::with_name("files") .help("List of file names to run") .takes_value(true) .multiple(true), ) .arg( watch_arg() .conflicts_with("no-run") .conflicts_with("coverage"), ) .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, 'b>() -> App<'a, 'b> { SubCommand::with_name("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, 'b>() -> App<'a, 'b> { SubCommand::with_name("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::with_name("version") .long("version") .help("The version to upgrade to") .takes_value(true), ) .arg( Arg::with_name("output") .long("output") .help("The path to output the updated version to") .takes_value(true), ) .arg( Arg::with_name("dry-run") .long("dry-run") .help("Perform all checks without replacing old exe"), ) .arg( Arg::with_name("force") .long("force") .short("f") .help("Replace current exe even if not out-of-date"), ) .arg( Arg::with_name("canary") .long("canary") .help("Upgrade to canary builds"), ) .arg(ca_file_arg())}
fn compile_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { 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 permission_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { app .arg( Arg::with_name("allow-read") .long("allow-read") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Allow file system read access"), ) .arg( Arg::with_name("allow-write") .long("allow-write") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Allow file system write access"), ) .arg( Arg::with_name("allow-net") .long("allow-net") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Allow network access") .validator(crate::flags_allow_net::validator), ) .arg( Arg::with_name("unsafely-ignore-certificate-errors") .long("unsafely-ignore-certificate-errors") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .value_name("HOSTNAMES") .help("DANGER: Disables verification of TLS certificates") .validator(crate::flags_allow_net::validator), ) .arg( Arg::with_name("allow-env") .long("allow-env") .min_values(0) .takes_value(true) .use_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::with_name("allow-run") .long("allow-run") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Allow running subprocesses"), ) .arg( Arg::with_name("allow-ffi") .long("allow-ffi") .min_values(0) .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Allow loading dynamic libraries"), ) .arg( Arg::with_name("allow-hrtime") .long("allow-hrtime") .help("Allow high resolution time measurement"), ) .arg( Arg::with_name("allow-all") .short("A") .long("allow-all") .help("Allow all permissions"), ) .arg( Arg::with_name("prompt") .long("prompt") .help("Fallback to prompt if required permission wasn't passed"), )}
fn runtime_args<'a, 'b>( app: App<'a, 'b>, include_perms: bool, include_inspector: bool,) -> App<'a, 'b> { 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())}
fn inspect_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> { app .arg( Arg::with_name("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::with_name("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), )}
fn import_map_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("import-map") .long("import-map") .alias("importmap") .value_name("FILE") .help("Load import map file") .long_help( "Load import map file from local file or remote URL.Docs: https://deno.land/manual/linking_to_external_code/import_mapsSpecification: https://wicg.github.io/import-maps/Examples: https://github.com/WICG/import-maps#the-import-map", ) .takes_value(true)}
fn reload_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("reload") .short("r") .min_values(0) .takes_value(true) .use_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", )}
fn ca_file_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("cert") .long("cert") .value_name("FILE") .help("Load certificate authority from PEM encoded file") .takes_value(true)}
fn cached_only_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("cached-only") .long("cached-only") .help("Require that remote dependencies are already cached")}
fn location_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("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")}
fn enable_testing_features_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("enable-testing-features-do-not-use") .long("enable-testing-features-do-not-use") .help("INTERNAL: Enable internal features used during integration testing") .hidden(true)}
fn v8_flags_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("v8-flags") .long("v8-flags") .takes_value(true) .use_delimiter(true) .require_equals(true) .help("Set V8 command line options (for help: --v8-flags=--help)")}
fn seed_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("seed") .long("seed") .value_name("NUMBER") .help("Seed Math.random()") .takes_value(true) .validator(|val: String| match val.parse::<u64>() { Ok(_) => Ok(()), Err(_) => Err("Seed should be a number".to_string()), })}
fn watch_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("watch") .long("watch") .help("UNSTABLE: Watch for file changes and restart process automatically") .long_help( "UNSTABLE: Watch for file changes and restart process automatically.Only local files from entry point module graph are watched.", )}
fn no_check_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("no-check") .long("no-check") .help("Skip type checking modules")}
fn script_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("script_arg") .multiple(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"), "_"), ("v8-flags", Some("-help"), "_"), ]) .help("Script arg") .value_name("SCRIPT_ARG")}
fn lock_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("lock") .long("lock") .value_name("FILE") .help("Check the specified lock file") .takes_value(true)}
fn lock_write_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("lock-write") .long("lock-write") .requires("lock") .help("Write lock file (use with --lock)")}
fn config_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("config") .short("c") .long("config") .value_name("FILE") .help("Load configuration file") .long_help( "Load configuration file.Before 1.14 Deno only supported loading tsconfig.json that allowedto customise TypeScript compiler settings.
Starting with 1.14 configuration file can be used to configure different subcommands like `deno lint` or `deno fmt`.
It's recommended to use `deno.json` or `deno.jsonc` as a filename.", ) .takes_value(true)}
fn no_remote_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name("no-remote") .long("no-remote") .help("Do not resolve remote modules")}
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 }; flags.watch = matches.is_present("watch"); 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 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) { let shell: &str = matches.value_of("shell").unwrap(); let mut buf: Vec<u8> = vec![]; clap_root(&*crate::version::deno()).gen_completions_to( "deno", clap::Shell::from_str(shell).unwrap(), &mut buf, ); 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"); flags.subcommand = DenoSubcommand::Coverage(CoverageFlags { files, 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); flags.watch = matches.is_present("watch"); 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 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); 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 rules_tags = match matches.values_of("rules-tags") { Some(f) => f.map(String::from).collect(), None => vec![], }; let rules_include = match matches.values_of("rules-include") { Some(f) => f.map(String::from).collect(), None => vec![], }; let rules_exclude = match matches.values_of("rules-exclude") { Some(f) => f.map(String::from).collect(), None => vec![], }; let json = matches.is_present("json"); flags.subcommand = DenoSubcommand::Lint(LintFlags { files, rules, rules_tags, rules_include, rules_exclude, ignore, json, });}
fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, false, true); flags.repl = true; flags.subcommand = DenoSubcommand::Repl(ReplFlags { eval: matches.value_of("eval").map(ToOwned::to_owned), }); 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 run_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, true); 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); } flags.watch = matches.is_present("watch"); flags.subcommand = DenoSubcommand::Run(RunFlags { script });}
fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) { runtime_args_parse(flags, matches, true, 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 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 { // TODO(caspervonb) drop the dependency on num_cpus when https://doc.rust-lang.org/std/thread/fn.available_concurrency.html becomes stable. NonZeroUsize::new(num_cpus::get()).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); flags.watch = matches.is_present("watch"); flags.subcommand = DenoSubcommand::Test(TestFlags { no_run, doc, fail_fast, include, ignore, filter, shuffle, allow_none, concurrent_jobs, });}
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 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 permission_args_parse(flags: &mut Flags, matches: &clap::ArgMatches) { 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(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); } 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<String> = ffi_wl.map(ToString::to_string).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_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; } if matches.is_present("prompt") { flags.prompt = true; }}
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); inspect_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 no_check_arg_parse(flags: &mut Flags, matches: &clap::ArgMatches) { if matches.is_present("no-check") { flags.no_check = true; }}
fn lock_args_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)); } if matches.is_present("lock-write") { flags.lock_write = true; }}
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: String) -> Result<(), String> { match val.parse::<SocketAddr>() { Ok(_) => Ok(()), Err(e) => Err(e.to_string()), }}
// 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::*; /// 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::VersionDisplayed); let r = flags_from_vec(svec!["deno", "-V"]); assert_eq!(r.unwrap_err().kind, clap::ErrorKind::VersionDisplayed); } #[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: 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_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: 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: true, ..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: true, ..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, rules_tags: vec![], rules_include: vec![], rules_exclude: vec![], json: false, ignore: vec![], }), ..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, rules_tags: vec![], rules_include: vec![], rules_exclude: vec![], 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, rules_tags: vec![], rules_include: vec![], rules_exclude: vec![], 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, rules_tags: svec![""], rules_include: svec!["ban-untagged-todo", "no-undef"], rules_exclude: 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, rules_tags: vec![], rules_include: vec![], rules_exclude: vec![], 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, rules_tags: vec![], rules_include: vec![], rules_exclude: vec![], 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 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()), no_check: true, 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: 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"]); assert_eq!( r.unwrap(), Flags { repl: true, subcommand: DenoSubcommand::Repl(ReplFlags { eval: None }), import_map_path: Some("import_map.json".to_string()), no_remote: true, config_path: Some("tsconfig.json".to_string()), no_check: true, 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 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: 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, ..Flags::default() } ); } #[test] fn allow_read_allowlist() { use tempfile::TempDir; let temp_dir = TempDir::new().expect("tempdir fail").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 tempfile::TempDir; let temp_dir = TempDir::new().expect("tempdir fail").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, }), no_check: true, ..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: 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()), no_check: true, 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(), }), no_check: true, ..Flags::default() } ); } #[test] fn 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 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 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", "--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(), }), unstable: 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(), }), ..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(), }), ..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(), }), 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(), }), watch: false, ..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(), }), watch: 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()), no_check: true, 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")], 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 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\"")); }}