implement command root

This commit is contained in:
dusk 2025-10-07 21:59:26 +00:00
parent c1ed7487d7
commit 15ffd16c01
No known key found for this signature in database
11 changed files with 107 additions and 170 deletions

View file

@ -1,5 +1,6 @@
namespace commands {
CommandResult parse_command(string prefix, string input);
string get_related_commands(string prefix, string input);
};
[Enum]
interface CommandResult {

View file

@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Write, usize};
use command_parser::{parameter::ParameterValue, Tree};
use command_parser::{parameter::ParameterValue, token::TokenMatchResult, Tree};
uniffi::include_scaffolding!("commands");
@ -143,3 +143,22 @@ pub fn parse_command(prefix: String, input: String) -> CommandResult {
},
)
}
pub fn get_related_commands(prefix: String, input: String) -> String {
let mut s = String::new();
for command in command_definitions::all() {
if command.tokens.first().map_or(false, |token| {
token
.try_match(Some(&input))
.map_or(false, |r| matches!(r, TokenMatchResult::MatchedValue))
}) {
writeln!(
&mut s,
"- **{prefix}{command}** - *{help}*",
help = command.help
)
.unwrap();
}
}
s
}

View file

@ -4,6 +4,16 @@ use command_parser::Tree;
use commands::COMMAND_TREE;
fn main() {
parse();
}
fn related() {
let cmd = std::env::args().nth(1).unwrap();
let related = commands::get_related_commands("pk;".to_string(), cmd);
println!("Related commands:\n{related}");
}
fn parse() {
let cmd = std::env::args()
.skip(1)
.intersperse(" ".to_string())