2025-10-19 17:37:51 +08:00
|
|
|
use clap::{Parser, Subcommand, ValueEnum};
|
|
|
|
|
|
2025-12-30 23:21:01 +08:00
|
|
|
// region: Clap Declaration
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ValueEnum)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub enum RegScope {
|
2025-10-19 17:37:51 +08:00
|
|
|
#[value(name = "user")]
|
|
|
|
|
User,
|
|
|
|
|
#[value(name = "system")]
|
|
|
|
|
System,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 16:30:25 +08:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-17 14:19:26 +08:00
|
|
|
/// Simple program to manage Windows file associations
|
|
|
|
|
#[derive(Parser)]
|
|
|
|
|
#[command(name = "Windows File Association Operator", version, about)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub struct Cli {
|
2025-12-30 23:21:01 +08:00
|
|
|
/// The TOML manifest file representing the complete program
|
2025-10-17 14:19:26 +08:00
|
|
|
#[arg(
|
2025-12-30 23:21:01 +08:00
|
|
|
short = 'm',
|
|
|
|
|
long = "manifest",
|
2026-04-21 16:30:25 +08:00
|
|
|
value_name = "MANIFEST_TOML",
|
2025-10-17 14:19:26 +08:00
|
|
|
required = true
|
|
|
|
|
)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub manifest_file: String,
|
2025-10-17 14:19:26 +08:00
|
|
|
#[command(subcommand)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub command: CliCommands,
|
2025-10-17 14:19:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub enum CliCommands {
|
2025-10-17 14:19:26 +08:00
|
|
|
#[command(name = "register")]
|
2025-10-19 17:37:51 +08:00
|
|
|
#[command(about = "Register application with given manifest and scope.")]
|
|
|
|
|
Register {
|
2026-04-21 16:30:25 +08:00
|
|
|
/// The scope where wfassoc operate.
|
|
|
|
|
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
|
|
|
|
|
target: RegScope,
|
2025-10-19 17:37:51 +08:00
|
|
|
},
|
2025-10-17 14:19:26 +08:00
|
|
|
#[command(name = "unregister")]
|
2025-10-19 17:37:51 +08:00
|
|
|
#[command(about = "Unregister application with given manifest and scope.")]
|
|
|
|
|
Unregister {
|
2026-04-21 16:30:25 +08:00
|
|
|
/// The scope where wfassoc operate.
|
|
|
|
|
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
|
|
|
|
|
target: RegScope,
|
2025-10-19 17:37:51 +08:00
|
|
|
},
|
2025-12-30 23:21:01 +08:00
|
|
|
#[command(name = "status")]
|
2026-04-21 16:30:25 +08:00
|
|
|
#[command(about = "Fetch the status of registration with given manifest and scope.")]
|
|
|
|
|
Status {
|
2026-04-22 14:53:22 +08:00
|
|
|
/// The view where fetch info.
|
2026-05-08 14:14:55 +08:00
|
|
|
#[arg(short = 't', long = "target", value_name = "TARGET", required = true, value_enum, default_value_t = RegScope::User)]
|
|
|
|
|
target: RegScope,
|
2026-04-21 16:30:25 +08:00
|
|
|
},
|
2025-12-30 23:21:01 +08:00
|
|
|
#[command(name = "ext")]
|
|
|
|
|
#[command(about = "File extension related operations according to given program manifest.")]
|
|
|
|
|
Ext {
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
command: CliExtCommands,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
2026-04-21 16:30:25 +08:00
|
|
|
pub enum CliExtCommands {
|
2025-12-30 23:21:01 +08:00
|
|
|
#[command(name = "link")]
|
|
|
|
|
#[command(about = "Link user given file extension to the program declared in manifest file.")]
|
|
|
|
|
Link {
|
2026-04-21 16:30:25 +08:00
|
|
|
/// 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,
|
2025-12-30 23:21:01 +08:00
|
|
|
// 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 {
|
2026-04-21 16:30:25 +08:00
|
|
|
/// 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,
|
2026-05-09 20:07:08 +08:00
|
|
|
// The file extensions (without leading dot) used for this operation. Specify * for all file extension.
|
2025-12-30 23:21:01 +08:00
|
|
|
#[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."
|
|
|
|
|
)]
|
2026-04-21 16:30:25 +08:00
|
|
|
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,
|
|
|
|
|
},
|
2025-12-30 23:21:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
|
|
2026-04-21 16:30:25 +08:00
|
|
|
/// Parse current commandline argument.
|
2025-12-30 23:21:01 +08:00
|
|
|
///
|
|
|
|
|
/// 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.
|
2026-04-21 16:30:25 +08:00
|
|
|
/// This is the mechanism of `clap` crate.
|
|
|
|
|
pub fn parse() -> Cli {
|
|
|
|
|
Cli::parse()
|
2025-10-17 14:19:26 +08:00
|
|
|
}
|