-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hinting of allowed value types for zsh completion
Adds new method/attribute `Arg::value_hint`, taking a `ValueHint` enum as argument. The hint can denote accepted values, for example: paths, usernames, hostnames, command names, etc. This initial implementation only supports hints for the zsh completion generator, but support for other shells can be added later.
- Loading branch information
Showing
7 changed files
with
255 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//! Example to test arguments with different ValueHint values. | ||
//! | ||
//! Usage with zsh: | ||
//! ```sh | ||
//! cargo run --example value_hints -- --generate=zsh > /usr/local/share/zsh/site-functions/_value_hints | ||
//! compinit | ||
//! ./target/debug/examples/value_hints --<TAB> | ||
//! ``` | ||
//! fish: | ||
//! ```sh | ||
//! cargo run --example value_hints -- --generate=fish > value_hints.fish | ||
//! . ./value_hints.fish | ||
//! ./target/debug/examples/value_hints --<TAB> | ||
//! ``` | ||
use clap::{App, AppSettings, Arg, ValueHint}; | ||
use clap_generate::generators::{Fish, Zsh}; | ||
use clap_generate::{generate, generators::Bash}; | ||
use std::io; | ||
|
||
const APPNAME: &str = "value_hints"; | ||
|
||
fn build_cli() -> App<'static> { | ||
App::new(APPNAME) | ||
.setting(AppSettings::DisableVersion) | ||
.setting(AppSettings::TrailingVarArg) | ||
.arg( | ||
Arg::with_name("generator") | ||
.long("generate") | ||
.possible_values(&["bash", "fish", "zsh"]), | ||
) | ||
.arg( | ||
Arg::with_name("unknown") | ||
.long("unknown") | ||
.value_hint(ValueHint::Unknown), | ||
) | ||
.arg( | ||
Arg::with_name("path") | ||
.long("path") | ||
.short('p') | ||
.value_hint(ValueHint::AnyPath), | ||
) | ||
.arg( | ||
Arg::with_name("file") | ||
.long("file") | ||
.short('f') | ||
.value_hint(ValueHint::FilePath), | ||
) | ||
.arg( | ||
Arg::with_name("dir") | ||
.long("dir") | ||
.short('d') | ||
.value_hint(ValueHint::DirPath), | ||
) | ||
.arg( | ||
Arg::with_name("exe") | ||
.long("exe") | ||
.short('e') | ||
.value_hint(ValueHint::ExecutablePath), | ||
) | ||
.arg( | ||
Arg::with_name("cmd_name") | ||
.long("cmd-name") | ||
.value_hint(ValueHint::CommandName), | ||
) | ||
.arg( | ||
Arg::with_name("cmd") | ||
.long("cmd") | ||
.short('c') | ||
.value_hint(ValueHint::CommandString), | ||
) | ||
.arg( | ||
Arg::with_name("command_with_args").multiple(true), // .value_hint(ValueHint::CommandWithArguments), | ||
) | ||
.arg( | ||
Arg::with_name("user") | ||
.short('u') | ||
.long("user") | ||
.value_hint(ValueHint::Username), | ||
) | ||
.arg( | ||
Arg::with_name("host") | ||
.short('h') | ||
.long("host") | ||
.value_hint(ValueHint::Hostname), | ||
) | ||
.arg(Arg::with_name("url").long("url").value_hint(ValueHint::Url)) | ||
.arg( | ||
Arg::with_name("email") | ||
.long("email") | ||
.value_hint(ValueHint::EmailAddress), | ||
) | ||
} | ||
|
||
fn main() { | ||
let matches = build_cli().get_matches(); | ||
|
||
if let Some(generator) = matches.value_of("generator") { | ||
let mut app = build_cli(); | ||
eprintln!("Generating completion file for {}...", generator); | ||
match generator { | ||
"bash" => generate::<Bash, _>(&mut app, APPNAME, &mut io::stdout()), | ||
"fish" => generate::<Fish, _>(&mut app, APPNAME, &mut io::stdout()), | ||
"zsh" => generate::<Zsh, _>(&mut app, APPNAME, &mut io::stdout()), | ||
_ => panic!("Unknown generator"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// Provides hints for shell completion. | ||
/// TODO expand documentation | ||
/// TODO more options? | ||
/// TODO serialization/yaml/etc? | ||
#[derive(Debug, PartialEq, Copy, Clone)] | ||
pub enum ValueHint { | ||
/// Default value if hint is not specified | ||
Unknown, | ||
/// Any existing path | ||
AnyPath, | ||
/// Path to a file | ||
FilePath, | ||
/// Path to a directory | ||
DirPath, | ||
/// Path to an executable file | ||
ExecutablePath, | ||
/// Command name, relative to a directory in PATH, or full path to executable | ||
CommandName, | ||
/// A single string containing a command and its arguments | ||
CommandString, | ||
/// A command and each argument as multiple strings. Common for command wrappers. | ||
/// | ||
/// This hint is special, the argument must be a positional argument and use `.multiple(true)`. | ||
/// It's also recommended to use `AppSettings::TrailingVarArg` to avoid confusion about which | ||
/// command the following options belong to. | ||
CommandWithArguments, | ||
/// Name of an operating system user | ||
Username, | ||
/// Host name of a computer | ||
Hostname, | ||
/// Complete web address | ||
Url, | ||
/// Email address | ||
EmailAddress, | ||
} | ||
|
||
impl Default for ValueHint { | ||
fn default() -> Self { | ||
ValueHint::Unknown | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters