use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use clap_complete::Shell;
#[derive(Debug, Parser)]
#[command(
name = "jlic",
version,
about,
long_about = None,
args_conflicts_with_subcommands = true,
subcommand_negates_reqs = true
)]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
#[command(flatten)]
pub new: NewArgs,
}
#[derive(Debug, Subcommand)]
pub enum Command {
New(NewArgs),
List {
#[cfg(feature = "json")]
#[arg(long)]
json: bool,
},
Show {
license: String,
},
Notice {
#[arg(default_value = "mit")]
license: String,
#[command(flatten)]
holder: HolderArgs,
},
Completions {
shell: Shell,
},
Man,
}
#[derive(Debug, Args, Clone)]
pub struct HolderArgs {
#[arg(short, long, value_name = "NAME")]
pub name: Option<String>,
#[arg(short, long, value_name = "EMAIL")]
pub email: Option<String>,
#[arg(short, long, value_name = "YEAR")]
pub year: Option<String>,
#[arg(long, conflicts_with = "email")]
pub no_email: bool,
}
#[derive(Debug, Args, Clone)]
pub struct NewArgs {
#[arg(default_value = "mit")]
pub license: String,
#[command(flatten)]
pub holder: HolderArgs,
#[arg(short, long, value_name = "PATH", default_value = "LICENSE")]
pub output: PathBuf,
#[arg(long, conflicts_with = "output")]
pub stdout: bool,
#[arg(short, long)]
pub force: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn cli_definition_is_valid() {
Cli::command().debug_assert();
}
#[test]
fn bare_invocation_defaults_to_mit_license_file() {
let cli = Cli::parse_from(["jlic"]);
assert!(cli.command.is_none());
assert_eq!(cli.new.license, "mit");
assert_eq!(cli.new.output, PathBuf::from("LICENSE"));
}
#[test]
fn license_can_be_given_without_subcommand() {
let cli = Cli::parse_from(["jlic", "apache-2.0", "--stdout"]);
assert!(cli.command.is_none());
assert_eq!(cli.new.license, "apache-2.0");
assert!(cli.new.stdout);
}
#[test]
fn subcommand_wins_over_positional_license() {
let cli = Cli::parse_from(["jlic", "list"]);
assert!(matches!(cli.command, Some(Command::List { .. })));
}
#[test]
fn stdout_conflicts_with_output() {
assert!(Cli::try_parse_from(["jlic", "mit", "--stdout", "-o", "LIC"]).is_err());
}
}