Skip to content

Commit dc915c6

Browse files
committed
implement basic help command
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 7e28320 commit dc915c6

File tree

6 files changed

+173
-10
lines changed

6 files changed

+173
-10
lines changed

lpm/cli_parser/src/install.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
#[derive(Debug, PartialEq)]
22
pub enum InstallSubcommand {
3-
None,
43
Local,
4+
Help,
5+
None,
56
}
67

78
impl InstallSubcommand {
89
pub(crate) fn parse(iter: &mut dyn Iterator<Item = &String>) -> Self {
910
if let Some(arg) = iter.next() {
1011
match arg.as_str() {
12+
"--help" | "-h" => Self::Help,
1113
"--local" | "-L" => Self::Local,
1214
_ => Self::None,
1315
}
1416
} else {
1517
Self::None
1618
}
1719
}
20+
21+
pub(crate) fn help() -> &'static str {
22+
"Usage: lpm --install [FLAGS] <Package Name or Path>/[OPTION]
23+
24+
Options:
25+
-h, --help Print help
26+
27+
Flags:
28+
-l, --local Activate installation from local *.lod file
29+
-y, --yes Preaccept the confirmation prompts
30+
"
31+
}
1832
}

lpm/cli_parser/src/lib.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum Command<'a> {
1616
Module(ModuleSubcommand<'a>),
1717
Repository(RepositorySubcommand<'a>),
1818
Version,
19+
Help,
1920
}
2021

2122
#[derive(Default)]
@@ -24,6 +25,56 @@ pub struct CliParser<'a> {
2425
pub force_yes: bool,
2526
}
2627

28+
impl Command<'_> {
29+
pub fn print_help(&self) {
30+
match self {
31+
Command::Install(_pkg_name_or_filepath, _subcommand) => {
32+
println!("{}", InstallSubcommand::help());
33+
}
34+
35+
Command::Update(_pkg_name, _subcommands) => {
36+
println!("{}", UpdateSubcommand::help());
37+
}
38+
39+
Command::Delete(_pkg_name) => {
40+
let help = "Usage: lpm --delete [FLAGS] <Package Name]
41+
42+
Flags:
43+
-y, --yes Preaccept the confirmation prompts
44+
";
45+
println!("{}", help);
46+
}
47+
48+
Command::Module(_subcommand) => {
49+
println!("{}", ModuleSubcommand::help());
50+
}
51+
52+
Command::Repository(_subcommand) => {
53+
println!("{}", RepositorySubcommand::help());
54+
}
55+
56+
Command::Help => {
57+
let help = "Lod Package Manager Command Line Interface
58+
59+
Usage: lpm [SUBCOMMAND] [SUBCOMMAND FLAGS] [SUBCOMMAND OPTIONS]
60+
61+
Subcommands:
62+
-i, --install Install package to system from remote repository or filesystem
63+
-d, --delete Delete package from system
64+
-u, --update Update operations(packages, repository index, lpm database migrations)
65+
-r, --repository Remote repository operations (add, delete, list)
66+
-m, --module Dynamic module operations (add, delete, list, run)
67+
68+
For more specific help, go for `lpm [SUBCOMMAND] --help`
69+
";
70+
println!("{}", help);
71+
}
72+
73+
Command::Version => panic!("This should never happen. Seems like a bug."),
74+
}
75+
}
76+
}
77+
2778
impl CliParser<'_> {
2879
pub fn parse_args(args: &[String]) -> CliParser<'_> {
2980
let mut iter = args.iter().peekable();
@@ -33,9 +84,15 @@ impl CliParser<'_> {
3384
match arg.as_str() {
3485
"--install" | "-i" => {
3586
if let Some(value) = iter.next() {
36-
cli_parser
37-
.commands
38-
.push(Command::Install(value, InstallSubcommand::parse(&mut iter)));
87+
if ["--help", "-h"].contains(&value.as_str()) {
88+
cli_parser
89+
.commands
90+
.push(Command::Install("", InstallSubcommand::Help));
91+
} else {
92+
cli_parser
93+
.commands
94+
.push(Command::Install(value, InstallSubcommand::parse(&mut iter)));
95+
}
3996
}
4097
}
4198
"--update" | "-u" => {
@@ -76,6 +133,9 @@ impl CliParser<'_> {
76133
"--version" | "-v" => {
77134
cli_parser.commands.push(Command::Version);
78135
}
136+
"--help" | "-h" => {
137+
cli_parser.commands.push(Command::Help);
138+
}
79139
_ => {}
80140
}
81141
}

