mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-14 17:50:13 +00:00
add missing -yes flags to command definitions, use log crate instead of printlns in command parser, also accept double dash for flags
This commit is contained in:
parent
8431255930
commit
12655fb539
16 changed files with 130 additions and 53 deletions
|
|
@ -16,7 +16,8 @@ pub fn cmds() -> impl IntoIterator<Item = Command> {
|
|||
command!(ap_account, Toggle => "cfg_ap_account_update")
|
||||
.help("Toggles autoproxy globally for the current account"),
|
||||
command!(ap_timeout => "cfg_ap_timeout_show").help("Shows the autoproxy timeout"),
|
||||
command!(ap_timeout, RESET => "cfg_ap_timeout_reset").help("Resets the autoproxy timeout"),
|
||||
command!(ap_timeout, RESET => "cfg_ap_timeout_reset")
|
||||
.help("Resets the autoproxy timeout"),
|
||||
command!(ap_timeout, parameter::Toggle::Off => "cfg_ap_timeout_off")
|
||||
.help("Disables the autoproxy timeout"),
|
||||
command!(ap_timeout, ("timeout", OpaqueString) => "cfg_ap_timeout_update")
|
||||
|
|
@ -26,8 +27,10 @@ pub fn cmds() -> impl IntoIterator<Item = Command> {
|
|||
let timezone_tokens = tokens!(cfg, ("timezone", ["zone", "tz"]));
|
||||
let timezone = [
|
||||
command!(timezone_tokens => "cfg_timezone_show").help("Shows the system timezone"),
|
||||
command!(timezone_tokens, RESET => "cfg_timezone_reset").help("Resets the system timezone"),
|
||||
command!(timezone_tokens, RESET => "cfg_timezone_reset")
|
||||
.help("Resets the system timezone"),
|
||||
command!(timezone_tokens, ("timezone", OpaqueString) => "cfg_timezone_update")
|
||||
.flag(YES)
|
||||
.help("Changes your system's time zone"),
|
||||
];
|
||||
|
||||
|
|
@ -168,7 +171,8 @@ pub fn cmds() -> impl IntoIterator<Item = Command> {
|
|||
let name_format_short = tokens!(cfg, ("nameformat", ["nf"]));
|
||||
let name_formatting = [
|
||||
command!(name_format => "cfg_name_format_show").help("Shows the name format"),
|
||||
command!(name_format, RESET => "cfg_name_format_reset").help("Resets the name format"),
|
||||
command!(name_format, RESET => "cfg_name_format_reset")
|
||||
.help("Resets the name format"),
|
||||
command!(name_format, ("format", OpaqueString) => "cfg_name_format_update")
|
||||
.help("Changes your system's username formatting"),
|
||||
command!(name_format_short => "cfg_name_format_show").help("Shows the name format"),
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
let group_new = tokens!(group, ("new", ["n"]));
|
||||
let group_new_cmd = once(
|
||||
command!(group_new, Remainder(("name", OpaqueString)) => "group_new")
|
||||
.flag(YES)
|
||||
.help("Creates a new group"),
|
||||
);
|
||||
|
||||
|
|
@ -37,9 +38,9 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
let group_name_cmd = [
|
||||
command!(group_name => "group_show_name").help("Shows the group's name"),
|
||||
command!(group_name, CLEAR => "group_clear_name")
|
||||
.flag(YES)
|
||||
.help("Clears the group's name"),
|
||||
command!(group_name, Remainder(("name", OpaqueString)) => "group_rename")
|
||||
.flag(YES)
|
||||
.help("Renames a group"),
|
||||
];
|
||||
|
||||
|
|
@ -48,7 +49,6 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
command!(group_display_name => "group_show_display_name")
|
||||
.help("Shows the group's display name"),
|
||||
command!(group_display_name, CLEAR => "group_clear_display_name")
|
||||
.flag(YES)
|
||||
.help("Clears the group's display name"),
|
||||
command!(group_display_name, Remainder(("name", OpaqueString)) => "group_change_display_name")
|
||||
.help("Changes the group's display name"),
|
||||
|
|
@ -65,7 +65,6 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
command!(group_description => "group_show_description")
|
||||
.help("Shows the group's description"),
|
||||
command!(group_description, CLEAR => "group_clear_description")
|
||||
.flag(YES)
|
||||
.help("Clears the group's description"),
|
||||
command!(group_description, Remainder(("description", OpaqueString)) => "group_change_description")
|
||||
.help("Changes the group's description"),
|
||||
|
|
@ -98,7 +97,6 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
let group_color_cmd = [
|
||||
command!(group_color => "group_show_color").help("Shows the group's color"),
|
||||
command!(group_color, CLEAR => "group_clear_color")
|
||||
.flag(YES)
|
||||
.help("Clears the group's color"),
|
||||
command!(group_color, ("color", OpaqueString) => "group_change_color")
|
||||
.help("Changes a group's color"),
|
||||
|
|
@ -126,7 +124,6 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
|
||||
let group_delete_cmd = [
|
||||
command!(group_target, ("delete", ["destroy", "erase", "yeet"]) => "group_delete")
|
||||
.flag(YES)
|
||||
.help("Deletes a group"),
|
||||
];
|
||||
|
||||
|
|
@ -151,7 +148,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
let group_modify_members_cmd = [
|
||||
command!(group_target, "add", Optional(MemberRefs) => "group_add_member")
|
||||
.help("Adds one or more members to a group")
|
||||
.flag(ALL).flag(YES),
|
||||
.flag(ALL),
|
||||
command!(group_target, ("remove", ["rem", "rm"]), Optional(MemberRefs) => "group_remove_member")
|
||||
.help("Removes one or more members from a group")
|
||||
.flag(ALL).flag(YES),
|
||||
|
|
|
|||
|
|
@ -36,7 +36,9 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
let delete = ("delete", ["del", "remove"]);
|
||||
|
||||
let member_new_cmd = once(
|
||||
command!(member, new, ("name", OpaqueString) => "member_new").help("Creates a new member"),
|
||||
command!(member, new, ("name", OpaqueString) => "member_new")
|
||||
.flag(YES)
|
||||
.help("Creates a new member"),
|
||||
);
|
||||
|
||||
let member_info_cmd = once(
|
||||
|
|
@ -161,6 +163,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
command!(member_proxy => "member_proxy_show")
|
||||
.help("Shows a member's proxy tags"),
|
||||
command!(member_proxy, ("add", ["a"]), ("tag", OpaqueString) => "member_proxy_add")
|
||||
.flag(YES)
|
||||
.help("Adds proxy tag to a member"),
|
||||
command!(member_proxy, ("remove", ["r", "rm"]), ("tag", OpaqueString) => "member_proxy_remove")
|
||||
.help("Removes proxy tag from a member"),
|
||||
|
|
@ -168,6 +171,7 @@ pub fn cmds() -> impl Iterator<Item = Command> {
|
|||
.flag(YES)
|
||||
.help("Clears all proxy tags from a member"),
|
||||
command!(member_proxy, Remainder(("tags", OpaqueString)) => "member_proxy_set")
|
||||
.flag(YES)
|
||||
.help("Sets a member's proxy tags"),
|
||||
]
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::utils::get_list_flags;
|
|||
use super::*;
|
||||
|
||||
pub fn cmds() -> impl Iterator<Item = Command> {
|
||||
let random = ("random", ["rand"]);
|
||||
let random = ("random", ["rand", "r"]);
|
||||
let group = group::group();
|
||||
let member = member::member();
|
||||
|
||||
|
|
|
|||
|
|
@ -21,14 +21,17 @@ pub fn cmds() -> impl IntoIterator<Item = Command> {
|
|||
.help("Shows help for switch commands"),
|
||||
command!(switch, out => "switch_out").help("Registers a switch with no members"),
|
||||
command!(switch, delete => "switch_delete")
|
||||
.flag(YES)
|
||||
.help("Deletes the latest switch")
|
||||
.flag(("all", ["clear", "c"])),
|
||||
command!(switch, r#move, Remainder(OpaqueString) => "switch_move")
|
||||
.flag(YES)
|
||||
.help("Moves the latest switch in time"), // TODO: datetime parsing
|
||||
command!(switch, edit, out => "switch_edit_out")
|
||||
.help("Turns the latest switch into a switch-out")
|
||||
.flag(YES),
|
||||
command!(switch, edit, Optional(MemberRefs) => "switch_edit")
|
||||
.flag(YES)
|
||||
.help("Edits the members in the latest switch")
|
||||
.flags(edit_flags),
|
||||
command!(switch, copy, Optional(MemberRefs) => "switch_copy")
|
||||
|
|
|
|||
|
|
@ -230,15 +230,24 @@ pub fn edit() -> impl Iterator<Item = Command> {
|
|||
command!(system, Optional(SystemRef), front => "system_fronter")
|
||||
.help("Shows a system's fronter(s)"),
|
||||
make_front_history(tokens!(front, ("history", ["h"]))),
|
||||
make_front_history(tokens!(("fronthistory", ["fh"]))),
|
||||
make_front_history(tokens!(("fronthistory", ["fh", "history", "switches"]))),
|
||||
make_front_percent(tokens!(front, ("percent", ["p", "%"]))),
|
||||
make_front_percent(tokens!(("frontpercent", ["fp"]))),
|
||||
make_front_percent(tokens!((
|
||||
"frontpercent",
|
||||
["fp", "front%", "frontbreakdown"]
|
||||
))),
|
||||
];
|
||||
|
||||
let search_param = Optional(Remainder(("query", OpaqueString)));
|
||||
let apply_list_opts = |cmd: Command| cmd.flags(get_list_flags());
|
||||
|
||||
let members_subcmd = tokens!(("members", ["l", "ls", "list"]), search_param);
|
||||
let members_subcmd = tokens!(
|
||||
(
|
||||
"members",
|
||||
["l", "ls", "list", "find", "search", "query", "fd"]
|
||||
),
|
||||
search_param
|
||||
);
|
||||
let system_members_cmd = [
|
||||
command!(system, Optional(SystemRef), members_subcmd => "system_members")
|
||||
.help("Lists a system's members"),
|
||||
|
|
@ -265,6 +274,7 @@ pub fn edit() -> impl Iterator<Item = Command> {
|
|||
|
||||
let system_link = [
|
||||
command!("link", ("account", UserRef) => "system_link")
|
||||
.flag(YES)
|
||||
.help("Links another Discord account to your system"),
|
||||
command!("unlink", ("account", OpaqueString) => "system_unlink")
|
||||
.help("Unlinks a Discord account from your system")
|
||||
|
|
|
|||
|
|
@ -8,4 +8,5 @@ lazy_static = { workspace = true }
|
|||
smol_str = "0.3.2"
|
||||
ordermap = "0.5"
|
||||
regex = "1"
|
||||
strsim = "0.11"
|
||||
strsim = "0.11"
|
||||
log = "0.4"
|
||||
|
|
@ -18,6 +18,7 @@ use std::{collections::HashMap, usize};
|
|||
|
||||
use command::Command;
|
||||
use flag::{Flag, FlagMatchError, FlagValueMatchError};
|
||||
use log::debug;
|
||||
use parameter::ParameterValue;
|
||||
use smol_str::SmolStr;
|
||||
use string::MatchedFlag;
|
||||
|
|
@ -82,9 +83,9 @@ pub fn parse_command(
|
|||
(_, Token::Parameter(_)) => std::cmp::Ordering::Less,
|
||||
_ => std::cmp::Ordering::Equal,
|
||||
});
|
||||
println!("possible: {:?}", possible_tokens);
|
||||
debug!("possible: {:?}", possible_tokens);
|
||||
let next = next_token(possible_tokens.iter().cloned(), &input, current_pos);
|
||||
println!("next: {:?}", next);
|
||||
debug!("next: {:?}", next);
|
||||
match &next {
|
||||
Some((found_token, result, new_pos)) => {
|
||||
match &result {
|
||||
|
|
@ -156,7 +157,7 @@ pub fn parse_command(
|
|||
.pop()
|
||||
.and_then(|m| matches!(m.token, Token::Parameter(_)).then_some(m))
|
||||
{
|
||||
println!("redoing previous branch: {:?}", state.token);
|
||||
debug!("redoing previous branch: {:?}", state.token);
|
||||
local_tree = state.tree;
|
||||
current_pos = state.start_pos; // reset position to previous branch's start
|
||||
filtered_tokens = state.filtered_tokens; // reset filtered tokens to the previous branch's
|
||||
|
|
@ -251,7 +252,7 @@ pub fn parse_command(
|
|||
// match flags until there are none left
|
||||
while let Some(matched_flag) = string::next_flag(&input, current_pos) {
|
||||
current_pos = matched_flag.next_pos;
|
||||
println!("flag matched {matched_flag:?}");
|
||||
debug!("flag matched {matched_flag:?}");
|
||||
raw_flags.push((current_token_idx, matched_flag));
|
||||
}
|
||||
// if we have a command, stop parsing and return it (only if there is no remaining input)
|
||||
|
|
@ -355,7 +356,7 @@ pub fn parse_command(
|
|||
flags.insert(name.to_string(), value.clone());
|
||||
}
|
||||
|
||||
println!("{} {flags:?} {params:?}", full_cmd.cb);
|
||||
debug!("{} {flags:?} {params:?}", full_cmd.cb);
|
||||
return Ok(ParsedCommand {
|
||||
command_def: full_cmd.clone(),
|
||||
flags,
|
||||
|
|
@ -371,7 +372,7 @@ fn match_flag<'a>(
|
|||
) -> Option<Result<(SmolStr, Option<ParameterValue>), (&'a Flag, FlagMatchError)>> {
|
||||
// check for all (possible) flags, see if token matches
|
||||
for flag in possible_flags {
|
||||
println!("matching flag {flag:?}");
|
||||
debug!("matching flag {flag:?}");
|
||||
match flag.try_match(matched_flag.name, matched_flag.value) {
|
||||
Some(Ok(param)) => return Some(Ok((flag.get_name().into(), param))),
|
||||
Some(Err(err)) => return Some(Err((flag, err))),
|
||||
|
|
@ -397,7 +398,7 @@ fn next_token<'a>(
|
|||
) -> Option<(Token, TokenMatchResult, usize)> {
|
||||
// get next parameter, matching quotes
|
||||
let matched = string::next_param(&input, current_pos);
|
||||
println!("matched: {matched:?}\n---");
|
||||
debug!("matched: {matched:?}\n---");
|
||||
|
||||
// iterate over tokens and run try_match
|
||||
for token in possible_tokens {
|
||||
|
|
@ -421,7 +422,7 @@ fn next_token<'a>(
|
|||
};
|
||||
match token.try_match(input_to_match) {
|
||||
Some(result) => {
|
||||
//println!("matched token: {}", token);
|
||||
//debug!("matched token: {}", token);
|
||||
return Some((token.clone(), result, next_pos));
|
||||
}
|
||||
None => {} // continue matching until we exhaust all tokens
|
||||
|
|
@ -448,7 +449,7 @@ fn rank_possible_commands(
|
|||
.map(move |(display, scoring, is_alias)| {
|
||||
let similarity = strsim::jaro_winkler(&input, &scoring);
|
||||
// if similarity > 0.7 {
|
||||
// println!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
|
||||
// debug!("DEBUG: ranking: '{}' vs '{}' = {}", input, scoring, similarity);
|
||||
// }
|
||||
(cmd, display, similarity, is_alias)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use log::debug;
|
||||
use smol_str::{SmolStr, ToSmolStr};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
|
|
@ -79,8 +80,8 @@ pub(super) fn next_param<'a>(input: &'a str, current_pos: usize) -> Option<Match
|
|||
let leading_whitespace_count =
|
||||
input[..current_pos].len() - input[..current_pos].trim_start().len();
|
||||
let substr_to_match = &input[current_pos + leading_whitespace_count..];
|
||||
println!("stuff: {input} {current_pos} {leading_whitespace_count}");
|
||||
println!("to match: {substr_to_match}");
|
||||
debug!("stuff: {input} {current_pos} {leading_whitespace_count}");
|
||||
debug!("to match: {substr_to_match}");
|
||||
|
||||
if let Some(end_quote_pos) = find_quotes(substr_to_match) {
|
||||
// return quoted string, without quotes
|
||||
|
|
@ -132,20 +133,27 @@ pub(super) fn next_flag<'a>(input: &'a str, mut current_pos: usize) -> Option<Ma
|
|||
return None;
|
||||
}
|
||||
|
||||
println!("flag input {substr_to_match}");
|
||||
debug!("flag input {substr_to_match}");
|
||||
// strip the -
|
||||
let Some(substr_to_match) = substr_to_match.strip_prefix('-') else {
|
||||
let original_len = substr_to_match.len();
|
||||
let substr_without_dashes = substr_to_match.trim_start_matches('-');
|
||||
let dash_count = original_len - substr_without_dashes.len();
|
||||
|
||||
if dash_count == 0 || dash_count > 2 {
|
||||
// if it doesn't have one, then it is not a flag
|
||||
// or if it has more dashes than 2, assume its not a flag
|
||||
return None;
|
||||
};
|
||||
current_pos += 1;
|
||||
}
|
||||
|
||||
let substr_to_match = substr_without_dashes;
|
||||
current_pos += dash_count;
|
||||
|
||||
// try finding = or whitespace
|
||||
for (pos, char) in substr_to_match.char_indices() {
|
||||
println!("flag find char {char} at {pos}");
|
||||
debug!("flag find char {char} at {pos}");
|
||||
if char == '=' {
|
||||
let name = &substr_to_match[..pos];
|
||||
println!("flag find {name}");
|
||||
debug!("flag find {name}");
|
||||
// try to get the value
|
||||
let Some(param) = next_param(input, current_pos + pos + 1) else {
|
||||
return Some(MatchedFlag {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ lazy_static = { workspace = true }
|
|||
command_parser = { path = "../command_parser"}
|
||||
command_definitions = { path = "../command_definitions"}
|
||||
uniffi = { version = "0.29" }
|
||||
log = "0.4"
|
||||
simple_logger = "4.3.3"
|
||||
|
||||
[build-dependencies]
|
||||
uniffi = { version = "0.29", features = [ "build" ] }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
use std::{collections::HashMap, fmt::Write, sync::Arc};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt::Write,
|
||||
sync::{Arc, Once},
|
||||
};
|
||||
|
||||
use command_parser::{parameter::ParameterValue, token::TokenMatchResult, Tree};
|
||||
|
||||
|
|
@ -14,6 +18,8 @@ lazy_static::lazy_static! {
|
|||
};
|
||||
}
|
||||
|
||||
static LOG_INIT: Once = Once::new();
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum CommandResult {
|
||||
Ok { command: ParsedCommand },
|
||||
|
|
@ -121,6 +127,16 @@ pub struct ParsedCommand {
|
|||
}
|
||||
|
||||
pub fn parse_command(prefix: String, input: String) -> CommandResult {
|
||||
LOG_INIT.call_once(|| {
|
||||
if let Err(err) = simple_logger::SimpleLogger::new()
|
||||
.with_level(log::LevelFilter::Info)
|
||||
.env()
|
||||
.init()
|
||||
{
|
||||
eprintln!("cant initialize logger: {err}");
|
||||
}
|
||||
});
|
||||
|
||||
command_parser::parse_command(COMMAND_TREE.clone(), prefix, input).map_or_else(
|
||||
|error| CommandResult::Err { error },
|
||||
|parsed| CommandResult::Ok {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue