refactor(command_parser): simplify how tokens are defined in commands

This commit is contained in:
dusk 2025-01-24 04:08:59 +09:00
parent f804e7629f
commit 071db3d6d6
No known key found for this signature in database
9 changed files with 114 additions and 76 deletions

View file

@ -3,10 +3,11 @@ use command_parser::parameter;
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
let ap = tokens!(["config", "cfg"], ["autoproxy", "ap"]);
let cfg = ("config", ["cfg"]);
let ap = tokens!(cfg, ("autoproxy", ["ap"]));
let ap_account = concat_tokens!(ap, [["account", "ac"]]);
let ap_timeout = concat_tokens!(ap, [["timeout", "tm"]]);
let ap_account = tokens!(ap, ("account", ["ac"]));
let ap_timeout = tokens!(ap, ("timeout", ["tm"]));
[
command!(ap_account => "cfg_ap_account_show")
@ -14,9 +15,9 @@ pub fn cmds() -> impl Iterator<Item = Command> {
command!(ap_account, Toggle => "cfg_ap_account_update")
.help("Toggles autoproxy for the account"),
command!(ap_timeout => "cfg_ap_timeout_show").help("Shows the autoproxy timeout"),
command!(ap_timeout, parameter::RESET => "cfg_ap_timeout_reset")
command!(ap_timeout, ("reset", ["clear", "default"]) => "cfg_ap_timeout_reset")
.help("Resets the autoproxy timeout"),
command!(ap_timeout, parameter::DISABLE => "cfg_ap_timeout_off")
command!(ap_timeout, parameter::Toggle::Off => "cfg_ap_timeout_off")
.help("Disables the autoproxy timeout"),
command!(ap_timeout, ("timeout", OpaqueString) => "cfg_ap_timeout_update")
.help("Sets the autoproxy timeout"),

View file

@ -2,8 +2,8 @@ use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
[
command!(["thunder"] => "fun_thunder"),
command!(["meow"] => "fun_meow"),
command!("thunder" => "fun_thunder"),
command!("meow" => "fun_meow"),
]
.into_iter()
}

View file

@ -1,13 +1,13 @@
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
let help = ["help", "h"];
let help = ("help", ["h"]);
[
command!([help] => "help")
command!(help => "help")
.flag(("foo", OpaqueString)) // todo: just for testing
.help("Shows the help command"),
command!([help, "commands"] => "help_commands").help("help commands"),
command!([help, "proxy"] => "help_proxy").help("help proxy"),
command!(help, "commands" => "help_commands").help("help commands"),
command!(help, "proxy" => "help_proxy").help("help proxy"),
]
.into_iter()
}

View file

@ -18,9 +18,7 @@ pub mod server_config;
pub mod switch;
pub mod system;
use command_parser::{
command, command::Command, concat_tokens, parameter::ParameterKind::*, tokens,
};
use command_parser::{command, command::Command, parameter::ParameterKind::*, tokens};
pub fn all() -> impl Iterator<Item = Command> {
(help::cmds())
@ -29,3 +27,5 @@ pub fn all() -> impl Iterator<Item = Command> {
.chain(config::cmds())
.chain(fun::cmds())
}
pub const RESET: (&str, [&str; 2]) = ("reset", ["clear", "default"]);

View file

@ -1,17 +1,17 @@
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
let member = ["member", "m"];
let description = ["description", "desc"];
let privacy = ["privacy", "priv"];
let new = ["new", "n"];
let member = ("member", ["m"]);
let description = ("description", ["desc"]);
let privacy = ("privacy", ["priv"]);
let new = ("new", ["n"]);
let member_target = tokens!(member, MemberRef);
let member_desc = concat_tokens!(member_target, [description]);
let member_privacy = concat_tokens!(member_target, [privacy]);
let member_desc = tokens!(member_target, description);
let member_privacy = tokens!(member_target, privacy);
[
command!([member, new, ("name", OpaqueString)] => "member_new")
command!(member, new, ("name", OpaqueString) => "member_new")
.help("Creates a new system member"),
command!(member_target => "member_show")
.flag("pt")

View file

@ -1,13 +1,13 @@
use super::*;
pub fn cmds() -> impl Iterator<Item = Command> {
let system = ["system", "s"];
let new = ["new", "n"];
let system = ("system", ["s"]);
let new = ("new", ["n"]);
let system_new = tokens!(system, new);
[
command!([system] => "system_show").help("Shows information about your system"),
command!(system => "system_show").help("Shows information about your system"),
command!(system_new => "system_new").help("Creates a new system"),
command!(system_new, ("name", OpaqueString) => "system_new_name")
.help("Creates a new system (using the provided name)"),