librius 0.5.1

A personal library manager CLI written in Rust.
Documentation
use crate::fields::EDITABLE_FIELDS;
use crate::i18n::{tr, tr_s};
use clap::{Arg, ArgAction, Command};

/// Costruisce la CLI localizzata usando le stringhe già caricate in memoria.
pub fn build_cli() -> Command {
    Command::new(tr_s("app_name"))
        .version(env!("CARGO_PKG_VERSION"))
        .about(tr_s("app_about"))
        .disable_help_flag(true)
        .disable_help_subcommand(true)
        .arg(
            Arg::new("help")
                .short('h')
                .long("help")
                .help(tr_s("help_flag_about"))
                .action(ArgAction::Help)
                .global(true)
                .help_heading(tr_s("help.global_options"))
                .display_order(1),
        )
        .arg(
            Arg::new("verbose")
                .short('v')
                .long("verbose")
                .help(tr_s("help_verbose"))
                .action(ArgAction::SetTrue)
                .global(true)
                .help_heading(tr_s("help.global_options"))
                .display_order(2),
        )
        .arg(
            Arg::new("lang")
                .short('l')
                .long("lang")
                .help(tr_s("help_lang"))
                .global(true)
                .num_args(1)
                .help_heading(tr_s("help.global_options"))
                .display_order(3),
        )
        // 📘 list command
        .subcommand(
            Command::new("list")
                .about(tr_s("list_about"))
                .display_order(10)
                .arg(
                    Arg::new("short")
                        .long("short")
                        .help(tr_s("help.list.short"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.list_specific_options"))
                        .display_order(11),
                )
                .arg(
                    Arg::new("id")
                        .long("id")
                        .help(tr_s("help.list.id"))
                        .value_name("ID")
                        .num_args(1)
                        .value_parser(clap::value_parser!(i32))
                        .help_heading(tr_s("help.list_specific_options"))
                        .display_order(12),
                )
                .arg(
                    Arg::new("details")
                        .long("details")
                        .help(tr_s("help.list.details"))
                        .requires("id")
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.list_specific_options"))
                        .display_order(13),
                )
                .arg(
                    Arg::new("compact")
                        .long("compact")
                        .help(tr_s("help.list.compact"))
                        .requires("details")
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.list_specific_options"))
                        .display_order(14),
                ),
        )
        // 🔍 search command
        .subcommand(
            Command::new("search")
                .about(tr_s("search_about"))
                .display_order(15)
                .arg(
                    Arg::new("query")
                        .help(tr_s("search_query_help"))
                        .required(true)
                        .value_name("QUERY")
                        .num_args(1)
                        .help_heading(tr_s("help.search_specific_options"))
                        .display_order(16),
                )
                .arg(
                    Arg::new("short")
                        .long("short")
                        .help(tr_s("search_short_help"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.search_specific_options"))
                        .display_order(17),
                ),
        )
        // ➕ add book command
        .subcommand(
            Command::new("add")
                .about(tr("help.add.about"))
                .display_order(20)
                .subcommand(
                    Command::new("book")
                        .about(tr("help.add.book.about"))
                        .display_order(21)
                        .arg(
                            Arg::new("isbn")
                                .long("isbn")
                                .help(tr_s("help.add.book.isbn"))
                                .required(true)
                                .value_name("ISBN")
                                .help_heading(tr_s("help.add_specific_options"))
                                .display_order(22),
                        ),
                ),
        )
        // ✏️ edit book command (aggiornato con logica ibrida ID/ISBN)
        .subcommand(
            Command::new("edit")
                .about(tr("help.edit.about"))
                .display_order(30)
                .subcommand({
                    let mut cmd = Command::new("book")
                        .about(tr("help.edit.book.about"))
                        .display_order(31)
                        .arg(
                            Arg::new("key")
                                .help(tr_s("help.edit.book.key"))
                                .required(true)
                                .num_args(1)
                                .help_heading(tr_s("help.edit_specific_options"))
                                .display_order(32),
                        );

                    // ✅ Aggiunta dinamica di tutti i campi editabili
                    for (i, (name, help, short)) in EDITABLE_FIELDS.iter().enumerate() {
                        cmd = cmd.arg(
                            Arg::new(*name)
                                .long(*name)
                                .short(*short)
                                .help(tr_s(help))
                                .num_args(1)
                                .action(ArgAction::Set)
                                .help_heading(tr_s("help.edit_specific_options"))
                                .display_order(40 + i),
                        );
                    }

                    cmd
                }),
        )
        .subcommand(
            Command::new("del")
                .about(tr("help.del.about"))
                .display_order(50)
                .arg(
                    Arg::new("key")
                        .help(tr("help.del.key")) // e.g. "Book ID or ISBN to delete"
                        .required(true)
                        .value_name("ID|ISBN")
                        .num_args(1)
                        .display_order(51),
                )
                .arg(
                    Arg::new("force")
                        .long("force")
                        .short('f')
                        .help(tr("help.del.force")) // e.g. "Force deletion without confirmation"
                        .action(ArgAction::SetTrue)
                        .num_args(0)
                        .display_order(52),
                ),
        )
        // ⚙️ config command
        .subcommand(
            Command::new("config")
                .about(tr_s("config_about"))
                .display_order(60)
                .arg(
                    Arg::new("init")
                        .long("init")
                        .help(tr_s("config_init_help"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.config_specific_options"))
                        .display_order(61),
                )
                .arg(
                    Arg::new("print")
                        .long("print")
                        .help(tr_s("config_print_help"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.config_specific_options"))
                        .display_order(62),
                )
                .arg(
                    Arg::new("edit")
                        .long("edit")
                        .help(tr_s("config_edit_help"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.config_specific_options"))
                        .display_order(63),
                )
                .arg(
                    Arg::new("editor")
                        .long("editor")
                        .requires("edit")
                        .num_args(1)
                        .help(tr_s("config_editor_help"))
                        .help_heading(tr_s("help.config_specific_options"))
                        .display_order(64),
                ),
        )
        .subcommand(
            Command::new("db")
                .about(tr_s("db_about"))
                .display_order(65)
                .arg(
                    Arg::new("init")
                        .long("init")
                        .help(tr_s("db_init_help"))
                        .action(ArgAction::SetTrue)
                        .conflicts_with_all(["reset", "copy"])
                        .help_heading(tr_s("help.db_specific_options"))
                        .display_order(66),
                )
                .arg(
                    Arg::new("reset")
                        .long("reset")
                        .help(tr_s("db_reset_help"))
                        .action(ArgAction::SetTrue)
                        .conflicts_with_all(["init", "copy"])
                        .help_heading(tr_s("help.db_specific_options"))
                        .display_order(67),
                )
                .arg(
                    Arg::new("copy")
                        .long("copy")
                        .help(tr_s("db_copy_help"))
                        .action(ArgAction::SetTrue)
                        .conflicts_with_all(["init", "reset"])
                        .help_heading(tr_s("help.db_specific_options"))
                        .display_order(68),
                )
                .arg(
                    Arg::new("file")
                        .short('f')
                        .long("file")
                        .help(tr_s("db_file_help"))
                        .requires("copy")
                        .num_args(1)
                        .value_name("PATH")
                        .help_heading(tr_s("help.db_specific_options"))
                        .display_order(69),
                ),
        )
        // 💾 backup command
        .subcommand(
            Command::new("backup")
                .about(tr_s("backup_about"))
                .display_order(75)
                .arg(
                    Arg::new("compress")
                        .long("compress")
                        .help(tr_s("backup_compress_help"))
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.backup_specific_options"))
                        .display_order(76),
                ),
        )
        // 📤 export command
        .subcommand(
            Command::new("export")
                .about(tr_s("export_about"))
                .display_order(80)
                .arg(
                    Arg::new("csv")
                        .long("csv")
                        .help(tr_s("export_csv_help"))
                        .action(ArgAction::SetTrue)
                        .conflicts_with_all(["xlsx", "json"])
                        .help_heading(tr_s("help.export_specific_options"))
                        .display_order(81),
                )
                .arg(
                    Arg::new("xlsx")
                        .long("xlsx")
                        .help(tr_s("export_xlsx_help"))
                        .conflicts_with_all(["csv", "json"])
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.export_specific_options"))
                        .display_order(82),
                )
                .arg(
                    Arg::new("json")
                        .long("json")
                        .help(tr_s("export_json_help"))
                        .conflicts_with_all(["csv", "xlsx"])
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.export_specific_options"))
                        .display_order(83),
                )
                .arg(
                    Arg::new("output")
                        .short('o')
                        .long("output")
                        .help(tr_s("export_output_help"))
                        .value_name("FILE")
                        .required(false)
                        .help_heading(tr_s("help.export_specific_options"))
                        .display_order(84),
                ),
        )
        // 📥 import command
        .subcommand(
            Command::new("import")
                .about(tr_s("import_about"))
                .display_order(90)
                .arg(
                    Arg::new("file")
                        .short('f')
                        .long("file")
                        .help(tr_s("import_file_help"))
                        .required(true)
                        .value_name("PATH")
                        .help_heading(tr_s("help.import_specific_options"))
                        .display_order(91),
                )
                .arg(
                    Arg::new("csv")
                        .long("csv")
                        .help(tr_s("import_csv_help"))
                        .action(ArgAction::SetTrue)
                        .conflicts_with("json")
                        .help_heading(tr_s("help.import_specific_options"))
                        .display_order(92),
                )
                .arg(
                    Arg::new("json")
                        .long("json")
                        .help(tr_s("import_json_help"))
                        .conflicts_with("csv")
                        .action(ArgAction::SetTrue)
                        .help_heading(tr_s("help.import_specific_options"))
                        .display_order(93),
                )
                .arg(
                    Arg::new("delimiter")
                        .short('d')
                        .long("delimiter")
                        .help(tr_s("import_delimiter_help"))
                        .num_args(1)
                        .value_name("CHAR")
                        .required(false)
                        .value_parser(clap::builder::NonEmptyStringValueParser::new())
                        .help_heading(tr_s("help.import_specific_options"))
                        .display_order(94),
                ),
        )
        .subcommand(
            Command::new("help")
                .about(tr_s("help_flag_about"))
                .display_order(200)
                .arg(
                    Arg::new("command")
                        .value_name("COMMAND")
                        .help(tr_s("help_lang"))
                        .num_args(1)
                        .display_order(201),
                ),
        )
}