Esempio n. 1
0
 public static string[] ReadCommandLineArguments(this string text,
                                                 TextualArgumentOptions options = Default)
 {
     using (var reader = new StringReader(text))
     {
         var list = new List <string>();
         foreach (var arg in ReadCommandLineArguments(reader, options))
         {
             list.Add(arg);
         }
         return(list.ToArray());
     }
 }
Esempio n. 2
0
    public static IEnumerable <string> ReadCommandLineArguments(this TextReader reader,
                                                                TextualArgumentOptions options = Default)
    {
        string argument;

        for (;;)
        {
            argument = ReadCommandLineArgument(reader, options);
            if (argument == null)
            {
                yield break;
            }
            yield return(argument);
        }
    }
Esempio n. 3
0
    public static string ReadCommandLineArgument(this TextReader reader,
        TextualArgumentOptions options = Default)
    {
        if (!options.IsValid())
            throw new ArgumentOutOfRangeException(nameof(options));

        int cp;
        char c;
        for (;;)
        {
            cp = reader.Read();
            if (cp < 0) return null;

            c = (char)cp;
            if (!char.IsWhiteSpace(c))
                break;
        }

        char? quote = null;
        var useESLike = options.Has(EnableEcmaScriptLike);
        var fullyQuoted = (c == RegularQuoteChar || useESLike && c == ESLikeAdditionalQuoteChar);
        var sb = new StringBuilder();

        while (cp >= 0)
        {
            c = (char)cp;

            switch (c)
            {
                case ESLikeAdditionalQuoteChar:
                    if (useESLike)
                        goto case RegularQuoteChar;
                    goto default;
                case RegularQuoteChar:
                    if (c == quote)
                        quote = null;
                    else
                        quote = c;
                    break;
                case '\\':
                    var shouldEscape = false;
                    switch (options)
                    {
                        case MixCLikeEscape:
                        case MixEcmaScriptLikeEscape:
                            shouldEscape = true;
                            break;
                        case MixCLikeEscapeOnlyQuoted:
                        case MixEcmaScriptLikeEscapeOnlyQuoted:
                            shouldEscape = quote.HasValue;
                            break;
                        case MixCLikeEscapeOnlyFullyQuoted:
                        case MixEcmaScriptLikeEscapeOnlyFullyQuoted:
                            shouldEscape = fullyQuoted && quote.HasValue;
                            break;
                    }

                    if (shouldEscape)
                    {
                        sb.Append('\\');

                        cp = reader.Read();
                        if (cp < 0) continue;

                        c = (char)cp;
                    }
                    break;
                default:
                    if (!quote.HasValue && char.IsWhiteSpace(c))
                    {
                        cp = -1;
                        continue;
                    }
                    break;
            }
            sb.Append(c);

            cp = reader.Read();
        }
        return sb.ToString();
    }
Esempio n. 4
0
 public static string[] GetCommandLineArguments(
     TextualArgumentOptions options = Default)
     => Environment.CommandLine.ReadCommandLineArguments(options);
Esempio n. 5
0
 public static string[] ReadCommandLineArguments(this string text,
     TextualArgumentOptions options = Default)
 {
     using (var reader = new StringReader(text))
         return ReadCommandLineArguments(reader, options).ToArray();
 }
Esempio n. 6
0
 public static string[] ReadCommandLineArguments(this string text,
     TextualArgumentOptions options = Default)
 {
     using (var reader = new StringReader(text))
     {
         var list = new List<string>();
         foreach(var arg in ReadCommandLineArguments(reader, options))
             list.Add(arg);
         return list.ToArray();
     }
 }
Esempio n. 7
0
    public static string EvaluateCommandLineArgument(string argument,
        TextualArgumentOptions options = Default)
    {
        if (!options.IsValid())
            throw new ArgumentOutOfRangeException(nameof(options));

        char? last = null;
        char? quote = null;

        var useESLike = options.Has(EnableEcmaScriptLike);
        var fullyQuoted = (argument[0] == RegularQuoteChar || useESLike && argument[0] == ESLikeAdditionalQuoteChar);
        var sb = new StringBuilder();

        foreach (var c in argument)
        {
            if (last == '\\')
                last = null;
            else
            {
                switch (c)
                {
                    case ESLikeAdditionalQuoteChar:
                        if (useESLike)
                            goto case RegularQuoteChar;
                        goto default;
                    case RegularQuoteChar:
                        if (c == quote)
                            quote = null;
                        else
                            quote = c;

                        if (c == last)
                            last = null;
                        else
                        {
                            last = c;
                            continue;
                        }
                        break;
                    case '\\':
                        var shouldEscape = false;
                        switch (options)
                        {
                            case MixCLikeEscape:
                            case MixEcmaScriptLikeEscape:
                                shouldEscape = true;
                                break;
                            case MixCLikeEscapeOnlyQuoted:
                            case MixEcmaScriptLikeEscapeOnlyQuoted:
                                shouldEscape = quote.HasValue;
                                break;
                            case MixCLikeEscapeOnlyFullyQuoted:
                            case MixEcmaScriptLikeEscapeOnlyFullyQuoted:
                                shouldEscape = fullyQuoted && quote.HasValue;
                                break;
                        }

                        if (shouldEscape)
                        {
                            last = '\\';
                            continue;
                        }

                        break;
                    default:
                        last = null;
                        break;
                }
            }
            sb.Append(c);
        }
        return sb.ToString();
    }
