Skip to content

Commit

Permalink
⚗️ Add --generate option by clap_complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Jan 7, 2024
1 parent df9e0f0 commit 6c099e5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
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.

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansi_term = "0.12.1"
bytesize = "1.3.0"
chrono = "0.4.31"
clap = { version = "4.4.11", features = ["derive"] }
clap_complete = { version = "4.4.6", optional = true }
glob = "0.3.1"
indicatif = { version = "0.17.7", features = ["improved_unicode"] }
pna = { version = "0.5.0", path = "../pna" }
Expand All @@ -25,6 +26,7 @@ nix = { version = "0.27.1", features = ["user", "fs"] }

[features]
experimental = []
generate = ["experimental", "clap_complete"]

[[bin]]
name = "pna"
Expand Down
4 changes: 2 additions & 2 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bytesize::ByteSize;
use clap::{value_parser, ArgGroup, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Parser, Clone, Eq, PartialEq, Hash, Debug)]
#[command(
name = env!("CARGO_PKG_NAME"),
version,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) enum Verbosity {
Verbose,
}

#[derive(Subcommand, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Subcommand, Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) enum Commands {
#[command(visible_alias = "c", about = "Create archive")]
Create(CreateArgs),
Expand Down
6 changes: 1 addition & 5 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub(super) mod experimental;
pub mod extract;
pub mod list;

#[cfg(feature = "experimental")]
use self::experimental::ExperimentalCommands;
use crate::cli::{CipherAlgorithmArgs, Cli, Commands, PasswordArgs, Verbosity};
use std::io;

Expand All @@ -18,9 +16,7 @@ pub fn entry(cli: Cli) -> io::Result<()> {
Commands::Extract(args) => args.execute(cli.verbosity.verbosity()),
Commands::List(args) => args.execute(cli.verbosity.verbosity()),
#[cfg(feature = "experimental")]
Commands::Experimental(cmd) => match cmd.command {
ExperimentalCommands::Split(cmd) => cmd.execute(cli.verbosity.verbosity()),
},
Commands::Experimental(cmd) => cmd.execute(cli.verbosity.verbosity()),
}
}

Expand Down
41 changes: 39 additions & 2 deletions cli/src/command/experimental.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
#[cfg(feature = "generate")]
use crate::cli::Cli;
use crate::{
cli::Verbosity,
command::{commons::split_to_parts, Command},
utils::part_name,
};
use bytesize::ByteSize;
#[cfg(feature = "generate")]
use clap::CommandFactory;
use clap::{Args, Parser, Subcommand};
#[cfg(feature = "generate")]
use clap_complete::{generate, Generator, Shell};
use pna::{Archive, EntryPart, MIN_CHUNK_BYTES_SIZE, PNA_HEADER};
#[cfg(feature = "generate")]
use std::env;
use std::{fs::File, io, path::PathBuf};

#[derive(Args, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Args, Clone, Eq, PartialEq, Hash, Debug)]
#[command(args_conflicts_with_subcommands = true)]
pub(crate) struct ExperimentalArgs {
#[command(subcommand)]
pub(crate) command: ExperimentalCommands,
pub(crate) command: Option<ExperimentalCommands>,
#[cfg(feature = "generate")]
#[arg(long, help = "Generate shell auto complete")]
pub(crate) generate: Option<Shell>,
}

impl Command for ExperimentalArgs {
fn execute(self, verbosity: Verbosity) -> io::Result<()> {
#[cfg(feature = "generate")]
if let Some(shell) = self.generate {
let cmd = &mut Cli::command();
print_completions(shell, cmd);
return Ok(());
};
match self.command {
Some(ExperimentalCommands::Split(cmd)) => cmd.execute(verbosity),
None => unreachable!(),
}
}
}

#[derive(Subcommand, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand Down Expand Up @@ -91,3 +117,14 @@ fn split_archive(args: SplitCommand, verbosity: Verbosity) -> io::Result<()> {
writer.finalize()?;
Ok(())
}

#[cfg(feature = "generate")]
fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) {
let name = env::args().next().map(|it| PathBuf::from(it)).unwrap();

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure

Check warning

Code scanning / clippy

redundant closure Warning

redundant closure
generate(
gen,
cmd,
name.file_name().unwrap().to_string_lossy(),
&mut io::stdout(),
);
}

0 comments on commit 6c099e5

Please sign in to comment.