feat: deno completions command (#2577)

This commit is contained in:
Bartek Iwańczuk 2019-06-26 12:02:13 +02:00 committed by Ryan Dahl
parent d089f97978
commit 6906a2f75e
3 changed files with 83 additions and 16 deletions

View file

@ -1,7 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use clap::App;
use clap::AppSettings;
use clap::Arg;
use clap::ArgMatches;
use clap::Shell;
use clap::SubCommand;
use crate::deno_dir;
use log::Level;
use std;
use std::str::FromStr;
// Creates vector of strings, Vec<String>
macro_rules! svec {
@ -300,7 +307,7 @@ ability to spawn subprocesses.
// this is a fake subcommand - it's used in conjunction with
// AppSettings:AllowExternalSubcommand to treat it as an
// entry point script
SubCommand::with_name("<script>").about("Script to run"),
SubCommand::with_name("[SCRIPT]").about("Script to run"),
),
).subcommand(
SubCommand::with_name("xeval")
@ -376,13 +383,31 @@ To change installation directory use -d/--dir flag
// this is a fake subcommand - it's used in conjunction with
// AppSettings:AllowExternalSubcommand to treat it as an
// entry point script
SubCommand::with_name("<script>").about("Script URL"),
SubCommand::with_name("[SCRIPT]").about("Script URL"),
),
).subcommand(
SubCommand::with_name("completions")
.settings(&[
AppSettings::DisableHelpSubcommand,
AppSettings::DisableVersion,
]).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")
.arg(
Arg::with_name("shell")
.possible_values(&Shell::variants())
.required(true),
),
).subcommand(
// this is a fake subcommand - it's used in conjunction with
// AppSettings:AllowExternalSubcommand to treat it as an
// entry point script
SubCommand::with_name("<script>").about("Script to run"),
SubCommand::with_name("[SCRIPT]").about("Script to run"),
)
}
@ -537,6 +562,7 @@ const INSTALLER_URL: &str = "https://deno.land/std@b13441f/installer/mod.ts";
#[derive(Debug, PartialEq)]
pub enum DenoSubcommand {
Bundle,
Completions,
Eval,
Fetch,
Info,
@ -590,6 +616,15 @@ pub fn flags_from_vec(
argv.extend(vec![source_file.to_string(), out_file.to_string()]);
DenoSubcommand::Bundle
}
("completions", Some(completions_match)) => {
let shell: &str = completions_match.value_of("shell").unwrap();
create_cli_app().gen_completions_to(
"deno",
Shell::from_str(shell).unwrap(),
&mut std::io::stdout(),
);
DenoSubcommand::Completions
}
("eval", Some(eval_match)) => {
flags.allow_net = true;
flags.allow_env = true;
@ -1392,4 +1427,13 @@ mod tests {
assert_eq!(subcommand, DenoSubcommand::Run);
assert_eq!(argv, svec!["deno", "script.ts"])
}
#[test]
fn test_flags_from_vec_32() {
let (flags, subcommand, argv) =
flags_from_vec(svec!["deno", "completions", "bash"]);
assert_eq!(flags, DenoFlags::default());
assert_eq!(subcommand, DenoSubcommand::Completions);
assert_eq!(argv, svec!["deno"])
}
}

View file

@ -346,6 +346,7 @@ fn main() {
match subcommand {
DenoSubcommand::Bundle => bundle_command(flags, argv),
DenoSubcommand::Completions => {}
DenoSubcommand::Eval => eval_command(flags, argv),
DenoSubcommand::Fetch => fetch_or_info_command(flags, argv, false),
DenoSubcommand::Info => fetch_or_info_command(flags, argv, true),

View file

@ -618,18 +618,19 @@ OPTIONS:
--v8-flags=<v8-flags> Set V8 command line options
SUBCOMMANDS:
<script> Script to run
bundle Bundle module and dependencies into single file
eval Eval script
fetch Fetch the dependencies
fmt Format files
help Prints this message or the help of the given subcommand(s)
info Show source file related info
install Install script as executable
run Run a program given a filename or url to the source code
types Print runtime TypeScript declarations
version Print the version
xeval Eval a script on text segments from stdin
[SCRIPT] Script to run
bundle Bundle module and dependencies into single file
completions Generate shell completions
eval Eval script
fetch Fetch the dependencies
fmt Format files
help Prints this message or the help of the given subcommand(s)
info Show source file related info
install Install script as executable
run Run a program given a filename or url to the source code
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
@ -647,6 +648,27 @@ generated and cached source code is written and read to.
code can test if `NO_COLOR` was set without having `--allow-env` by using the
boolean constant `Deno.noColor`.
### Shell completion
You can generate completion script for your shell using the
`deno completions <shell>` command. The command outputs to stdout so you should
redirect it to an appropriate file.
The supported shells are:
- zsh
- bash
- fish
- powershell
- elvish
Example:
```shellsession
deno completions bash > /usr/local/etc/bash_completion.d/deno.bash
source /usr/local/etc/bash_completion.d/deno.bash
```
### V8 flags
V8 has many many internal command-line flags, that you can see with