lpm/cli_parser/src/module.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub enum ModuleSubcommand<'a> {
33
Add(Vec<&'a str>),
44
Delete(Vec<&'a str>),
55
List,
6+
Help,
67
None,
78
}
89

@@ -25,10 +26,25 @@ impl<'a> ModuleSubcommand<'a> {
2526
Self::Delete(arguments)
2627
}
2728
"--list" | "-l" => Self::List,
29+
"--help" | "-h" => Self::Help,
2830
_ => Self::None,
2931
}
3032
} else {
3133
Self::None
3234
}
3335
}
36+
37+
pub(crate) fn help() -> &'static str {
38+
"Usage: lpm --module [FLAGS] <Module Name to Run>/[OPTION]
39+
40+
Options:
41+
-a, --add <Module Name> <Dylib Path> Add dynamic module
42+
-d, --delete [<Module Name>] Delete list of dynamic modules
43+
-l, --list List usable dynamic modules on system
44+
-h, --help Print help
45+
46+
Flags:
47+
-y, --yes Preaccept the confirmation prompts
48+
"
49+
}
3450
}

lpm/cli_parser/src/repository.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub enum RepositorySubcommand<'a> {
33
Add(Vec<&'a str>),
44
Delete(Vec<&'a str>),
55
List,
6+
Help,
67
None,
78
}
89

@@ -25,10 +26,25 @@ impl<'a> RepositorySubcommand<'a> {
2526
Self::Delete(arguments)
2627
}
2728
"--list" | "-l" => Self::List,
29+
"--help" | "-h" => Self::Help,
2830
_ => Self::None,
2931
}
3032
} else {
3133
Self::None
3234
}
3335
}
36+
37+
pub(crate) fn help() -> &'static str {
38+
"Usage: lpm --repository [FLAGS] [OPTION]
39+
40+
Options:
41+
-a, --add <Repository Name> <Repository URL> Add package repository
42+
-d, --delete [<Repository Name>] Delete list of package repositories
43+
-l, --list List active package repositories on system
44+
-h, --help Print help
45+
46+
Flags:
47+
-y, --yes Preaccept the confirmation prompts
48+
"
49+
}
3450
}

lpm/cli_parser/src/update.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub enum UpdateSubcommand<'a> {
55
Db,
66
Packages,
77
All,
8+
Help,
89
None,
910
}
1011

@@ -23,10 +24,27 @@ impl<'a> UpdateSubcommand<'a> {
2324
"--packages" | "-p" => Self::Packages,
2425
"--index" | "-i" => Self::Index,
2526
"--db" | "-d" => Self::Db,
27+
"--help" | "-h" => Self::Help,
2628
_ => Self::None,
2729
}
2830
} else {
2931
Self::None
3032
}
3133
}
34+
35+
pub(crate) fn help() -> &'static str {
36+
"Usage: lpm --update [FLAGS] <Package Name or Path>/[OPTION]
37+
38+
Options:
39+
-a, --all Update everything(packages, repository index, db migrations)
40+
-p, --packages Update all the installed packages
41+
-i, --index Update repository index from remote
42+
-d, --db Update lpm database(by applying remote migrations)
43+
-h, --help Print help
44+
45+
Flags:
46+
-l, --local Activate updates from local *.lod file
47+
-y, --yes Preaccept the confirmation prompts
48+
"
49+
}
3250
}

lpm/main/src/main.rs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use cli_parser::{CliParser, Command, InstallSubcommand, ModuleSubcommand, UpdateSubcommand};
1+
use cli_parser::{
2+
CliParser, Command, InstallSubcommand, ModuleSubcommand, RepositorySubcommand, UpdateSubcommand,
3+
};
24
use common::some_or_error;
35
use core::*;
46
use std::{env, panic};
@@ -45,6 +47,11 @@ fn main() {
4547
try_or_error!(install_from_lod_file(ctx(), pkg_name_or_filepath));
4648
}
4749

