From dc915c6dd38826dc5b452ea2465449d25940721c Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Thu, 9 Nov 2023 19:48:41 +0300 Subject: [PATCH] implement basic help command Signed-off-by: onur-ozkan --- lpm/cli_parser/src/install.rs | 16 +++++++- lpm/cli_parser/src/lib.rs | 66 ++++++++++++++++++++++++++++++-- lpm/cli_parser/src/module.rs | 16 ++++++++ lpm/cli_parser/src/repository.rs | 16 ++++++++ lpm/cli_parser/src/update.rs | 18 +++++++++ lpm/main/src/main.rs | 51 +++++++++++++++++++++--- 6 files changed, 173 insertions(+), 10 deletions(-) diff --git a/lpm/cli_parser/src/install.rs b/lpm/cli_parser/src/install.rs index 6bf4f75..4390b7f 100644 --- a/lpm/cli_parser/src/install.rs +++ b/lpm/cli_parser/src/install.rs @@ -1,13 +1,15 @@ #[derive(Debug, PartialEq)] pub enum InstallSubcommand { - None, Local, + Help, + None, } impl InstallSubcommand { pub(crate) fn parse(iter: &mut dyn Iterator) -> Self { if let Some(arg) = iter.next() { match arg.as_str() { + "--help" | "-h" => Self::Help, "--local" | "-L" => Self::Local, _ => Self::None, } @@ -15,4 +17,16 @@ impl InstallSubcommand { Self::None } } + + pub(crate) fn help() -> &'static str { + "Usage: lpm --install [FLAGS] /[OPTION] + +Options: + -h, --help Print help + +Flags: + -l, --local Activate installation from local *.lod file + -y, --yes Preaccept the confirmation prompts +" + } } diff --git a/lpm/cli_parser/src/lib.rs b/lpm/cli_parser/src/lib.rs index 1c9f0c5..2321535 100644 --- a/lpm/cli_parser/src/lib.rs +++ b/lpm/cli_parser/src/lib.rs @@ -16,6 +16,7 @@ pub enum Command<'a> { Module(ModuleSubcommand<'a>), Repository(RepositorySubcommand<'a>), Version, + Help, } #[derive(Default)] @@ -24,6 +25,56 @@ pub struct CliParser<'a> { pub force_yes: bool, } +impl Command<'_> { + pub fn print_help(&self) { + match self { + Command::Install(_pkg_name_or_filepath, _subcommand) => { + println!("{}", InstallSubcommand::help()); + } + + Command::Update(_pkg_name, _subcommands) => { + println!("{}", UpdateSubcommand::help()); + } + + Command::Delete(_pkg_name) => { + let help = "Usage: lpm --delete [FLAGS] { + println!("{}", ModuleSubcommand::help()); + } + + Command::Repository(_subcommand) => { + println!("{}", RepositorySubcommand::help()); + } + + Command::Help => { + let help = "Lod Package Manager Command Line Interface + +Usage: lpm [SUBCOMMAND] [SUBCOMMAND FLAGS] [SUBCOMMAND OPTIONS] + +Subcommands: + -i, --install Install package to system from remote repository or filesystem + -d, --delete Delete package from system + -u, --update Update operations(packages, repository index, lpm database migrations) + -r, --repository Remote repository operations (add, delete, list) + -m, --module Dynamic module operations (add, delete, list, run) + +For more specific help, go for `lpm [SUBCOMMAND] --help` +"; + println!("{}", help); + } + + Command::Version => panic!("This should never happen. Seems like a bug."), + } + } +} + impl CliParser<'_> { pub fn parse_args(args: &[String]) -> CliParser<'_> { let mut iter = args.iter().peekable(); @@ -33,9 +84,15 @@ impl CliParser<'_> { match arg.as_str() { "--install" | "-i" => { if let Some(value) = iter.next() { - cli_parser - .commands - .push(Command::Install(value, InstallSubcommand::parse(&mut iter))); + if ["--help", "-h"].contains(&value.as_str()) { + cli_parser + .commands + .push(Command::Install("", InstallSubcommand::Help)); + } else { + cli_parser + .commands + .push(Command::Install(value, InstallSubcommand::parse(&mut iter))); + } } } "--update" | "-u" => { @@ -76,6 +133,9 @@ impl CliParser<'_> { "--version" | "-v" => { cli_parser.commands.push(Command::Version); } + "--help" | "-h" => { + cli_parser.commands.push(Command::Help); + } _ => {} } } diff --git a/lpm/cli_parser/src/module.rs b/lpm/cli_parser/src/module.rs index efddda2..ce9be1f 100644 --- a/lpm/cli_parser/src/module.rs +++ b/lpm/cli_parser/src/module.rs @@ -3,6 +3,7 @@ pub enum ModuleSubcommand<'a> { Add(Vec<&'a str>), Delete(Vec<&'a str>), List, + Help, None, } @@ -25,10 +26,25 @@ impl<'a> ModuleSubcommand<'a> { Self::Delete(arguments) } "--list" | "-l" => Self::List, + "--help" | "-h" => Self::Help, _ => Self::None, } } else { Self::None } } + + pub(crate) fn help() -> &'static str { + "Usage: lpm --module [FLAGS] /[OPTION] + +Options: + -a, --add Add dynamic module + -d, --delete [] Delete list of dynamic modules + -l, --list List usable dynamic modules on system + -h, --help Print help + +Flags: + -y, --yes Preaccept the confirmation prompts +" + } } diff --git a/lpm/cli_parser/src/repository.rs b/lpm/cli_parser/src/repository.rs index 61726de..4b7fa5e 100644 --- a/lpm/cli_parser/src/repository.rs +++ b/lpm/cli_parser/src/repository.rs @@ -3,6 +3,7 @@ pub enum RepositorySubcommand<'a> { Add(Vec<&'a str>), Delete(Vec<&'a str>), List, + Help, None, } @@ -25,10 +26,25 @@ impl<'a> RepositorySubcommand<'a> { Self::Delete(arguments) } "--list" | "-l" => Self::List, + "--help" | "-h" => Self::Help, _ => Self::None, } } else { Self::None } } + + pub(crate) fn help() -> &'static str { + "Usage: lpm --repository [FLAGS] [OPTION] + +Options: + -a, --add Add package repository + -d, --delete [] Delete list of package repositories + -l, --list List active package repositories on system + -h, --help Print help + +Flags: + -y, --yes Preaccept the confirmation prompts +" + } } diff --git a/lpm/cli_parser/src/update.rs b/lpm/cli_parser/src/update.rs index 9fa0c2d..2af1a6b 100644 --- a/lpm/cli_parser/src/update.rs +++ b/lpm/cli_parser/src/update.rs @@ -5,6 +5,7 @@ pub enum UpdateSubcommand<'a> { Db, Packages, All, + Help, None, } @@ -23,10 +24,27 @@ impl<'a> UpdateSubcommand<'a> { "--packages" | "-p" => Self::Packages, "--index" | "-i" => Self::Index, "--db" | "-d" => Self::Db, + "--help" | "-h" => Self::Help, _ => Self::None, } } else { Self::None } } + + pub(crate) fn help() -> &'static str { + "Usage: lpm --update [FLAGS] /[OPTION] + +Options: + -a, --all Update everything(packages, repository index, db migrations) + -p, --packages Update all the installed packages + -i, --index Update repository index from remote + -d, --db Update lpm database(by applying remote migrations) + -h, --help Print help + +Flags: + -l, --local Activate updates from local *.lod file + -y, --yes Preaccept the confirmation prompts +" + } } diff --git a/lpm/main/src/main.rs b/lpm/main/src/main.rs index 0375bd0..325c5ba 100644 --- a/lpm/main/src/main.rs +++ b/lpm/main/src/main.rs @@ -1,4 +1,6 @@ -use cli_parser::{CliParser, Command, InstallSubcommand, ModuleSubcommand, UpdateSubcommand}; +use cli_parser::{ + CliParser, Command, InstallSubcommand, ModuleSubcommand, RepositorySubcommand, UpdateSubcommand, +}; use common::some_or_error; use core::*; use std::{env, panic}; @@ -45,6 +47,11 @@ fn main() { try_or_error!(install_from_lod_file(ctx(), pkg_name_or_filepath)); } + InstallSubcommand::Help => { + should_print_green_message = false; + command.print_help(); + } + InstallSubcommand::None => { try_or_error!(install_from_repository(ctx(), pkg_name_or_filepath)); } @@ -85,6 +92,12 @@ fn main() { try_or_error!(get_and_apply_repository_patches(&core_db())); try_or_error!(update_pkgs_from_repository(ctx())); } + + UpdateSubcommand::Help => { + should_print_green_message = false; + command.print_help(); + } + UpdateSubcommand::None => { panic!("Invalid command on 'lpm --update'."); } @@ -94,13 +107,19 @@ fn main() { Command::Delete(pkg_name) => { should_print_green_message = true; - try_or_error!(delete_lod(ctx(), pkg_name)); + if ["-h", "--help"].contains(pkg_name) { + should_print_green_message = false; + command.print_help(); + } else { + try_or_error!(delete_lod(ctx(), pkg_name)); + } } Command::Module(subcommand) => match subcommand { ModuleSubcommand::None => { try_or_error!(trigger_lpm_module(&core_db(), args.clone())) } + ModuleSubcommand::Add(list) => { should_print_green_message = true; let (module_name, dylib_path) = ( @@ -109,17 +128,24 @@ fn main() { ); try_or_error!(add_module(ctx(), module_name, dylib_path)) } + ModuleSubcommand::Delete(module_names) => { should_print_green_message = true; let module_names: Vec = module_names.iter().map(|t| t.to_string()).collect(); try_or_error!(delete_modules(ctx(), &module_names)) } + + ModuleSubcommand::Help => { + should_print_green_message = false; + command.print_help(); + } + ModuleSubcommand::List => try_or_error!(print_modules(ctx())), }, Command::Repository(subcommand) => match subcommand { - cli_parser::RepositorySubcommand::Add(args) => { + RepositorySubcommand::Add(args) => { should_print_green_message = true; let (name, address) = ( some_or_error!(args.first(), "Repository name is missing"), @@ -127,20 +153,33 @@ fn main() { ); try_or_error!(add_repository(ctx(), name, address)); } - cli_parser::RepositorySubcommand::Delete(repository_names) => { + + RepositorySubcommand::Delete(repository_names) => { should_print_green_message = true; let repository_names: Vec = repository_names.iter().map(|t| t.to_string()).collect(); try_or_error!(delete_repositories(ctx(), &repository_names)) } - cli_parser::RepositorySubcommand::List => { + + RepositorySubcommand::List => { try_or_error!(print_repositories(&core_db())) } - cli_parser::RepositorySubcommand::None => { + + RepositorySubcommand::Help => { + should_print_green_message = false; + command.print_help(); + } + + RepositorySubcommand::None => { panic!("Invalid command on 'lpm --repository'."); } }, + Command::Help => { + should_print_green_message = false; + command.print_help(); + } + Command::Version => { println!("lpm version: {}", LPM_VERSION); }