static Cfg Args2Cfg(ArgsDictionary argsDictionary) { var cfg = new Cfg(); FieldInfo[] fields = (typeof(Cfg)).GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (var field in fields) { var paramAttribute = field.GetCustomAttribute(typeof (ParamAttribute), false) as ParamAttribute; if (null == paramAttribute) { continue; } bool isValueSet = false; foreach (var alias in paramAttribute.Aliases) { if (argsDictionary.ContainsKey(alias)) { if (typeof(String) == field.FieldType) { field.SetValue(cfg, argsDictionary[alias]); isValueSet = true; } else { var message = String.Format( "Invalid command line: {0}. Make sure that you have not duplicated any command-line arguments, " + "make sure that you have not used any arguments that are not valid.", Environment.CommandLine); throw new ApplicationException(message); } } } if (!isValueSet && paramAttribute.IsRequired) { var message = String.Format( "Parameter: '{0}' is required. Run with no arguments to displays help at the command prompt.", paramAttribute.Aliases[0]); throw new ApplicationException(message); } } return cfg; }
private static void Main(string[] args) { //args = new string[] //{ // "C:\\inetpub\\wwwroot\\Анкета.Редактор\\Анкета.Редактор.xml", // "-h", //}; if (0 == args.Length) { Console.WriteLine(StrUsage); return; } if ((from arg in args from hlpKey in HlpKeys where 0 == String.Compare(arg, hlpKey, StringComparison.OrdinalIgnoreCase) select arg).Any()) { Console.WriteLine(StrUsage); return; } try { var argsDictionary = new ArgsDictionary(args); var cfg = Args2Cfg(argsDictionary); foreach (var file in argsDictionary.Files) { if (!File.Exists(file)) { WriteLine(TraceEventType.Error, null, "{0}: was not found or the caller does not have the required permission.", file); continue; } try { byte[] hash; Dictionary<string, string> extendedProperties; if (!String.IsNullOrEmpty(cfg.Macro)) { // Macro. hash = ComputeHash(String.IsNullOrEmpty(cfg.HashAlgorithm) ? StrMd5 : cfg.HashAlgorithm, cfg.Macro, file); //extendedProperties = new Dictionary<string, string> //{ // { "HashAlgorithmName", String.IsNullOrEmpty(cfg.HashAlgorithm) ? StrMd5 : cfg.HashAlgorithm }, // { "Macro", cfg.Macro }, //}; // A string of hexadecimal pairs separated by hyphens, where each pair represents // the corresponding element in value; for example, "7F-2C-4A-00". WriteLine(TraceEventType.Information, null, "{0}: {1}", file, BitConverter.ToString(hash).Replace("-", String.Empty)); continue; } // hash = ComputeHash(String.IsNullOrEmpty(cfg.HashAlgorithm) ? StrMd5 : cfg.HashAlgorithm, file); //extendedProperties = new Dictionary<string, string> //{ // { "HashAlgorithmName", String.IsNullOrEmpty(cfg.HashAlgorithm) ? StrMd5 : cfg.HashAlgorithm }, //}; // A string of hexadecimal pairs separated by hyphens, where each pair represents // the corresponding element in value; for example, "7F-2C-4A-00". WriteLine(TraceEventType.Information, null, "{0}: {1}", file, BitConverter.ToString(hash).Replace("-", String.Empty)); } catch (Exception exception) { WriteLine(TraceEventType.Error, exception.Data, "{0}: {1}", file, exception.Message); } } } catch (Exception exception) { WriteLine(TraceEventType.Error, exception.Data, exception.Message); } }