Esempio n. 8
0
 public static IEnumerable<string> ReadCommandLineArguments(this TextReader reader,
     TextualArgumentOptions options = Default)
 {
     string argument;
     for (;;)
     {
         argument = ReadCommandLineArgument(reader, options);
         if (argument == null) yield break;
         yield return argument;
     }
 }
Esempio n. 9
0
 public static bool IsValid(this TextualArgumentOptions value)
 => TextualArgumentOptions.MixEcmaScriptLikeEscapeOnlyFullyQuoted.Has(value);
Esempio n. 10
0
 public void ForEval(TextualArgumentOptions options, string expected, string input)
 {
     Assert.Equal(expected, TextualArgumentUtility.EvaluateCommandLineArgument(input, options));
 }
Esempio n. 11
0
 public static string[] ReadCommandLineArguments(this string text,
                                                 TextualArgumentOptions options = Default)
 {
     using (var reader = new StringReader(text))
         return(ReadCommandLineArguments(reader, options).ToArray());
 }
Esempio n. 12
0
    public static string EvaluateCommandLineArgument(string argument,
                                                     TextualArgumentOptions options = Default)
    {
        if (!options.IsValid())
        {
            throw new ArgumentOutOfRangeException(nameof(options));
        }

        char?last  = null;
        char?quote = null;

        var useESLike   = options.Has(EnableEcmaScriptLike);
        var fullyQuoted = (argument[0] == RegularQuoteChar || useESLike && argument[0] == ESLikeAdditionalQuoteChar);
        var sb          = new StringBuilder();

        foreach (var c in argument)
        {
            if (last == '\\')
            {
                last = null;
            }
            else
            {
                switch (c)
                {
                case ESLikeAdditionalQuoteChar:
                    if (useESLike)
                    {
                        goto case RegularQuoteChar;
                    }
                    goto default;

                case RegularQuoteChar:
                    if (c == quote)
                    {
                        quote = null;
                    }
                    else
                    {
                        quote = c;
                    }

                    if (c == last)
                    {
                        last = null;
                    }
                    else
                    {
                        last = c;
                        continue;
                    }
                    break;

                case '\\':
                    var shouldEscape = false;
                    switch (options)
                    {
                    case MixCLikeEscape:
                    case MixEcmaScriptLikeEscape:
                        shouldEscape = true;
                        break;

                    case MixCLikeEscapeOnlyQuoted:
                    case MixEcmaScriptLikeEscapeOnlyQuoted:
                        shouldEscape = quote.HasValue;
                        break;

                    case MixCLikeEscapeOnlyFullyQuoted:
                    case MixEcmaScriptLikeEscapeOnlyFullyQuoted:
                        shouldEscape = fullyQuoted && quote.HasValue;
                        break;
                    }

                    if (shouldEscape)
                    {
                        last = '\\';
                        continue;
                    }

                    break;

                default:
                    last = null;
                    break;
                }
            }
            sb.Append(c);
        }
        return(sb.ToString());
    }
Esempio n. 13
0
    public static string ReadCommandLineArgument(this TextReader reader,
                                                 TextualArgumentOptions options = Default)
    {
        if (!options.IsValid())
        {
            throw new ArgumentOutOfRangeException(nameof(options));
        }

        int  cp;
        char c;

        for (;;)
        {
            cp = reader.Read();
            if (cp < 0)
            {
                return(null);
            }

            c = (char)cp;
            if (!char.IsWhiteSpace(c))
            {
                break;
            }
        }

        char?quote       = null;
        var  useESLike   = options.Has(EnableEcmaScriptLike);
        var  fullyQuoted = (c == RegularQuoteChar || useESLike && c == ESLikeAdditionalQuoteChar);
        var  sb          = new StringBuilder();

        while (cp >= 0)
        {
            c = (char)cp;

            switch (c)
            {
            case ESLikeAdditionalQuoteChar:
                if (useESLike)
                {
                    goto case RegularQuoteChar;
                }
                goto default;

            case RegularQuoteChar:
                if (c == quote)
                {
                    quote = null;
                }
                else
                {
                    quote = c;
                }
                break;

            case '\\':
                var shouldEscape = false;
                switch (options)
                {
                case MixCLikeEscape:
                case MixEcmaScriptLikeEscape:
                    shouldEscape = true;
                    break;

                case MixCLikeEscapeOnlyQuoted:
                case MixEcmaScriptLikeEscapeOnlyQuoted:
                    shouldEscape = quote.HasValue;
                    break;

                case MixCLikeEscapeOnlyFullyQuoted:
                case MixEcmaScriptLikeEscapeOnlyFullyQuoted:
                    shouldEscape = fullyQuoted && quote.HasValue;
                    break;
                }

                if (shouldEscape)
                {
                    sb.Append('\\');

                    cp = reader.Read();
                    if (cp < 0)
                    {
                        continue;
                    }

                    c = (char)cp;
                }
                break;

            default:
                if (!quote.HasValue && char.IsWhiteSpace(c))
                {
                    cp = -1;
                    continue;
                }
                break;
            }
            sb.Append(c);

            cp = reader.Read();
        }
        return(sb.ToString());
    }
Esempio n. 14
0
 public static string[] GetCommandLineArguments(
     TextualArgumentOptions options = Default)
 => Environment.CommandLine.ReadCommandLineArguments(options);