mirror of
https://github.com/denoland/deno.git
synced 2025-01-09 07:39:15 -05:00
refactor: reorganize flags (#3389)
- Remove ability to specify run arguments like `--allow-net` after the script argument. It's too hacky to make work with clap. - Remove `--v8-options`, instead use `--v8-flags=--help` - Give more descriptive names to unit tests in flags.rs - Assume argv and subcommand into DenoFlags struct so the output of flags module is only DenoFlags rather than the tuple (subcommand, flags, argv). - Improve CLI help text - Make `deno run` specific args like `--allow-net` only show up in 'deno help run' instead of as global flags in `deno help`. - Removes `deno version` to simplify our implementation and be closer to clap defaults. `deno -V` now only shows Deno's version and not V8's nor TypeScript. `Deno.versions` can be used to see that information. - Prevent clap from auto-detecting terminal width and attempting to wrap text.
This commit is contained in:
parent
f88dc4e197
commit
c016684653
6 changed files with 1378 additions and 1380 deletions
2599
cli/flags.rs
2599
cli/flags.rs
File diff suppressed because it is too large
Load diff
66
cli/lib.rs
66
cli/lib.rs
|
@ -95,7 +95,6 @@ impl log::Log for Logger {
|
|||
|
||||
fn create_worker_and_state(
|
||||
flags: DenoFlags,
|
||||
argv: Vec<String>,
|
||||
) -> (Worker, ThreadSafeGlobalState) {
|
||||
use crate::shell::Shell;
|
||||
use std::sync::Arc;
|
||||
|
@ -111,6 +110,8 @@ fn create_worker_and_state(
|
|||
}
|
||||
});
|
||||
|
||||
// TODO(ry) Remove argv param from ThreadSafeGlobalState::new.
|
||||
let argv = flags.argv.clone();
|
||||
let global_state = ThreadSafeGlobalState::new(flags, argv, progress)
|
||||
.map_err(deno_error::print_err_and_exit)
|
||||
.unwrap();
|
||||
|
@ -248,11 +249,12 @@ async fn print_file_info(worker: Worker, module_specifier: ModuleSpecifier) {
|
|||
}
|
||||
}
|
||||
|
||||
fn info_command(flags: DenoFlags, argv: Vec<String>) {
|
||||
let (mut worker, state) = create_worker_and_state(flags, argv.clone());
|
||||
fn info_command(flags: DenoFlags) {
|
||||
let argv_len = flags.argv.len();
|
||||
let (mut worker, state) = create_worker_and_state(flags);
|
||||
|
||||
// If it was just "deno info" print location of caches and exit
|
||||
if argv.len() == 1 {
|
||||
if argv_len == 1 {
|
||||
return print_cache_info(worker);
|
||||
}
|
||||
|
||||
|
@ -276,8 +278,8 @@ fn info_command(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn fetch_command(flags: DenoFlags, argv: Vec<String>) {
|
||||
let (mut worker, state) = create_worker_and_state(flags, argv.clone());
|
||||
fn fetch_command(flags: DenoFlags) {
|
||||
let (mut worker, state) = create_worker_and_state(flags);
|
||||
|
||||
let main_module = state.main_module.as_ref().unwrap().clone();
|
||||
|
||||
|
@ -294,8 +296,8 @@ fn fetch_command(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn eval_command(flags: DenoFlags, argv: Vec<String>) {
|
||||
let (mut worker, state) = create_worker_and_state(flags, argv);
|
||||
fn eval_command(flags: DenoFlags) {
|
||||
let (mut worker, state) = create_worker_and_state(flags);
|
||||
let ts_source = state.argv[1].clone();
|
||||
// Force TypeScript compile.
|
||||
let main_module =
|
||||
|
@ -322,15 +324,11 @@ fn eval_command(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn bundle_command(flags: DenoFlags, argv: Vec<String>) {
|
||||
let (worker, state) = create_worker_and_state(flags, argv);
|
||||
|
||||
fn bundle_command(flags: DenoFlags) {
|
||||
let out_file = flags.bundle_output.clone();
|
||||
let (worker, state) = create_worker_and_state(flags);
|
||||
let main_module = state.main_module.as_ref().unwrap().clone();
|
||||
assert!(state.argv.len() >= 2);
|
||||
let out_file = match state.argv.len() {
|
||||
3 => Some(state.argv[2].clone()),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
debug!(">>>>> bundle_async START");
|
||||
// NOTE: we need to poll `worker` otherwise TS compiler worker won't run properly
|
||||
let main_future = async move {
|
||||
|
@ -351,8 +349,8 @@ fn bundle_command(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn run_repl(flags: DenoFlags, argv: Vec<String>) {
|
||||
let (mut worker, _state) = create_worker_and_state(flags, argv);
|
||||
fn run_repl(flags: DenoFlags) {
|
||||
let (mut worker, _state) = create_worker_and_state(flags);
|
||||
// Setup runtime.
|
||||
js_check(worker.execute("denoMain()"));
|
||||
let main_future = async move {
|
||||
|
@ -363,9 +361,9 @@ fn run_repl(flags: DenoFlags, argv: Vec<String>) {
|
|||
tokio_util::run(main_future);
|
||||
}
|
||||
|
||||
fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
||||
fn run_script(flags: DenoFlags) {
|
||||
let use_current_thread = flags.current_thread;
|
||||
let (mut worker, state) = create_worker_and_state(flags, argv);
|
||||
let (mut worker, state) = create_worker_and_state(flags);
|
||||
|
||||
let main_module = state.main_module.as_ref().unwrap().clone();
|
||||
// Normal situation of executing a module.
|
||||
|
@ -406,22 +404,18 @@ fn run_script(flags: DenoFlags, argv: Vec<String>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn version_command() {
|
||||
println!("deno: {}", version::DENO);
|
||||
println!("v8: {}", version::v8());
|
||||
println!("typescript: {}", version::TYPESCRIPT);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
#[cfg(windows)]
|
||||
ansi_term::enable_ansi_support().ok(); // For Windows 10
|
||||
|
||||
log::set_logger(&LOGGER).unwrap();
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let (flags, subcommand, argv) = flags::flags_from_vec(args);
|
||||
let flags = flags::flags_from_vec(args);
|
||||
|
||||
if let Some(ref v8_flags) = flags.v8_flags {
|
||||
v8_set_flags(v8_flags.clone());
|
||||
let mut v8_flags_ = v8_flags.clone();
|
||||
v8_flags_.insert(0, "UNUSED_BUT_NECESSARY_ARG0".to_string());
|
||||
v8_set_flags(v8_flags_);
|
||||
}
|
||||
|
||||
let log_level = match flags.log_level {
|
||||
|
@ -430,15 +424,15 @@ pub fn main() {
|
|||
};
|
||||
log::set_max_level(log_level.to_level_filter());
|
||||
|
||||
match subcommand {
|
||||
DenoSubcommand::Bundle => bundle_command(flags, argv),
|
||||
match flags.subcommand {
|
||||
DenoSubcommand::Bundle => bundle_command(flags),
|
||||
DenoSubcommand::Completions => {}
|
||||
DenoSubcommand::Eval => eval_command(flags, argv),
|
||||
DenoSubcommand::Fetch => fetch_command(flags, argv),
|
||||
DenoSubcommand::Info => info_command(flags, argv),
|
||||
DenoSubcommand::Repl => run_repl(flags, argv),
|
||||
DenoSubcommand::Run => run_script(flags, argv),
|
||||
DenoSubcommand::Eval => eval_command(flags),
|
||||
DenoSubcommand::Fetch => fetch_command(flags),
|
||||
DenoSubcommand::Info => info_command(flags),
|
||||
DenoSubcommand::Repl => run_repl(flags),
|
||||
DenoSubcommand::Run => run_script(flags),
|
||||
DenoSubcommand::Types => types_command(),
|
||||
DenoSubcommand::Version => version_command(),
|
||||
_ => panic!("bad subcommand"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -168,17 +168,17 @@ itest!(_012_async {
|
|||
});
|
||||
|
||||
itest!(_013_dynamic_import {
|
||||
args: "013_dynamic_import.ts --reload --allow-read",
|
||||
args: "run --reload --allow-read 013_dynamic_import.ts",
|
||||
output: "013_dynamic_import.ts.out",
|
||||
});
|
||||
|
||||
itest!(_014_duplicate_import {
|
||||
args: "014_duplicate_import.ts --reload --allow-read",
|
||||
args: "run --reload --allow-read 014_duplicate_import.ts ",
|
||||
output: "014_duplicate_import.ts.out",
|
||||
});
|
||||
|
||||
itest!(_015_duplicate_parallel_import {
|
||||
args: "015_duplicate_parallel_import.js --reload --allow-read",
|
||||
args: "run --reload --allow-read 015_duplicate_parallel_import.js",
|
||||
output: "015_duplicate_parallel_import.js.out",
|
||||
});
|
||||
|
||||
|
@ -418,7 +418,7 @@ itest!(lock_check_err {
|
|||
});
|
||||
|
||||
itest!(lock_check_err2 {
|
||||
args: "run 019_media_types.ts --lock=lock_check_err2.json",
|
||||
args: "run --lock=lock_check_err2.json 019_media_types.ts",
|
||||
output: "lock_check_err2.out",
|
||||
check_stderr: true,
|
||||
exit_code: 10,
|
||||
|
@ -538,7 +538,7 @@ itest!(error_013_missing_script {
|
|||
});
|
||||
|
||||
itest!(error_014_catch_dynamic_import_error {
|
||||
args: "error_014_catch_dynamic_import_error.js --reload --allow-read",
|
||||
args: "run --reload --allow-read error_014_catch_dynamic_import_error.js",
|
||||
output: "error_014_catch_dynamic_import_error.js.out",
|
||||
exit_code: 1,
|
||||
});
|
||||
|
@ -641,25 +641,10 @@ itest!(v8_flags {
|
|||
});
|
||||
|
||||
itest!(v8_help {
|
||||
args: "--v8-options",
|
||||
args: "run --v8-flags=--help",
|
||||
output: "v8_help.out",
|
||||
});
|
||||
|
||||
itest!(version {
|
||||
args: "version",
|
||||
output: "version.out",
|
||||
});
|
||||
|
||||
itest!(version_long_flag {
|
||||
args: "--version",
|
||||
output: "version.out",
|
||||
});
|
||||
|
||||
itest!(version_short_flag {
|
||||
args: "-v",
|
||||
output: "version.out",
|
||||
});
|
||||
|
||||
itest!(wasm {
|
||||
args: "run wasm.ts",
|
||||
output: "wasm.ts.out",
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
deno:[WILDCARD]
|
||||
v8:[WILDCARD]
|
||||
typescript:[WILDCARD]
|
|
@ -494,7 +494,7 @@ test({
|
|||
async fn(): Promise<void> {
|
||||
// Runs a simple server as another process
|
||||
const p = Deno.run({
|
||||
args: [Deno.execPath(), "http/testdata/simple_server.ts", "--allow-net"],
|
||||
args: [Deno.execPath(), "--allow-net", "http/testdata/simple_server.ts"],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
||||
|
@ -535,9 +535,9 @@ test({
|
|||
const p = Deno.run({
|
||||
args: [
|
||||
Deno.execPath(),
|
||||
"http/testdata/simple_https_server.ts",
|
||||
"--allow-net",
|
||||
"--allow-read"
|
||||
"--allow-read",
|
||||
"http/testdata/simple_https_server.ts"
|
||||
],
|
||||
stdout: "piped"
|
||||
});
|
||||
|
|
|
@ -676,56 +676,39 @@ if (import.meta.main) {
|
|||
Use `deno help` to see the help text.
|
||||
|
||||
```
|
||||
deno
|
||||
A secure JavaScript and TypeScript runtime
|
||||
|
||||
Docs: https://deno.land/manual.html
|
||||
Docs: https://deno.land/std/manual.md
|
||||
Modules: https://deno.land/x/
|
||||
Bugs: https://github.com/denoland/deno/issues
|
||||
|
||||
To run the REPL:
|
||||
To run the REPL supply no arguments:
|
||||
|
||||
deno
|
||||
|
||||
To execute a sandboxed script:
|
||||
|
||||
deno https://deno.land/std/examples/welcome.ts
|
||||
|
||||
To evaluate code from the command line:
|
||||
|
||||
deno eval "console.log(30933 + 404)"
|
||||
|
||||
To get help on the another subcommands (run in this case):
|
||||
To execute a script:
|
||||
|
||||
deno help run
|
||||
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.
|
||||
|
||||
USAGE:
|
||||
deno [OPTIONS] [SUBCOMMAND]
|
||||
deno [SUBCOMMAND]
|
||||
|
||||
OPTIONS:
|
||||
-A, --allow-all Allow all permissions
|
||||
--allow-env Allow environment access
|
||||
--allow-hrtime Allow high resolution time measurement
|
||||
--allow-net=<allow-net> Allow network access
|
||||
--allow-read=<allow-read> Allow file system read access
|
||||
--allow-run Allow running subprocesses
|
||||
--allow-write=<allow-write> Allow file system write access
|
||||
-c, --config <FILE> Load compiler configuration file
|
||||
--current-thread Use tokio::runtime::current_thread
|
||||
-h, --help Prints help information
|
||||
--importmap <FILE> Load import map file
|
||||
--lock <FILE> Check the specified lock file
|
||||
--lock-write Write lock file. Use with --lock.
|
||||
-L, --log-level <log-level> Set log level [possible values: debug, info]
|
||||
--no-fetch Do not download remote modules
|
||||
-r, --reload=<CACHE_BLACKLIST> Reload source code cache (recompile TypeScript)
|
||||
--seed <NUMBER> Seed Math.random()
|
||||
--v8-flags=<v8-flags> Set V8 command line options
|
||||
--v8-options Print V8 command line options
|
||||
-v, --version Print the version
|
||||
-h, --help Prints help information
|
||||
-L, --log-level <log-level> Set log level [possible values: debug, info]
|
||||
-V, --version Prints version information
|
||||
|
||||
SUBCOMMANDS:
|
||||
[SCRIPT] Script to run
|
||||
bundle Bundle module and dependencies into single file
|
||||
completions Generate shell completions
|
||||
eval Eval script
|
||||
|
@ -734,17 +717,17 @@ SUBCOMMANDS:
|
|||
help Prints this message or the help of the given subcommand(s)
|
||||
info Show info about cache or info related to source file
|
||||
install Install script as executable
|
||||
repl Read Eval Print Loop
|
||||
run Run a program given a filename or url to the source code
|
||||
test Run tests
|
||||
types Print runtime TypeScript declarations
|
||||
version Print the version
|
||||
xeval Eval a script on text segments from stdin
|
||||
|
||||
ENVIRONMENT VARIABLES:
|
||||
DENO_DIR Set deno's base directory
|
||||
NO_COLOR Set to disable color
|
||||
HTTP_PROXY Set proxy address for HTTP requests (module downloads, fetch)
|
||||
HTTPS_PROXY Set proxy address for HTTPS requests (module downloads, fetch)
|
||||
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
|
||||
```
|
||||
|
||||
### Environmental variables
|
||||
|
|
Loading…
Reference in a new issue