forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.rs
4499 lines (4127 loc) · 117 KB
/
flags.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 UninstallFlags {
pub name: String,
pub root: Option<PathBuf>,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct LintFlags {
pub files: Vec<PathBuf>,
pub ignore: Vec<PathBuf>,
pub rules: bool,
pub maybe_rules_tags: Option<Vec<String>>,
pub maybe_rules_include: Option<Vec<String>>,
pub maybe_rules_exclude: Option<Vec<String>>,
pub json: bool,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ReplFlags {
pub eval: 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),
Uninstall(UninstallFlags),
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<PathBuf>>,
pub allow_read: Option<Vec<PathBuf>>,
pub allow_run: Option<Vec<String>>,
pub allow_write: Option<Vec<PathBuf>>,
pub policy_file: Option<Vec<PathBuf>>,
pub strict: bool,
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,
/// If true, a list of Node built-in modules will be injected into
/// the import map.
pub compat: 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={}", join_paths(ffi_allowlist, ","));
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,
policy_file: flags.policy_file,
strict: flags.strict,
}
}
}
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. "[email protected];[email protected]")
DENO_TLS_CA_STORE Comma-separated 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/manual
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues
To start the REPL:
deno
To execute a script:
deno run https://deno.land/std/examples/welcome.ts
To 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("uninstall") {
uninstall_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 output
By 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(uninstall_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.js
If 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 them
in the local cache, without running any code:
deno cache https://deno.land/std/http/file_server.ts
Future 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.ts
Any flags passed which affect runtime behavior, such as '--unstable',
'--allow-*', '--v8-flags', etc. are encoded into the output executable and used
at runtime as if they were passed to a similar 'deno run' command.
The executable name is inferred by default:
- Attempt to take the file stem of the URL path. The above example would
become 'file_server'.
- If the file stem is something generic like 'main', 'mod', 'index' or 'cli',
and the path has no parent, take the file name of the parent path. Otherwise
settle with the generic name.
- If the resulting name has an '@...' suffix, strip it.
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. The
aarch64-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_profile
Print a report to stdout:
deno coverage cov_profile
Include urls that start with the file schema:
deno coverage --include=\"^file:\" cov_profile
Exclude urls ending with test.ts and test.js:
deno coverage --exclude=\"test\\.(ts|js)\" cov_profile
Include urls that start with the file schema and exclude files ending with test.ts and test.js, for
an url to match it must match the include pattern and not match the exclude pattern:
deno coverage --include=\"^file:\" --exclude=\"test\\.(ts|js)\" cov_profile
Write a report using the lcov format:
deno coverage --lcov cov_profile > cov.lcov
Generate 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.ts
Output private documentation to standard output:
deno doc --private ./path/to/module.ts
Output documentation in JSON format:
deno doc --json ./path/to/module.ts
Target a specific symbol:
deno doc ./path/to/module.ts MyClass.someField
Show 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 --check
Format stdin and write to stdout:
cat file.ts | deno fmt -
Ignore formatting code by preceding it with an ignore comment:
// deno-fmt-ignore
Ignore 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.ts
The 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.ts
To change the executable name, use -n/--name:
deno install --allow-net --allow-read -n serve https://deno.land/std/http/file_server.ts
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.