一个简单的命令行解析工具类

作者:追风剑情 发布于:2018-12-1 18:39 分类:C#

示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = "cmd -quit -output  \"C:\\Program Files (x86)\"  -update:mode -2 -5 -save \"C:\\Program Files (x86)\"";
            CommandLine cmd = new CommandLine();
            cmd.Parse(line);
            cmd.Print();

            Console.WriteLine("是否存在 {0}: {1}", "-quit", cmd.ExistParam("-quit"));
            Console.WriteLine("是否存在 {0}: {1}", "-output", cmd.ExistParam("-output"));
            Console.WriteLine("参数值为 {0}: {1}", "-output", cmd.GetParamValue("-output"));
            Console.Read();
        }
    }
}

public class CommandLine
{
    private char[] _chars;
    private int _pos = 0;
    private string _whiteSpace = " \t\r\n";
    private List<string> _params = new List<string>();

    public CommandLine()
    {
    }

    public void Parse(string line)
    {
        this._chars = line.ToCharArray();

        _params.Clear();
        string p = NextParam();
        while (!string.IsNullOrEmpty(p)) {
            _params.Add(p);
            p = NextParam();
        }
    }

    public string[] GetParams()
    {
        return _params.ToArray();
    }

    //判断参数是否存在
    public bool ExistParam(string p)
    {
        return _params.Contains(p);
    }

    //获取参数值
    public string GetParamValue(string p)
    {
        for (int i=0; i< _params.Count; i++) {
            if (_params[i] == p) {
                if (i+1 < _params.Count) {
                    return _params[i+1];
                }
                break;
            }
        }
        return null;
    }

    private string NextParam()
    {
        EatWhiteSpace();
        
        int start_pos = _pos;
        char c = NextChar();
        //如果参数是用双引号括起来的
        bool is_double_quote = IsDoubleQuote(c);
        if (is_double_quote) {
            //找到关闭引号"所在位置
            while (!IsDoubleQuote(NextChar()));
        }else{
            while (!IsWhiteSpace(NextChar()));
        }

        int length = 0;
        if (is_double_quote){
            //去掉参数两边的双引号
            start_pos++;
        }
        length = _pos - start_pos - 1;
        if (length <= 0)
            return null;

        string s = new string(_chars, start_pos, length);
        //s = s.Trim();
        return s;
    }

    private char NextChar()
    {
        if (_pos >= _chars.Length)
            return '\n';
        char c = _chars[_pos];
        _pos++;
        return c;
    }

    private bool IsDoubleQuote(char c)
    {
        return c == '"';
    }

    private bool IsWhiteSpace(char c)
    {
        return _whiteSpace.IndexOf(c) != -1;
    }

    private void EatWhiteSpace()
    {
        if (_pos >= _chars.Length)
            return;
        while (IsWhiteSpace(_chars[_pos] )) {
            _pos++;
            if (_pos >= _chars.Length)
                break;
        }
    }

    public void Print()
    {
        for(int i=0; i< _params.Count; i++)
        {
            Console.WriteLine(_params[i]);
        }
    }
}

运行测试

1111.png

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号