Skip to content

Commit

Permalink
feat: generate shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
bomgar committed Nov 24, 2023
1 parent 7762342 commit 4f9f5c3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ regex = "1"
mlua = { version = "0.9", features = ["lua54", "vendored"] }
lazy_static = "1.4.0"
handlebars = "4"
clap_complete = "4.4.4"

[dependencies.clap]
version = "4"
Expand Down
11 changes: 10 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::substitution::Substitution;
use crate::template;
use clap::{crate_version, ArgAction};
use clap::{crate_version, value_parser, ArgAction, ValueHint};
use clap::{Arg, Command};
use clap_complete::Shell;

pub fn app() -> Command {
Command::new("fblog")
Expand All @@ -17,6 +18,13 @@ pub fn app() -> Command {
.conflicts_with("excluded-value")
.help("adds additional values"),
)
.arg(
Arg::new("generate-completions")
.long("generate-completions")
.action(ArgAction::Set)
.hide(true)
.value_parser(value_parser!(Shell)),
)
.arg(
Arg::new("message-key")
.long("message-key")
Expand Down Expand Up @@ -92,6 +100,7 @@ pub fn app() -> Command {
Arg::new("INPUT")
.help("Sets the input file to use, otherwise assumes stdin")
.required(false)
.value_hint(ValueHint::AnyPath)
.default_value("-"),
)
.arg(
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod substitution;
mod template;

use crate::log::LogSettings;
use clap_complete::{generate, Shell};
use std::fs;
use substitution::Substitution;

Expand All @@ -21,6 +22,14 @@ fn main() {

let mut log_settings = LogSettings::new_default_settings();

if let Some(generator) = matches.get_one::<Shell>("generate-completions").copied() {
let mut app = app::app();
let name = app.get_name().to_string();
eprintln!("Generating completion file for {generator}...");
generate(generator, &mut app, name, &mut io::stdout());
return;
}

if let Some(values) = matches.get_many::<String>("additional-value") {
log_settings.add_additional_values(values.map(ToOwned::to_owned).collect());
}
Expand Down

0 comments on commit 4f9f5c3

Please sign in to comment.