From cec0fa8726ac312cb88b4f94987fadb3535153c7 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Sat, 18 Feb 2023 18:20:42 +0100 Subject: [PATCH] rework argument parser --- TODO.md | 4 +- crates/pacdef_core/src/args.rs | 117 ++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 53 deletions(-) diff --git a/TODO.md b/TODO.md index f06ed76..5c8a7a9 100644 --- a/TODO.md +++ b/TODO.md @@ -3,4 +3,6 @@ - make transition for existing user easier - create an outer error type for main - change version names (1.0.0-beta.10) -- make table use section header with brackets \ No newline at end of file +- make table use section header with brackets +- tutorial +- remove "Arch" references diff --git a/crates/pacdef_core/src/args.rs b/crates/pacdef_core/src/args.rs index 0dc25ab..399bbb9 100644 --- a/crates/pacdef_core/src/args.rs +++ b/crates/pacdef_core/src/args.rs @@ -8,63 +8,76 @@ use crate::action::*; use crate::core::get_version_string; fn get_arg_parser() -> Command { + let package_cmd = get_package_cmd(); + let group_cmd = get_group_cmd(); + let version_cmd = Command::new(VERSION).about("show version info"); + Command::new("pacdef") - .about("declarative package manager for Arch Linux") + .about("declarative package manager for Linux") .version(get_version_string()) .subcommand_required(true) .arg_required_else_help(true) - .subcommand(Command::new(CLEAN).about("remove unmanaged packages")) - .subcommand( - Command::new(EDIT) - .about("edit one or more existing group files") - .arg_required_else_help(true) - .arg(Arg::new("group").num_args(1..)), + .subcommands([package_cmd, group_cmd, version_cmd]) +} + +fn get_group_cmd() -> Command { + let remove = Command::new(REMOVE) + .about("remove one or more previously imported groups") + .arg_required_else_help(true) + .arg(Arg::new("groups").num_args(1..)); + + let edit = Command::new(EDIT) + .about("edit one or more existing group files") + .arg_required_else_help(true) + .arg(Arg::new("group").num_args(1..)); + + let list = Command::new(GROUPS).about("list names of imported groups"); + + let import = Command::new(IMPORT) + .about("import one or more group files") + .arg_required_else_help(true) + .arg(Arg::new("files").num_args(1..)); + + let new = Command::new(NEW) + .about("create new group files") + .arg_required_else_help(true) + .arg( + Arg::new("edit") + .short('e') + .long("edit") + .help("edit the new group files after creation") + .action(clap::ArgAction::SetTrue), ) - .subcommand(Command::new(GROUPS).about("show names of imported groups")) - .subcommand( - Command::new(IMPORT) - .about("import one or more group files") - .arg_required_else_help(true) - .arg(Arg::new("files").num_args(1..)), - ) - .subcommand( - Command::new(NEW) - .about("create new group files") - .arg_required_else_help(true) - .arg( - Arg::new("edit") - .short('e') - .long("edit") - .help("edit the new group files after creation") - .action(clap::ArgAction::SetTrue), - ) - .arg(Arg::new("groups").num_args(1..)), - ) - .subcommand( - Command::new(REMOVE) - .about("remove one or more previously imported groups") - .arg_required_else_help(true) - .arg(Arg::new("groups").num_args(1..)), - ) - .subcommand(Command::new(REVIEW).about("review unmanaged packages")) - .subcommand( - Command::new(SEARCH) - .about("search for packages which match a provided string literal or regex") - .arg_required_else_help(true) - .arg(Arg::new("string")), - ) - .subcommand( - Command::new(SHOW) - .about("show packages under an imported group") - .arg_required_else_help(true) - .arg(Arg::new("group").num_args(1..)), - ) - .subcommand(Command::new(SYNC).about("install packages from all imported groups")) - .subcommand( - Command::new(UNMANAGED) - .about("show explicitly installed packages not managed by pacdef"), - ) - .subcommand(Command::new(VERSION).about("show version info")) + .arg(Arg::new("groups").num_args(1..)); + + let show = Command::new(SHOW) + .about("show packages under an imported group") + .arg_required_else_help(true) + .arg(Arg::new("group").num_args(1..)); + + Command::new("group") + .arg_required_else_help(true) + .about("TODO????") + .subcommand_required(true) + .subcommands([edit, import, list, new, remove, show]) +} + +fn get_package_cmd() -> Command { + let sync = Command::new(SYNC).about("install packages from all imported groups"); + let clean = Command::new(CLEAN).about("remove unmanaged packages"); + let unmanaged = + Command::new(UNMANAGED).about("show explicitly installed packages not managed by pacdef"); + let review = Command::new(REVIEW).about("review unmanaged packages"); + let search = Command::new(SEARCH) + .about("search for packages which match a provided string literal or regex") + .arg_required_else_help(true) + .arg(Arg::new("string")); + + Command::new("package") + .arg_required_else_help(true) + .about("TODO????") + .subcommand_required(true) + .subcommands([clean, review, unmanaged, search, sync]) } /// Get and parse the CLI arguments.