C# 在C中初始化函数指针和限制的字典#
我正在与一个小组合作制作一个命令行工具,该工具包含许多子命令,例如:C# 在C中初始化函数指针和限制的字典#,c#,dictionary,command-line,C#,Dictionary,Command Line,我正在与一个小组合作制作一个命令行工具,该工具包含许多子命令,例如: cmdtool.exe foo 1 2 3 或 目前,它是通过一个长而丑陋的列表来实现的 else if (command == "foo") { if (argLength < 3) { Console.Error.WriteLine("Error: not enough args for foo"); errorCode = ExitCode.Error; }
cmdtool.exe foo 1 2 3
或
目前,它是通过一个长而丑陋的列表来实现的
else if (command == "foo")
{
if (argLength < 3)
{
Console.Error.WriteLine("Error: not enough args for foo");
errorCode = ExitCode.Error;
}
else
{
// Do something useful
}
}
else if (command == "bar")
// repeat everything from foo
else if(命令==“foo”)
{
如果(argLength<3)
{
Console.Error.WriteLine(“错误:没有足够的参数用于foo”);
errorCode=ExitCode.Error;
}
其他的
{
//做些有用的事
}
}
else if(命令==“bar”)
//重复foo的所有内容
我的问题是有很多重复的代码,并且没有很好地概述所有的子命令我知道我将如何在python中实现它:
def commandFoo(args):
return DoFooStuff(args)
def commandBar(args):
return DoBarStuff(args)
REGULAR_COMMANDS = {
"foo": [1, 3, commandFoo],
"bar": [2, 2, commandBar]
}
def evaluateCommand(cmdString, args):
if cmdString not in REGULAR_COMMANDS:
print("Unknown command:", cmdString)
return False
lower = REGULAR_COMMANDS[cmdString][0]
upper = REGULAR_COMMANDS[cmdString][1]
if not lower <= len(args) <= upper:
print("Wrong number of args for:", cmdString)
return False
func = REGULAR_COMMANDS[cmdString][2]
return func(args)
def commandFoo(args):
返回DoFooStuff(args)
def命令栏(args):
返回DoBarStuff(args)
常规命令={
“foo”:[1,3,commandFoo],
“bar”:[2,2,commandBar]
}
def evaluateCommand(cmdString,args):
如果cmdString不在常规命令中:
打印(“未知命令:”,cmdString)
返回错误
lower=常规_命令[cmdString][0]
上限=常规命令[cmdString][1]
如果不低于我同意C#复杂对象/字典的初始化不像其他语言那样简单。但是,我认为您只需要一个调整即可使您的尝试生效:
private static Dictionary<string, List<object>> m_dCommandFuncs = new Dictionary<string, List<object>> {
{ "foo", new List<object>{1, 3, CommandFoo}},
{ "bar", new List<object>{2, 2, CommandBar}}
};
private static Dictionary m_dCommandFuncs=新字典{
{“foo”,新列表{1,3,CommandFoo},
{“bar”,新列表{2,2,CommandBar}
};
我同意C#复杂对象/字典初始化不像其他语言那样简单。但是,我认为您只需要一个调整即可使您的尝试生效:
private static Dictionary<string, List<object>> m_dCommandFuncs = new Dictionary<string, List<object>> {
{ "foo", new List<object>{1, 3, CommandFoo}},
{ "bar", new List<object>{2, 2, CommandBar}}
};
private static Dictionary m_dCommandFuncs=新字典{
{“foo”,新列表{1,3,CommandFoo},
{“bar”,新列表{2,2,CommandBar}
};
您可以定义一个委托int命令(字符串[]args)代码>
并按如下方式初始化字典:
private static Dictionary<string, List<object>> m_dCommandFuncs = new Dictionary<string, List<object>>
{
{ "foo", new List<object>{1, 3, (Command)CommandFoo}},
{ "bar", new List<object>{2, 2, (Command)CommandBar}}
};
class CommandConfig
{
public int Lower { get; set; }
public int Upper { get; set; }
public Command Command { get; set; }
}
private static Dictionary<string, CommandConfig> m_dCommandFuncs2 = new Dictionary<string, CommandConfig>
{
{"foo", new CommandConfig {Lower = 1, Upper = 3, Command = CommandFoo}},
{"bar", new CommandConfig {Lower = 2, Upper = 2, Command = CommandBar}}
};
命令仍然是委托类型。您可以定义一个委托int命令(字符串[]args)代码>
并按如下方式初始化字典:
private static Dictionary<string, List<object>> m_dCommandFuncs = new Dictionary<string, List<object>>
{
{ "foo", new List<object>{1, 3, (Command)CommandFoo}},
{ "bar", new List<object>{2, 2, (Command)CommandBar}}
};
class CommandConfig
{
public int Lower { get; set; }
public int Upper { get; set; }
public Command Command { get; set; }
}
private static Dictionary<string, CommandConfig> m_dCommandFuncs2 = new Dictionary<string, CommandConfig>
{
{"foo", new CommandConfig {Lower = 1, Upper = 3, Command = CommandFoo}},
{"bar", new CommandConfig {Lower = 2, Upper = 2, Command = CommandBar}}
};
命令
仍然是委托类型。我正在与一个组一起制作一个包含大量子命令的命令行工具“
我将使用参数解析器,如“我正在与一个组一起制作一个包含大量子命令的命令行工具”
我将使用参数解析器,如Thank,我要试一试。我想我以前已经试过了-但是让我再检查一下@mcbr提到的委托类型
,这会有用的。谢谢,谢谢,我试试看。我想我以前已经试过了-但是让我再检查一下@mcbr提到的委托类型
,这会有用的。谢谢