Skip to content

Commit

Permalink
feat: shell completion generator
Browse files Browse the repository at this point in the history
  • Loading branch information
hms5232 committed Dec 3, 2024
1 parent 0211159 commit 87ef95d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
26 changes: 18 additions & 8 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 @@ -8,5 +8,6 @@ description = "A program to parse, format and export ufw log."

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
clap_complete = "4.5.38"
csv = "1.3.0"
indicatif = "0.17.8"
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{CommandFactory, Parser, Subcommand};
use clap::{CommandFactory, Parser, Subcommand, ValueHint};
use clap_complete::generate;

mod export;
mod parser;
Expand All @@ -25,6 +26,12 @@ fn main() {
_ => println!("Current not support other format"),
}
}
Some(SubCommands::Completion { shell }) => {
// generate shell completion
let mut app = Cli::command();
let name = app.get_name().to_owned();
generate(*shell, &mut app, name, &mut std::io::stdout());
}
_ => {
// show help message
Cli::command().print_help().unwrap();
Expand All @@ -33,7 +40,7 @@ fn main() {
}

#[derive(Parser)]
#[command(name = "ufwlog", version, about, long_about = None)]
#[command(name = "ufwlog", bin_name = "ufwlog", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<SubCommands>,
Expand All @@ -46,6 +53,7 @@ struct Cli {
long,
value_name = "log_path",
global = true,
value_hint = ValueHint::FilePath,
default_value = "/var/log/ufw.log"
)]
log_path: Option<String>,
Expand All @@ -56,6 +64,7 @@ struct Cli {
long,
value_name = "log_path",
global = true,
value_hint = ValueHint::FilePath,
default_value = "./ufw.log"
)]
log_path: Option<String>,
Expand All @@ -74,8 +83,14 @@ enum SubCommands {
short,
long = "output",
value_name = "filename",
value_hint = ValueHint::AnyPath,
default_value = "ufwlog"
)]
output_filename: Option<String>,
},
/// Generate shell completion.
Completion {
#[arg(value_name = "shell", value_enum)]
shell: clap_complete::Shell,
},
}

0 comments on commit 87ef95d

Please sign in to comment.