alanlawrance / uconsole

In game console for Unity3D that allows for easy set up of commands with parameters.
MIT License
12 stars 2 forks source link

Argument parser woes with strings #2

Open Dregu opened 1 year ago

Dregu commented 1 year ago

The argument parser accepts quoted multi word strings as arguments, but replaces commas inside the string with spaces. Also empty strings don't work. I would like to use both of these things.

Mixing multiple delimiters is kinda dubious anyway, but I made this parser to handle inputs like

cmd "first, argument", second    3 ""

into four arguments first, argument, second, 3 and <empty string>:

public static object RunCommand(string commandWithArgs) {
...
        uConsoleHistory.Add(commandWithArgs);
        Regex parser = new Regex("["+new string(uConsoleInput.m_DelimterChars)+"](?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
        string[] tokens = parser.Split(commandWithArgs);
        tokens = tokens.Where(x => !string.IsNullOrEmpty(x)).ToArray();
        tokens = tokens.Select(x => x.StartsWith("\"") && x.EndsWith("\"") ? x.Substring(1, x.Length-2) : x).ToArray();
        BuildArgListFromTokens(tokens);
...

private static void BuildArgListFromTokens(string[] tokens) {
...
            //if (tokens[i][0] == '\"') {
            //fix to allow explicitly defined empty strings as arguments
            if (tokens[i].Length > 0 && tokens[i][0] == '\"') {

(Seeing as this is an open source project I thought I'd report some PB3 bugs here instead. I can make a PR if you want.)

Dregu commented 1 year ago

Actually I don't really like comma as a separator at all, it just feels weird in a console environment and it's messing with things like arbitrary number of arguments that should be treated as strings.