Skip to content
Merged
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions flycomp/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static"]

[target.aarch64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static"]
2 changes: 2 additions & 0 deletions flycomp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ anyhow = "1.0.102"
clap = { version = "4.6.1", features = ["derive"] }
clap_complete = { version = "4.6.3", features = ["unstable-dynamic"] }
log = "0.4"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
6 changes: 3 additions & 3 deletions flycomp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ──────────────────────────────────────────────────────────────────────────────

/// A single command-line argument / flag.
#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize)]
pub struct Arg {
/// Long flag name, e.g. `--verbose`.
pub long: Option<String>,
Expand All @@ -26,7 +26,7 @@ pub struct Arg {
}

/// A parsed command (or sub-command).
#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize)]
pub struct Command {
/// Name of the command, if known.
pub name: Option<String>,
Expand Down Expand Up @@ -842,7 +842,7 @@ fn find_subcommand_mut<'a>(root: &'a mut Command, path: &[String]) -> Option<&'a
///
/// Many tools print their help to *stderr* rather than *stdout*; this function
/// returns whichever stream is non-empty (preferring stdout).
pub(crate) fn run_help(command_path: &str, extra_args: &[&str]) -> anyhow::Result<String> {
pub fn run_help(command_path: &str, extra_args: &[&str]) -> anyhow::Result<String> {
let output = std::process::Command::new(command_path)
.args(extra_args)
.arg("--help")
Expand Down
39 changes: 34 additions & 5 deletions flycomp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
use clap::Parser;

#[derive(Clone, Debug, clap::ValueEnum)]
#[value(rename_all = "lower")]
enum OutputFormat {
Bash,
Elvish,
Fish,
Powershell,
Zsh,
Json,
}

#[derive(Parser, Debug)]
#[command(name = "flycomp")]
#[command(about = "Generate shell completions from COMMAND --help output")]
struct CliArgs {
/// Command name or path to synthesize completions for.
command: String,
/// Output shell type (defaults to bash).
#[arg(long, value_enum, default_value_t = clap_complete::Shell::Bash)]
shell: clap_complete::Shell,
/// Output format (defaults to bash).
#[arg(long, value_enum, default_value_t = OutputFormat::Bash)]
output: OutputFormat,
}

fn main() -> anyhow::Result<()> {
let args = CliArgs::parse();
let script = flycomp::generate_completion_script(&args.command, args.shell)?;
print!("{}", script);

if matches!(args.output, OutputFormat::Json) {
let parsed_cmd = flycomp::synthesize_completion(&args.command, |extra_args| {
flycomp::run_help(&args.command, extra_args)
})?;
let json = serde_json::to_string_pretty(&parsed_cmd)?;
println!("{}", json);
} else {
let shell = match args.output {
OutputFormat::Bash => clap_complete::Shell::Bash,
OutputFormat::Elvish => clap_complete::Shell::Elvish,
OutputFormat::Fish => clap_complete::Shell::Fish,
OutputFormat::Powershell => clap_complete::Shell::PowerShell,
OutputFormat::Zsh => clap_complete::Shell::Zsh,
OutputFormat::Json => unreachable!(),
};
let script = flycomp::generate_completion_script(&args.command, shell)?;
print!("{}", script);
}

Ok(())
}