use clap::{Arg, command, Command};
use AppCommand::{CreateTaskCommand, DeleteTaskCommand, GetTasksCommand};
use crate::commands::{AppCommand, CreateTask, DeleteTask, GetTasks};
pub(crate) fn parse(_: Vec<String>) -> AppCommand {
let matches = command!()
.subcommand(Command::new("create")
.about("Create resource")
.args([
Arg::new("resource")
.required(true)
.value_parser(["task"]),
]))
.subcommand(Command::new("delete")
.about("Delete resource")
.arg(Arg::new("resource")
.required(true)
.value_parser(["task"])
)
.arg(Arg::new("id")
.required(false)
)
)
.subcommand(Command::new("get")
.about("Get resource")
.arg(
Arg::new("resource")
.value_parser(["tasks"])
.required(true)
)
)
.get_matches();
if let Some(arg_m) = matches.subcommand_matches("create") {
if let Some(arg_m) = arg_m.get_one::<String>("resource") {
return match arg_m.as_str() {
"task" => CreateTaskCommand(CreateTask),
&_ => panic!("Not valid value = {}", arg_m.as_str())
};
}
}
if let Some(command) = matches.subcommand_matches("delete") {
if let Some(resource) = command.get_one::<String>("resource") {
match resource.as_str() {
"task" => {
if let Some(id) = command.get_one::<String>("id") {
return DeleteTaskCommand(DeleteTask {
id: id.parse().expect("Resource id must be a number.")
});
}
}
&_ => panic!("Not valid value = {}", resource.as_str())
};
}
}
if let Some(command) = matches.subcommand_matches("get") {
if let Some(resource) = command.get_one::<String>("resource") {
return match resource.as_str() {
"tasks" => {
GetTasksCommand(GetTasks::new(None, None))
}
&_ => panic!("Not valid value = {}", resource.as_str())
};
}
}
panic!("Parse error!")
}