50+
InstallSubcommand::Help => {
51+
should_print_green_message = false;
52+
command.print_help();
53+
}
54+
4855
InstallSubcommand::None => {
4956
try_or_error!(install_from_repository(ctx(), pkg_name_or_filepath));
5057
}
@@ -85,6 +92,12 @@ fn main() {
8592
try_or_error!(get_and_apply_repository_patches(&core_db()));
8693
try_or_error!(update_pkgs_from_repository(ctx()));
8794
}
95+
96+
UpdateSubcommand::Help => {
97+
should_print_green_message = false;
98+
command.print_help();
99+
}
100+
88101
UpdateSubcommand::None => {
89102
panic!("Invalid command on 'lpm --update'.");
90103
}
@@ -94,13 +107,19 @@ fn main() {
94107

95108
Command::Delete(pkg_name) => {
96109
should_print_green_message = true;
97-
try_or_error!(delete_lod(ctx(), pkg_name));
110+
if ["-h", "--help"].contains(pkg_name) {
111+
should_print_green_message = false;
112+
command.print_help();
113+
} else {
114+
try_or_error!(delete_lod(ctx(), pkg_name));
115+
}
98116
}
99117

100118
Command::Module(subcommand) => match subcommand {
101119
ModuleSubcommand::None => {
102120
try_or_error!(trigger_lpm_module(&core_db(), args.clone()))
103121
}
122+
104123
ModuleSubcommand::Add(list) => {
105124
should_print_green_message = true;
106125
let (module_name, dylib_path) = (
@@ -109,38 +128,58 @@ fn main() {
109128
);
110129
try_or_error!(add_module(ctx(), module_name, dylib_path))
111130
}
131+
112132
ModuleSubcommand::Delete(module_names) => {
113133
should_print_green_message = true;
114134
let module_names: Vec<String> =
115135
module_names.iter().map(|t| t.to_string()).collect();
116136
try_or_error!(delete_modules(ctx(), &module_names))
117137
}
138+
139+
ModuleSubcommand::Help => {
140+
should_print_green_message = false;
141+
command.print_help();
142+
}
143+
118144
ModuleSubcommand::List => try_or_error!(print_modules(ctx())),
119145
},
120146

121147
Command::Repository(subcommand) => match subcommand {
122-
cli_parser::RepositorySubcommand::Add(args) => {
148+
RepositorySubcommand::Add(args) => {
123149
should_print_green_message = true;
124150
let (name, address) = (
125151
some_or_error!(args.first(), "Repository name is missing"),
126152
some_or_error!(args.get(1), "Repository address is missing"),
127153
);
128154
try_or_error!(add_repository(ctx(), name, address));
129155
}
130-
cli_parser::RepositorySubcommand::Delete(repository_names) => {
156+
157+
RepositorySubcommand::Delete(repository_names) => {
131158
should_print_green_message = true;
132159
let repository_names: Vec<String> =
133160
repository_names.iter().map(|t| t.to_string()).collect();
134161
try_or_error!(delete_repositories(ctx(), &repository_names))
135162
}
136-
cli_parser::RepositorySubcommand::List => {
163+
164+
RepositorySubcommand::List => {
137165
try_or_error!(print_repositories(&core_db()))
138166
}
139-
cli_parser::RepositorySubcommand::None => {
167+
168+
RepositorySubcommand::Help => {
169+
should_print_green_message = false;
170+
command.print_help();
171+
}
172+
173+
RepositorySubcommand::None => {
140174
panic!("Invalid command on 'lpm --repository'.");
141175
}
142176
},
143177

178+
Command::Help => {
179+
should_print_green_message = false;
180+
command.print_help();
181+
}
182+
144183
Command::Version => {
145184
println!("lpm version: {}", LPM_VERSION);
146185
}

0 commit comments

Comments
 (0)