Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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# 枚举类型数组的命令行解析器_C#_Command Line_Command Line Arguments - Fatal编程技术网

C# 枚举类型数组的命令行解析器

C# 枚举类型数组的命令行解析器,c#,command-line,command-line-arguments,C#,Command Line,Command Line Arguments,我们正试图找到一个命令行解析器,它可以解析带有枚举的数组。 CommandlineParser支持使用int或string解析数组,但不支持枚举。 例如 很好。但不是下面的一个: public enum OptimizeFor { Unspecified, Speed, Accuracy } [OptionArray("o", "output", HelpText = "The ou

我们正试图找到一个命令行解析器,它可以解析带有枚举的数组。 CommandlineParser支持使用int或string解析数组,但不支持枚举。 例如

很好。但不是下面的一个:

 public enum OptimizeFor
        {
            Unspecified,
            Speed,
            Accuracy
        }
    [OptionArray("o", "output", HelpText = "The output files to generate.", DefaultValue = new[] { OptimizeFor.Accuracy, OptimizeFor.Speed })]
    public OptimizeFor[] OutputFiles { get; set; }

不确定这是否是你想要的,如果是的话,它会让你走上正确的轨道

public enum OptimizeFor
{
    Unspecified,
    Speed,
    Accuracy
}

public class EnumParser
{
    public static IEnumerable<TEnum> FindSelected<TEnum>(IEnumerable<string> enumItemNames)
    {
        var selectedOtions = Enum.GetNames(typeof(TEnum))
            .Intersect(enumItemNames, StringComparer.InvariantCultureIgnoreCase)
            .Select(i => Enum.Parse(typeof(TEnum), i))
            .Cast<TEnum>();
        return selectedOtions;
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Some fake arguments
        args = new[] {"-o", "SPEED", "accuracy", "SomethingElse"};

        var selectedEnumVals = EnumParser.FindSelected<OptimizeFor>(args);

        selectedEnumVals.Select(i => i.ToString()).ToList().ForEach(Console.WriteLine);
        Console.Read();
    }
}
的公共枚举优化
{
未明,
速度
精确
}
公共类枚举分析器
{
公共静态IEnumerable FindSelected(IEnumerable enumItemNames)
{
var selectedOptions=Enum.GetNames(typeof(TEnum))
.Intersect(enumItemNames、StringComparer.InvariantCultureInogoreCase)
.Select(i=>Enum.Parse(typeof(TEnum),i))
.Cast();
返回所选选项;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
//一些假论点
args=new[]{“-o”,“速度”,“精度”,“某物”};
var selectedEnumVals=EnumParser.FindSelected(args);
selectedEnumVals.Select(i=>i.ToString()).ToList().ForEach(Console.WriteLine);
Console.Read();
}
}

下面是一个用于解析枚举数组的命令行补丁。我已经创建了一个拉取请求

public bool SetValue(IList值、对象选项)
{
var elementType=_property.PropertyType.GetElementType();
var propertyType=elementType;
如果(propertyType.IsGenericType&&
propertyType.GetGenericTypeDefinition()==typeof(可为null))
{
propertyType=propertyType.GetGenericArguments()[0];
}
var array=array.CreateInstance(elementType,values.Count);
for(var i=0;i
你不需要问任何问题,只需要说出你的意图。您是否在询问如何将该功能添加到CommandLineParser?或者是否有其他库内置了此功能?或者如何构建自己的命令行解析器?Peter Hallam的库()支持enumsThanks Matt。我正在尝试使用现有的命令行解析器,因为我认为它应该随时可用。CommandLineParser确实支持枚举,但我还没有看到任何使用枚举的枚举数组的示例。实际上,在查看上面的注释后,我肯定会查看其中一个库。看起来现有的解析器不支持枚举集合。作为一种解决方法,我使用字符串数组&在代码中将它们转换为enum。
public enum OptimizeFor
{
    Unspecified,
    Speed,
    Accuracy
}

public class EnumParser
{
    public static IEnumerable<TEnum> FindSelected<TEnum>(IEnumerable<string> enumItemNames)
    {
        var selectedOtions = Enum.GetNames(typeof(TEnum))
            .Intersect(enumItemNames, StringComparer.InvariantCultureIgnoreCase)
            .Select(i => Enum.Parse(typeof(TEnum), i))
            .Cast<TEnum>();
        return selectedOtions;
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Some fake arguments
        args = new[] {"-o", "SPEED", "accuracy", "SomethingElse"};

        var selectedEnumVals = EnumParser.FindSelected<OptimizeFor>(args);

        selectedEnumVals.Select(i => i.ToString()).ToList().ForEach(Console.WriteLine);
        Console.Read();
    }
}
    public bool SetValue(IList<string> values, object options)
    {
        var elementType = _property.PropertyType.GetElementType();

        var propertyType = elementType;
        if (propertyType.IsGenericType &&
        propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
            propertyType = propertyType.GetGenericArguments()[0];
        }

        var array = Array.CreateInstance(elementType, values.Count);

        for (var i = 0; i < array.Length; i++)
        {
            try
            {
                if (propertyType.BaseType.Equals(typeof (System.Enum)))
                {
                    array.SetValue(Enum.Parse(propertyType, values[i]), i);
                    _property.SetValue(options, array, null);
                }
                else
                {
                    array.SetValue(Convert.ChangeType(values[i], elementType, _parsingCulture), i);
                    _property.SetValue(options, array, null);
                }
            }
            catch (FormatException)
            {
                return false;
            }
        }

        return ReceivedValue = true;
    }