Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ xee-xslt-compiler = { path = "../xee-xslt-compiler", version = "0.1.6" }
xee-interpreter = { path = "../xee-interpreter", version = "0.2.0" }
xot = { workspace = true }
clap = { workspace = true, features = ["derive", "cargo"] }
clap_complete = "4.6.0"
ariadne = "0.5.1"
anyhow.workspace = true
regex = { workspace = true }
Expand Down
48 changes: 48 additions & 0 deletions xee/src/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::io;

use clap::{CommandFactory, Parser};
use clap_complete::Shell;

use crate::Cli;

/// Generate shell completion scripts.
///
/// The completion script is written to stdout.
#[derive(Debug, Parser)]
pub(crate) struct Completion {
/// The shell to generate completions for.
pub(crate) shell: Shell,
}

impl Completion {
pub(crate) fn run(&self) -> anyhow::Result<()> {
let mut cmd = Cli::command();
clap_complete::generate(self.shell, &mut cmd, "xee", &mut io::stdout());
Ok(())
}
}

#[cfg(test)]
mod tests {
use clap::ValueEnum;

use super::*;

#[test]
fn verify_cli_for_completion() {
Cli::command().debug_assert();
}

#[test]
fn generate_completions_for_all_shells() {
for shell in Shell::value_variants() {
let mut cmd = Cli::command();
let mut buf = Vec::new();
clap_complete::generate(*shell, &mut cmd, "xee", &mut buf);
assert!(
!buf.is_empty(),
"completion output for {shell} should not be empty"
);
}
}
}
6 changes: 6 additions & 0 deletions xee/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod common;
mod completion;
mod error;
mod format;
mod indent;
Expand Down Expand Up @@ -32,11 +33,16 @@ enum Commands {
Repl(repl::Repl),
/// Transform an XML document using an XSLT stylesheet.
Xslt(xslt::Xslt),
/// Generate shell completion scripts.
Completion(completion::Completion),
}

fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Completion(completion) => {
completion.run()?;
}
Commands::Indent(indent) => {
indent.run()?;
}
Expand Down