From 40e7bb4b483022c18584800593b3a7031a0fa8bf Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 3 Oct 2022 04:04:40 +0700 Subject: [PATCH] Add new argument types for JSON and players This patch adds two more possible argument types for Acedia's commands: for arbitrary JSON values (argument types for any specific type of value were already availabler before) and for players type of value (allowing to specify a list of players with selectors, similar to how one does it for targeted commands). This patch does not actually implement parsing of these two arguments, simply adds a possibility to declare them. --- sources/Commands/Command.uc | 6 +- sources/Commands/CommandDataBuilder.uc | 116 +++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/sources/Commands/Command.uc b/sources/Commands/Command.uc index acbfe1c..83df47b 100644 --- a/sources/Commands/Command.uc +++ b/sources/Commands/Command.uc @@ -97,7 +97,11 @@ enum ParameterType // Parses into `HashTable` CPT_Object, // Parses into `ArrayList` - CPT_Array + CPT_Array, + // Parses into any JSON value + CPT_JSON, + // Parses into an array of specified players + CPT_PLAYERS }; /** diff --git a/sources/Commands/CommandDataBuilder.uc b/sources/Commands/CommandDataBuilder.uc index 66dbbe7..b2057c8 100644 --- a/sources/Commands/CommandDataBuilder.uc +++ b/sources/Commands/CommandDataBuilder.uc @@ -973,6 +973,122 @@ public final function CommandDataBuilder ParamArrayList( return self; } +/** + * Adds new JSON value parameter (required or optional depends on whether + * `RequireTarget()` call happened) to the currently selected + * sub-command / option. + * + * Only fails if provided `name` is `none`. + * + * @param name Name of the parameter, will be copied + * (as it would appear in the generated help info). + * @param variableName Name of the variable that will store this + * parameter's value in `HashTable` after user's command input + * is parsed. Provided value will be copied. + * If left `none`, - will coincide with `name` parameter. + * @return Returns the caller `CommandDataBuilder` to allow for + * method chaining. + */ +public final function CommandDataBuilder ParamJSON( + BaseText name, + optional BaseText variableName) +{ + if (name == none) { + return self; + } + PushParameter(NewParameter(name, CPT_JSON, false, variableName)); + return self; +} + +/** + * Adds new parameter for list of JSON values (required or optional depends on + * whether `RequireTarget()` call happened) to the currently selected + * sub-command / option. + * + * Only fails if provided `name` is `none`. + * + * @param name Name of the parameter, will be copied + * (as it would appear in the generated help info). + * @param variableName Name of the variable that will store this + * parameter's value in `HashTable` after user's command input + * is parsed. Provided value will be copied. + * If left `none`, - will coincide with `name` parameter. + * @return Returns the caller `CommandDataBuilder` to allow for + * method chaining. + */ +public final function CommandDataBuilder ParamJSONList( + BaseText name, + optional BaseText variableName) +{ + if (name == none) { + return self; + } + PushParameter(NewParameter(name, CPT_JSON, true, variableName)); + return self; +} + +/** + * Adds new players value parameter (required or optional depends on whether + * `RequireTarget()` call happened) to the currently selected + * sub-command / option. + * + * Players parameter is a parameter that allows one to specify a list of + * players through special selectors, the same way one does for + * targeted commands. + * + * Only fails if provided `name` is `none`. + * + * @param name Name of the parameter, will be copied + * (as it would appear in the generated help info). + * @param variableName Name of the variable that will store this + * parameter's value in `HashTable` after user's command input + * is parsed. Provided value will be copied. + * If left `none`, - will coincide with `name` parameter. + * @return Returns the caller `CommandDataBuilder` to allow for + * method chaining. + */ +public final function CommandDataBuilder ParamPlayers( + BaseText name, + optional BaseText variableName) +{ + if (name == none) { + return self; + } + PushParameter(NewParameter(name, CPT_PLAYERS, false, variableName)); + return self; +} + +/** + * Adds new parameter for list of players values (required or optional depends + * on whether `RequireTarget()` call happened) to the currently selected + * sub-command / option. + * + * Players parameter is a parameter that allows one to specify a list of + * players through special selectors, the same way one does for + * targeted commands. + * + * Only fails if provided `name` is `none`. + * + * @param name Name of the parameter, will be copied + * (as it would appear in the generated help info). + * @param variableName Name of the variable that will store this + * parameter's value in `HashTable` after user's command input + * is parsed. Provided value will be copied. + * If left `none`, - will coincide with `name` parameter. + * @return Returns the caller `CommandDataBuilder` to allow for + * method chaining. + */ +public final function CommandDataBuilder ParamPlayersList( + BaseText name, + optional BaseText variableName) +{ + if (name == none) { + return self; + } + PushParameter(NewParameter(name, CPT_PLAYERS, true, variableName)); + return self; +} + defaultproperties { errLongNameTooShort = (l=LOG_Error,m="Command `%1` is trying to register an option with a name that is way too short (<2 characters). Option will be discarded: %2")