我正在开发一个控制台工具,它接受一些参数,然后解析为Option类。
我需要的是一个属性,它将验证Option 类中的许多标记字段中是否只有一个具有值(已传递参数)。
前任。当我们运行时没问题:
我的.exe -a
我的.exe -b
但不是:
我的.exe
我的.exe -a -b
CommandLine.OptionAttribute 不能做这样的事情。我所做的是:主类 args[] 有扩展名 .Parse:
args.Parse(options)` //where options is my Options class
里面:
CommandLine.Parser.Default.ParseArguments(args, options);
var isOnlyOneOfMany = options.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(OneOfMany)) && prop.GetValue(options) != null).Count() == 1;
如何更好地做到这一点?