public static CommandLineItem Argument(CommandLinePart part)
 {
     return(new CommandLineItem(CommandLineItemType.Argument, null, null, part.Value));
 }
Esempio n. 2
0
        public CommandLine ParseString(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }

            var _outputBuffer       = new char[s.Length];
            var _outputBufferIndex  = 0;
            var _partWindows        = new List <Tuple <int, int, int, int> >();
            var _partStart          = default(int?);
            var _partBufferStart    = default(int?);
            var _quoteChar          = default(char?);
            var _escapeChar         = default(char?);
            var _escapedValueBuffer = new char[s.Length];

            for (var _i = 0; _i < s.Length; _i++)
            {
                var _c = s[_i];

                if (_partStart.HasValue)
                {
                    if (!_escapeChar.HasValue && _quoteChar.HasValue && _quoteChar == _c)
                    {
                        _quoteChar = null;
                    }
                    else
                    {
                        if (_escapeChar.HasValue)
                        {
                            if (!_quoteChar.HasValue && _IsSeparatorChar(_c))
                            {
                                _outputBuffer[_outputBufferIndex++] = _escapeChar.Value;
                                _partWindows.Add(Tuple.Create(_partStart.Value, _i, _partBufferStart.Value, _outputBufferIndex));
                                _partStart = null;
                            }
                            else if (_IsEscapeChar(_c) || _IsQuoteChar(_c))
                            {
                                _outputBuffer[_outputBufferIndex++] = _c;
                            }
                            else
                            {
                                _outputBuffer[_outputBufferIndex++] = _escapeChar.Value;
                                _outputBuffer[_outputBufferIndex++] = _c;
                            }

                            _escapeChar = null;
                        }
                        else if (_IsEscapeChar(_c))
                        {
                            _escapeChar = _c;
                        }
                        else if (!_quoteChar.HasValue && _IsQuoteChar(_c))
                        {
                            _quoteChar = _c;
                        }
                        else if (!_quoteChar.HasValue && _IsSeparatorChar(_c))
                        {
                            _partWindows.Add(Tuple.Create(_partStart.Value, _i, _partBufferStart.Value, _outputBufferIndex));
                            _partStart = null;
                        }
                        else
                        {
                            _outputBuffer[_outputBufferIndex++] = _c;
                        }
                    }
                }
                else
                {
                    if (!_IsSeparatorChar(_c))
                    {
                        _partStart       = _i;
                        _partBufferStart = _outputBufferIndex;

                        if (_IsQuoteChar(_c))
                        {
                            _quoteChar = _c;
                        }
                        else if (_IsEscapeChar(_c))
                        {
                            _escapeChar = _c;
                        }
                        else
                        {
                            _outputBuffer[_outputBufferIndex++] = _c;
                        }
                    }
                }
            }

            if (_escapeChar.HasValue)
            {
                _outputBuffer[_outputBufferIndex++] = _escapeChar.Value;
            }

            if (_partStart.HasValue)
            {
                _partWindows.Add(Tuple.Create(_partStart.Value, s.Length, _partBufferStart.Value, _outputBufferIndex));
            }

            var _parts     = new CommandLinePart[_partWindows.Count];
            var _partIndex = 0;

            foreach (var _partWindow in _partWindows)
            {
                var _start        = _partWindow.Item1;
                var _length       = _partWindow.Item2 - _start;
                var _bufferStart  = _partWindow.Item3;
                var _bufferLength = _partWindow.Item4 - _bufferStart;
                var _original     = s.Substring(_start, _length);
                var _value        = new string(_outputBuffer, _bufferStart, _bufferLength);

                var _escapedValue = _GetEscapedValue(_value, _escapedValueBuffer);

                _parts[_partIndex] = new CommandLinePart(
                    _partIndex++,
                    _start,
                    _original,
                    _value,
                    _escapedValue);
            }

            return(new CommandLine(
                       s,
                       string.Join(_DefaultSeparatorCharacter.ToString(), _parts.Select(_part => _part.EscapedValue)),
                       _parts));
        }
 public static CommandLineItem Flag(CommandLinePart part)
 {
     return(new CommandLineItem(CommandLineItemType.Flag, null, part.Value, null));
 }