Updated argument parsing for commands
When entering a command, autocompletion and /help
guide the user through the syntax of each command. However, argument verification is still necessary to avoid misbehavior of the commands and to avoid confusion of the user when another value was expected.
Currently, arguments are passed as a List<string>
to all methods which need a command's arguments (those include Execute
, GetExpectedArguments
, GetAutoCompleteEntries
and CheckArgumentsValid
). This involves quite a hassle, as the parameters are constantly passed around and need to be converted to the correct types each time. Furthermore, spaces mark the end of an argument, meaning there is no way to include them as an argument's value.
Improving the command system to fix this would involve:
- Making use of the instances of commands: have a property which stores the arguments of the command instance, and have an option to omit passing the arguments to each function call every time if the same command instance is referenced in multiple places.
- Create a base class for each command's argument, and allow sub-classing to easily allow each command to handle arguments as it wants, converting them to the types it needs (whether this class should be generic or not is up for debate). This would replace the current
CommandArgument
system, only used for/help
and similar. - Add a flag (on by default) to each argument to allow passing a string with spaces by using "quotes". Including a quote in such a string will involve escaping it using a backslash or similar.
- Include optional arguments with a default value in case it is not specified.
-
ArgumentError
s could be verified for each argument independently, for if it is sub-classed. Using lambdas for a more dynamic solution would be possible as well, though not preferred due to less clean code. - Allow marking a parameter (or a specific value of that parameter) as "not implemented" (see https://gitlab.com/redmser/MserDuck/issues/6 )
- Different base types of parameters:
-
CommandArgument
- has one value assigned to it, of any type. Example: X/Y coordinate of/spawn
-
StringCommandArgument
- has one string value assigned to it, automatically adding "quotes" to keep spaces. Example: Code provided for/execute
-
SelectCommandArgument
- has a certain list of values that are valid, of any type. Example: Items for/spawn
, colors for/color
-
ModeCommandArgument
- similar toSelectCommandArgument
, except that these are handled as a command's mode. A command's mode changes which arguments will follow afterwards. Commands can only have one mode assigned to them. Example:/role
-
ListCommandArgument
- has any amount of values assigned to it, of any type. Delimited by spaces, at the end of the command. Example: User list for/role set
, aliases for/chain