forked from denoland/deno
/
flags.rs
2092 lines (1891 loc) · 51.4 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-2020 the Deno authors. All rights reserved. MIT license.
use crate::fs::resolve_from_cwd;
use clap::App;
use clap::AppSettings;
use clap::Arg;
use clap::ArgMatches;
use clap::SubCommand;
use log::Level;
use std::collections::HashSet;
use std::path::Path;
/// Creates vector of strings, Vec<String>
macro_rules! svec {
($($x:expr),*) => (vec![$($x.to_string()),*]);
}
/// Creates HashSet<String> from string literals
macro_rules! sset {
($($x:expr),*) => {{
let _v = svec![$($x.to_string()),*];
let hash_set: HashSet<String> = _v.iter().cloned().collect();
hash_set
}}
}
macro_rules! std_url {
($x:expr) => {
concat!("https://deno.land/std@v0.29.0/", $x)
};
}
/// Used for `deno test...` subcommand
const TEST_RUNNER_URL: &str = std_url!("testing/runner.ts");
#[derive(Clone, Debug, PartialEq)]
pub enum DenoSubcommand {
Bundle {
source_file: String,
out_file: Option<String>,
},
Completions {
buf: Box<[u8]>,
},
Eval {
code: String,
},
Fetch {
files: Vec<String>,
},
Format {
check: bool,
files: Option<Vec<String>>,
},
Help,
Info {
file: Option<String>,
},
Install {
dir: Option<String>,
exe_name: String,
module_url: String,
args: Vec<String>,
force: bool,
},
Repl,
Run {
script: String,
},
Types,
}
impl Default for DenoSubcommand {
fn default() -> DenoSubcommand {
DenoSubcommand::Repl
}
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct DenoFlags {
/// Vector of CLI arguments - these are user script arguments, all Deno
/// specific flags are removed.
pub argv: Vec<String>,
pub subcommand: DenoSubcommand,
pub log_level: Option<Level>,
pub version: bool,
pub reload: bool,
pub config_path: Option<String>,
pub import_map_path: Option<String>,
pub allow_read: bool,
pub read_whitelist: Vec<String>,
pub cache_blacklist: Vec<String>,
pub allow_write: bool,
pub write_whitelist: Vec<String>,
pub allow_net: bool,
pub net_whitelist: Vec<String>,
pub allow_env: bool,
pub allow_run: bool,
pub allow_plugin: bool,
pub allow_hrtime: bool,
pub no_prompts: bool,
pub no_remote: bool,
pub cached_only: bool,
pub seed: Option<u64>,
pub v8_flags: Option<Vec<String>>,
pub lock: Option<String>,
pub lock_write: bool,
}
impl DenoFlags {
/// Return list of permission arguments that are equivalent
/// to the ones used to create `self`.
pub fn to_permission_args(&self) -> Vec<String> {
let mut args = vec![];
if !self.read_whitelist.is_empty() {
let s = format!("--allow-read={}", self.read_whitelist.join(","));
args.push(s);
}
if self.allow_read {
args.push("--allow-read".to_string());
}
if !self.write_whitelist.is_empty() {
let s = format!("--allow-write={}", self.write_whitelist.join(","));
args.push(s);
}
if self.allow_write {
args.push("--allow-write".to_string());
}
if !self.net_whitelist.is_empty() {
let s = format!("--allow-net={}", self.net_whitelist.join(","));
args.push(s);
}
if self.allow_net {
args.push("--allow-net".to_string());
}
if self.allow_env {
args.push("--allow-env".to_string());
}
if self.allow_run {
args.push("--allow-run".to_string());
}
if self.allow_plugin {
args.push("--allow-plugin".to_string());
}
if self.allow_hrtime {
args.push("--allow-hrtime".to_string());
}
args
}
}
static ENV_VARIABLES_HELP: &str = "ENVIRONMENT VARIABLES:
DENO_DIR Set deno's base directory
NO_COLOR Set to disable color
HTTP_PROXY Proxy address for HTTP requests (module downloads, fetch)
HTTPS_PROXY Same but for HTTPS";
static DENO_HELP: &str = "A secure JavaScript and TypeScript runtime
Docs: https://deno.land/std/manual.md
Modules: https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues
To run the REPL supply no arguments:
deno
To evaluate code from the command line:
deno eval \"console.log(30933 + 404)\"
To execute a script:
deno https://deno.land/std/examples/welcome.ts
The default subcommand is 'run'. The above is equivalent to
deno run https://deno.land/std/examples/welcome.ts
See 'deno help run' for run specific flags.";
lazy_static! {
static ref LONG_VERSION: String = format!(
"{}\nv8 {}\ntypescript {}",
crate::version::DENO,
crate::version::v8(),
crate::version::TYPESCRIPT
);
}
/// Main entry point for parsing deno's command line flags.
/// Exits the process on error.
pub fn flags_from_vec(args: Vec<String>) -> DenoFlags {
match flags_from_vec_safe(args) {
Ok(flags) => flags,
Err(err) => err.exit(),
}
}
/// Same as flags_from_vec but does not exit on error.
pub fn flags_from_vec_safe(args: Vec<String>) -> clap::Result<DenoFlags> {
let args = arg_hacks(args);
let app = clap_root();
let matches = app.get_matches_from_safe(args)?;
let mut flags = DenoFlags::default();
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 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("fetch") {
fetch_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 {
unimplemented!();
}
Ok(flags)
}
fn clap_root<'a, 'b>() -> 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(crate::version::DENO)
.long_version(LONG_VERSION.as_str())
.arg(
Arg::with_name("log-level")
.short("L")
.long("log-level")
.help("Set log level")
.takes_value(true)
.possible_values(&["debug", "info"])
.global(true),
)
.subcommand(bundle_subcommand())
.subcommand(completions_subcommand())
.subcommand(eval_subcommand())
.subcommand(fetch_subcommand())
.subcommand(fmt_subcommand())
.subcommand(info_subcommand())
.subcommand(install_subcommand())
.subcommand(repl_subcommand())
.subcommand(run_subcommand())
.subcommand(test_subcommand())
.subcommand(types_subcommand())
.long_about(DENO_HELP)
.after_help(ENV_VARIABLES_HELP)
}
fn types_parse(flags: &mut DenoFlags, _matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Types;
}
fn fmt_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
let maybe_files = match matches.values_of("files") {
Some(f) => {
let files: Vec<String> = f.map(String::from).collect();
Some(files)
}
None => None,
};
let check = matches.is_present("check");
flags.subcommand = DenoSubcommand::Format {
check,
files: maybe_files,
}
}
fn install_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
permission_args_parse(flags, matches);
let dir = if matches.is_present("dir") {
let install_dir = matches.value_of("dir").unwrap();
Some(install_dir.to_string())
} else {
None
};
let force = matches.is_present("force");
let exe_name = matches.value_of("exe_name").unwrap().to_string();
let cmd_values = matches.values_of("cmd").unwrap();
let mut cmd_args = vec![];
for value in cmd_values {
cmd_args.push(value.to_string());
}
let module_url = cmd_args[0].to_string();
let args = cmd_args[1..].to_vec();
flags.subcommand = DenoSubcommand::Install {
dir,
exe_name,
module_url,
args,
force,
};
}
fn bundle_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
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 = true;
Some(out_file.to_string())
} else {
None
};
flags.subcommand = DenoSubcommand::Bundle {
source_file,
out_file,
};
}
fn completions_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
let shell: &str = matches.value_of("shell").unwrap();
let mut buf: Vec<u8> = vec![];
use std::str::FromStr;
clap_root().gen_completions_to(
"deno",
clap::Shell::from_str(shell).unwrap(),
&mut buf,
);
flags.subcommand = DenoSubcommand::Completions {
buf: buf.into_boxed_slice(),
};
}
fn repl_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
v8_flags_arg_parse(flags, matches);
flags.subcommand = DenoSubcommand::Repl;
flags.allow_net = true;
flags.allow_env = true;
flags.allow_run = true;
flags.allow_read = true;
flags.allow_write = true;
flags.allow_plugin = true;
flags.allow_hrtime = true;
}
fn eval_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
v8_flags_arg_parse(flags, matches);
flags.allow_net = true;
flags.allow_env = true;
flags.allow_run = true;
flags.allow_read = true;
flags.allow_write = true;
flags.allow_plugin = true;
flags.allow_hrtime = true;
let code = matches.value_of("code").unwrap().to_string();
flags.subcommand = DenoSubcommand::Eval { code }
}
fn info_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Info {
file: matches.value_of("file").map(|f| f.to_string()),
};
}
fn fetch_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
reload_arg_parse(flags, matches);
lock_args_parse(flags, matches);
importmap_arg_parse(flags, matches);
config_arg_parse(flags, matches);
no_remote_arg_parse(flags, matches);
let files = matches
.values_of("file")
.unwrap()
.map(String::from)
.collect();
flags.subcommand = DenoSubcommand::Fetch { files };
}
fn lock_args_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
if matches.is_present("lock") {
let lockfile = matches.value_of("lock").unwrap();
flags.lock = Some(lockfile.to_string());
}
if matches.is_present("lock-write") {
flags.lock_write = true;
}
}
fn resolve_fs_whitelist(whitelist: &[String]) -> Vec<String> {
whitelist
.iter()
.map(|raw_path| {
resolve_from_cwd(Path::new(&raw_path))
.unwrap()
.to_str()
.unwrap()
.to_owned()
})
.collect()
}
// Shared between the run and test subcommands. They both take similar options.
fn run_test_args_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
reload_arg_parse(flags, matches);
lock_args_parse(flags, matches);
importmap_arg_parse(flags, matches);
config_arg_parse(flags, matches);
v8_flags_arg_parse(flags, matches);
no_remote_arg_parse(flags, matches);
permission_args_parse(flags, matches);
if matches.is_present("cached-only") {
flags.cached_only = true;
}
if matches.is_present("seed") {
let seed_string = matches.value_of("seed").unwrap();
let seed = seed_string.parse::<u64>().unwrap();
flags.seed = Some(seed);
let v8_seed_flag = format!("--random-seed={}", seed);
match flags.v8_flags {
Some(ref mut v8_flags) => {
v8_flags.push(v8_seed_flag);
}
None => {
flags.v8_flags = Some(svec![v8_seed_flag]);
}
}
}
}
fn run_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
run_test_args_parse(flags, matches);
let mut script: Vec<String> = matches
.values_of("script_arg")
.unwrap()
.map(String::from)
.collect();
assert!(!script.is_empty());
let script_args = script.split_off(1);
let script = script[0].to_string();
for v in script_args {
flags.argv.push(v);
}
flags.subcommand = DenoSubcommand::Run { script };
}
fn test_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
flags.subcommand = DenoSubcommand::Run {
script: TEST_RUNNER_URL.to_string(),
};
flags.allow_read = true;
run_test_args_parse(flags, matches);
if matches.is_present("quiet") {
flags.argv.push("--quiet".to_string());
}
if matches.is_present("failfast") {
flags.argv.push("--failfast".to_string());
}
if matches.is_present("exclude") {
flags.argv.push("--exclude".to_string());
let exclude: Vec<String> = matches
.values_of("exclude")
.unwrap()
.map(String::from)
.collect();
flags.argv.extend(exclude);
}
if matches.is_present("files") {
flags.argv.push("--".to_string());
let files: Vec<String> = matches
.values_of("files")
.unwrap()
.map(String::from)
.collect();
flags.argv.extend(files);
}
}
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_runtime.d.ts
The declaration file could be saved and used for typing information.",
)
}
fn fmt_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("fmt")
.about("Format files")
.long_about(
"Auto-format JavaScript/TypeScript source code
deno fmt
deno fmt myfile1.ts myfile2.ts
deno fmt --check
# Format stdin and write to stdout
cat file.ts | deno fmt -",
)
.arg(
Arg::with_name("check")
.long("check")
.help("Check if the source files are formatted.")
.takes_value(false),
)
.arg(
Arg::with_name("files")
.takes_value(true)
.multiple(true)
.required(false),
)
}
fn repl_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("repl")
.about("Read Eval Print Loop")
.arg(v8_flags_arg())
}
fn install_subcommand<'a, 'b>() -> App<'a, 'b> {
permission_args(SubCommand::with_name("install"))
.setting(AppSettings::TrailingVarArg)
.arg(
Arg::with_name("dir")
.long("dir")
.short("d")
.help("Installation directory (defaults to $HOME/.deno/bin)")
.takes_value(true)
.multiple(false))
.arg(
Arg::with_name("force")
.long("force")
.short("f")
.help("Forcefully overwrite existing installation")
.takes_value(false))
.arg(
Arg::with_name("exe_name")
.required(true)
)
.arg(
Arg::with_name("cmd")
.required(true)
.multiple(true)
.allow_hyphen_values(true)
)
.about("Install script as executable")
.long_about(
"Installs a script as executable. The default installation directory is
$HOME/.deno/bin and it must be added to the path manually.
deno install --allow-net --allow-read file_server https://deno.land/std/http/file_server.ts
deno install colors https://deno.land/std/examples/colors.ts
To change installation directory use -d/--dir flag
deno install --allow-net --allow-read -d /usr/local/bin file_server https://deno.land/std/http/file_server.ts")
}
fn bundle_subcommand<'a, 'b>() -> App<'a, 'b> {
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))
.about("Bundle module and dependencies into single file")
.long_about(
"Output a single JavaScript file with all dependencies.
If a out_file argument is omitted, the output of the bundle will be sent to
standard out. Examples:
deno bundle https://deno.land/std/examples/colors.ts
deno bundle https://deno.land/std/examples/colors.ts colors.bundle.js",
)
}
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.
Example:
deno completions bash > /usr/local/etc/bash_completion.d/deno.bash
source /usr/local/etc/bash_completion.d/deno.bash",
)
}
fn eval_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("eval")
.about("Eval script")
.long_about(
"Evaluate JavaScript from command-line
This command has implicit access to all permissions (--allow-all)
deno eval \"console.log('hello world')\"",
)
.arg(Arg::with_name("code").takes_value(true).required(true))
.arg(v8_flags_arg())
}
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 source file and cache
Example: 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.
compiled: Local path of compiled source code (TypeScript only)
map: Local path of source map (TypeScript only)
deps: Dependency tree of the source file.
Without any additional arguments 'deno info' shows:
DENO_DIR: directory containing Deno-related files
Remote modules cache: directory containing remote modules
TypeScript compiler cache: directory containing TS compiler output",
)
.arg(Arg::with_name("file").takes_value(true).required(false))
}
fn fetch_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("fetch")
.arg(reload_arg())
.arg(lock_arg())
.arg(lock_write_arg())
.arg(importmap_arg())
.arg(config_arg())
.arg(no_remote_arg())
.arg(
Arg::with_name("file")
.takes_value(true)
.required(true)
.min_values(1),
)
.about("Fetch the dependencies")
.long_about(
"Fetch and compile remote dependencies recursively.
Downloads all statically imported scripts and save them in local
cache, without running the code. No future import network requests
would be made unless --reload is specified.
Downloads all dependencies
deno fetch https://deno.land/std/http/file_server.ts
Once cached, static imports no longer send network requests
deno run -A https://deno.land/std/http/file_server.ts",
)
}
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"),
)
.arg(
Arg::with_name("allow-env")
.long("allow-env")
.help("Allow environment access"),
)
.arg(
Arg::with_name("allow-run")
.long("allow-run")
.help("Allow running subprocesses"),
)
.arg(
Arg::with_name("allow-plugin")
.long("allow-plugin")
.help("Allow loading plugins"),
)
.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"),
)
}
fn run_test_args<'a, 'b>(app: App<'a, 'b>) -> App<'a, 'b> {
permission_args(app)
.arg(importmap_arg())
.arg(reload_arg())
.arg(config_arg())
.arg(lock_arg())
.arg(lock_write_arg())
.arg(no_remote_arg())
.arg(v8_flags_arg())
.arg(
Arg::with_name("cached-only")
.long("cached-only")
.help("Require that remote dependencies are already cached"),
)
.arg(
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 run_subcommand<'a, 'b>() -> App<'a, 'b> {
run_test_args(SubCommand::with_name("run"))
.setting(AppSettings::TrailingVarArg)
.arg(script_arg())
.about("Run a program given a filename or url to the source code")
.long_about(
"Run a program given a filename or url to the source code.
By default all programs are run in sandbox without access to disk, network or
ability to spawn subprocesses.
deno run https://deno.land/std/examples/welcome.ts
With all permissions
deno run -A https://deno.land/std/http/file_server.ts
With only permission to read from disk and listen to network
deno run --allow-net --allow-read https://deno.land/std/http/file_server.ts
With only permission to read whitelist files from disk
deno run --allow-read=/etc https://deno.land/std/http/file_server.ts",
)
}
fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
run_test_args(SubCommand::with_name("test"))
.arg(
Arg::with_name("failfast")
.short("f")
.long("failfast")
.help("Stop on first error")
.takes_value(false),
)
.arg(
Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("Don't show output from test cases")
.takes_value(false),
)
.arg(
Arg::with_name("exclude")
.short("e")
.long("exclude")
.help("List of file names to exclude from run")
.takes_value(true),
)
.arg(
Arg::with_name("files")
.help("List of file names to run")
.takes_value(true)
.multiple(true),
)
.about("Run tests")
.long_about(
"Run tests using test runner
Searches the specified directories for all files that end in _test.ts or
_test.js and executes them.
deno test src/",
)
}
fn script_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("script_arg")
.multiple(true)
.required(true)
.help("script args")
.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")
.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 tsconfig.json configuration file")
.takes_value(true)
}
fn config_arg_parse(flags: &mut DenoFlags, matches: &ArgMatches) {
flags.config_path = matches.value_of("config").map(ToOwned::to_owned);
}
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_BLACKLIST")
.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 reload_arg_parse(flags: &mut DenoFlags, matches: &ArgMatches) {
if matches.is_present("reload") {
if matches.value_of("reload").is_some() {
let cache_bl = matches.values_of("reload").unwrap();
let raw_cache_blacklist: Vec<String> =
cache_bl.map(std::string::ToString::to_string).collect();
flags.cache_blacklist = resolve_urls(raw_cache_blacklist);
debug!("cache blacklist: {:#?}", &flags.cache_blacklist);
flags.reload = false;
} else {
flags.reload = true;
}
}
}
fn importmap_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("importmap")
.long("importmap")
.value_name("FILE")
.help("Load import map file")
.long_help(
"Load import map file
Docs: https://deno.land/std/manual.md#import-maps
Specification: https://wicg.github.io/import-maps/
Examples: https://github.com/WICG/import-maps#the-import-map",
)
.takes_value(true)
}
fn importmap_arg_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
flags.import_map_path = matches.value_of("importmap").map(ToOwned::to_owned);
}
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 v8_flags_arg_parse(flags: &mut DenoFlags, matches: &ArgMatches) {
if let Some(v8_flags) = matches.values_of("v8-flags") {
let s: Vec<String> = v8_flags.map(String::from).collect();
flags.v8_flags = Some(s);
}
}
fn no_remote_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name("no-remote")
.long("no-remote")
.help("Do not resolve remote modules")
}
fn no_remote_arg_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
if matches.is_present("no-remote") {
flags.no_remote = true;
}
}
fn permission_args_parse(flags: &mut DenoFlags, matches: &clap::ArgMatches) {
if matches.is_present("allow-read") {
if matches.value_of("allow-read").is_some() {
let read_wl = matches.values_of("allow-read").unwrap();