Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 命令行(类似于csc的编译器)解析_C#_.net_Command Line - Fatal编程技术网

C# 命令行(类似于csc的编译器)解析

C# 命令行(类似于csc的编译器)解析,c#,.net,command-line,C#,.net,Command Line,我正在寻找解析此命令行的算法/库: var cmd = "foo /option:value /option2:valueof /etc:.. baa /t:4 etc xd"; 致: 我尝试了“纯模式”(C-like),只使用if和for。但是使用正则表达式或linq的解决方案非常受欢迎 我的代码(不工作): var cmd=“foo/option:value/option2:valueof/etc:…baa/t:4 etc xd”; var keys=newdictionary(); 对于

我正在寻找解析此命令行的算法/库:

var cmd = "foo /option:value /option2:valueof /etc:.. baa /t:4 etc xd";
致:

我尝试了“纯模式”(C-like),只使用if和for。但是使用正则表达式或linq的解决方案非常受欢迎

我的代码(不工作):

var cmd=“foo/option:value/option2:valueof/etc:…baa/t:4 etc xd”;
var keys=newdictionary();
对于(int pos=0,len=cmd.Length;pos
致:


当然有一些库可以处理命令行解析()

我将使用逐字遍历字符串

            var tokenCmd = cmd.Split(' ');
            string currentKey = "";

            foreach (var token in tokenCmd)
            {
                if ((char.IsLetterOrDigit(token[0])) &&
                    (!keys.ContainsKey(currentKey)) ||
                    (keys[currentKey].Any()))
                {
                    currentKey = token;
                    keys.Add(currentKey,
                             new Dictionary<string, string>());
                }
                else
                {
                    var splitToken = new[] { token, "" };

                    if (token.Contains(':'))
                    {
                        splitToken = token
                            .Replace("/", "")
                            .Split(':');
                    }

                    keys[currentKey].Add(splitToken[0],
                                         splitToken[1]);
                }
            }
var-tokenCmd=cmd.Split(“”);
字符串currentKey=“”;
foreach(tokenCmd中的var令牌)
{
if((字符IsleterOrdGit(标记[0]))&&
(!key.ContainsKey(currentKey))||
(键[currentKey].Any())
{
currentKey=token;
键。添加(当前键,
新字典());
}
其他的
{
var splitToken=new[]{token,“};
if(token.Contains(':'))
{
splitToken=token
.替换(“/”,“”)
.Split(“:”);
}
密钥[currentKey]。添加(拆分令牌[0],
splitToken[1]);
}
}

如果您可以保留分开的命令行(它们在您的输入变量中被标记在一起),并保留args所使用的string[]对象类型,那么我认为这就是您正在寻找的droid:

static void Main(string[] args)
{
    Hashtable parsedArgs = new Hashtable();
    args.ToList().ForEach(x => {
        int dpos = x.IndexOf(":");
        if (dpos > -1)
            parsedArgs[x.Substring(1, dpos - 1)] = x.Substring(dpos + 1);
        else
            parsedArgs[x.Substring(1)] = true;
    });
}

编辑:修改为Linq到哈希表中,如下所示。哈希表比我原始答案中看起来更优雅(代码更少)的KeyValuePair更容易使用。

选项值中可以有空格吗?如果是这样,你能用引号把它们括起来吗?参数是否可以在没有选项的情况下发生?选项是否可以在没有值的情况下出现?您需要在一行上解析三个命令,这有什么原因吗?回答这些问题将帮助我们回答您的问题。
} while (c != '/' && c != ' '); 
            var tokenCmd = cmd.Split(' ');
            string currentKey = "";

            foreach (var token in tokenCmd)
            {
                if ((char.IsLetterOrDigit(token[0])) &&
                    (!keys.ContainsKey(currentKey)) ||
                    (keys[currentKey].Any()))
                {
                    currentKey = token;
                    keys.Add(currentKey,
                             new Dictionary<string, string>());
                }
                else
                {
                    var splitToken = new[] { token, "" };

                    if (token.Contains(':'))
                    {
                        splitToken = token
                            .Replace("/", "")
                            .Split(':');
                    }

                    keys[currentKey].Add(splitToken[0],
                                         splitToken[1]);
                }
            }
static void Main(string[] args)
{
    Hashtable parsedArgs = new Hashtable();
    args.ToList().ForEach(x => {
        int dpos = x.IndexOf(":");
        if (dpos > -1)
            parsedArgs[x.Substring(1, dpos - 1)] = x.Substring(dpos + 1);
        else
            parsedArgs[x.Substring(1)] = true;
    });
}