1
0
Files
wfassoc/wfassoc-exec/src/cli.rs
2026-05-09 20:07:08 +08:00

146 lines
4.8 KiB
Rust

use clap::{Parser, Subcommand, ValueEnum};
// region: Clap Declaration
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
pub enum RegScope {
#[value(name = "user")]
User,
#[value(name = "system")]
System,
}
impl From<RegScope> for wfassoc::Scope {
fn from(value: RegScope) -> Self {
match value {
RegScope::User => wfassoc::Scope::User,
RegScope::System => wfassoc::Scope::System,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
pub enum RegView {
#[value(name = "user")]
User,
#[value(name = "system")]
System,
#[value(name = "hybrid")]
Hybrid,
}
impl From<RegView> for wfassoc::View {
fn from(value: RegView) -> Self {
match value {
RegView::User => wfassoc::View::User,
RegView::System => wfassoc::View::System,
RegView::Hybrid => wfassoc::View::Hybrid,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
pub enum ExtListStyle {
#[value(name = "human")]
Human,
#[value(name = "machine")]
Machine,
}
/// Simple program to manage Windows file associations
#[derive(Parser)]
#[command(name = "Windows File Association Operator", version, about)]
pub struct Cli {
/// The TOML manifest file representing the complete program
#[arg(
short = 'm',
long = "manifest",
value_name = "MANIFEST_TOML",
required = true
)]
pub manifest_file: String,
#[command(subcommand)]
pub command: CliCommands,
}
#[derive(Subcommand)]
pub enum CliCommands {
#[command(name = "register")]
#[command(about = "Register application with given manifest and scope.")]
Register {
/// The scope where wfassoc operate.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
target: RegScope,
},
#[command(name = "unregister")]
#[command(about = "Unregister application with given manifest and scope.")]
Unregister {
/// The scope where wfassoc operate.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
target: RegScope,
},
#[command(name = "status")]
#[command(about = "Fetch the status of registration with given manifest and scope.")]
Status {
/// The view where fetch info.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
target: RegScope,
},
#[command(name = "ext")]
#[command(about = "File extension related operations according to given program manifest.")]
Ext {
#[command(subcommand)]
command: CliExtCommands,
},
}
#[derive(Subcommand)]
pub enum CliExtCommands {
#[command(name = "link")]
#[command(about = "Link user given file extension to the program declared in manifest file.")]
Link {
/// The scope where link file extension.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
target: RegScope,
// The file extensions used for this operation. Specify * for all file extension.
#[arg(required = true, value_name = "EXTS", num_args = 1..)]
exts: Vec<String>,
},
#[command(name = "unlink")]
#[command(
about = "Unlink user given file extension from the program declared in manifest file."
)]
Unlink {
/// The scope where unlink file extension.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
target: RegScope,
// The file extensions (without leading dot) used for this operation. Specify * for all file extension.
#[arg(required = true, value_name = "EXTS", num_args = 1..)]
exts: Vec<String>,
},
#[command(name = "list")]
#[command(
about = "List the association status for all extensions declared in given program manifest."
)]
List {
/// The view where list file extension.
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegView::User)]
target: RegView,
/// The style when listing file extensions.
#[arg(short = 's', long = "style", value_name = "STYLE", required = true, value_enum, default_value_t = ExtListStyle::Human)]
style: ExtListStyle,
},
}
// endregion
/// Parse current commandline argument.
///
/// Please note that if there is "help" or "version" command matched,
/// or any command line parse error occurs,
/// this function will order program exit immediately.
/// This is the mechanism of `clap` crate.
pub fn parse() -> Cli {
Cli::parse()
}