use std::io::stdout;
use std::process::exit;
use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::generator::generate;
use clap_complete::{Generator, Shell};
use crate::build::CLAP_LONG_VERSION;
use crate::commands::boilerplate::BoilerplateArgs;
use crate::commands::dependencies::DependenciesArgs;
use crate::commands::print_schema::PrintSchemaArgs;
use crate::commands::publish::PublishArgs;
use crate::commands::setup::SetupArgs;
use crate::commands::vcs::VcsArgs;
use crate::commands::version::VersionArgs;
use crate::commands::workspace::WorkspaceArgs;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
#[clap(name = "repo", long_version = CLAP_LONG_VERSION)]
pub(crate) struct RepoArgs {
#[command(subcommand)]
pub command: RepoCommand,
}
#[derive(Debug, Subcommand)]
pub(crate) enum RepoCommand {
Version(VersionArgs),
Publish(PublishArgs),
Boilerplate(BoilerplateArgs),
Setup(SetupArgs),
Vcs(VcsArgs),
Workspace(WorkspaceArgs),
Dependencies(DependenciesArgs),
Completions(CompletionsArgs),
PrintSchema(PrintSchemaArgs),
}
#[derive(Args, Debug)]
pub(crate) struct CompletionsArgs {
#[clap(verbatim_doc_comment, id = "SHELL")]
shell: Shell,
#[clap(long)]
bin_name: Option<String>,
}
fn completions_for_shell(
cmd: &mut clap::Command,
generator: impl Generator,
bin_name: Option<String>,
) {
generate(
generator,
cmd,
bin_name.unwrap_or_else(|| cmd.get_name().to_owned()),
&mut stdout(),
);
}
pub(crate) fn get_args() -> RepoArgs {
let mut command = RepoArgs::command();
let args = RepoArgs::parse();
if let RepoCommand::Completions(completions_args) = args.command {
completions_for_shell(
&mut command,
completions_args.shell,
completions_args.bin_name,
);
exit(0);
};
args
}
#[cfg(test)]
mod tests {
use crate::args::RepoArgs;
#[test]
fn test_clap_args() {
use clap::CommandFactory;
RepoArgs::command().debug_assert();
}
}