当前位置: 首页>>代码示例>>C#>>正文


C# CommandLineParser.GetSyntax方法代码示例

本文整理汇总了C#中CommandLineParser.GetSyntax方法的典型用法代码示例。如果您正苦于以下问题:C# CommandLineParser.GetSyntax方法的具体用法?C# CommandLineParser.GetSyntax怎么用?C# CommandLineParser.GetSyntax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CommandLineParser的用法示例。


在下文中一共展示了CommandLineParser.GetSyntax方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            var commandLineParser = new CommandLineParser();
            Arguments arguments;
            if (!commandLineParser.TryParse<Arguments>(args, out arguments))
                Abort(commandLineParser.GetSyntax<Arguments>());

            if (!File.Exists(arguments.InputFile))
                Abort("Input file does not exist");

            if (arguments.PreviousYearFiles.Any(file => !File.Exists(file)))
                Abort("Previous year import file does not exist.");

            IList<Person> people;
            var csvParser = new CsvParser();
            if (!csvParser.TryParse<Person>(arguments.InputFile, out people))
                Abort("Input file not in correct format.");

            foreach(var file in arguments.PreviousYearFiles)
            {
                if (!TryParsePreviousYearAssignments(File.ReadAllLines(file), people))
                    Abort("Previous year import failed.");
            }

            if (!string.IsNullOrEmpty(arguments.SmtpServer)
                && !File.Exists(arguments.EmailTemplateFile))
                Abort("You need to specify an Email template");

            var stopwatch = Stopwatch.StartNew();
            IList<Person> shuffled;
            while (true)
            {
                shuffled = people.Shuffle();
                Console.Write(".");
                if (IsValid(people, shuffled))
                    break;
                if (stopwatch.Elapsed > TimeSpan.FromSeconds(arguments.CalculationTime))
                    Abort("Cound not find a solution in the allotted time. Try increasing CalculationTime");
                Console.Write(".");
            }

            var assignments = new Dictionary<Person, Person>();
            for (int i = 0; i < people.Count; i++)
                assignments.Add(people[i], shuffled[i]);

            Console.WriteLine("\nAssignment completed.");

            int current = 0;
            if (!string.IsNullOrEmpty(arguments.SmtpServer))
            {
                // Read email template
                var templateText = File.ReadAllText(arguments.EmailTemplateFile);
                var subject = templateText.Substring(0, templateText.IndexOf(Environment.NewLine));
                var body = templateText.Substring(templateText.IndexOf(Environment.NewLine));

                foreach (var assignment in assignments)
                {
                    MailMessage message = new MailMessage(new MailAddress("[email protected]"), new MailAddress(assignment.Key.Email))
                    {
                        Subject = string.Format(subject, assignment.Key.Name),
                        Body = string.Format(body, assignment.Key.Name, assignment.Value.FullName),
                    };

                    var credentials = new NetworkCredential(arguments.SmtpUsername, arguments.SmtpPassword);
                    var client = new SmtpClient(arguments.SmtpServer)
                    {
                        Credentials = credentials,
                        EnableSsl = true,
                    };

                    try
                    {
                        client.Send(message);
                    }
                    catch (SmtpException ex)
                    {
                        string error = ex.Message;
                        if (ex.InnerException != null)
                            error += "\n" + ex.InnerException.Message;
                        Abort(error);
                    }

                    Console.Write("\rSent Email {0} of {1}.", ++current, assignments.Count);
                }
                Console.WriteLine();
            }

            if (!string.IsNullOrEmpty(arguments.OutputFile))
            {
                using (var writer = new StreamWriter(arguments.OutputFile))
                    foreach (var assignment in assignments)
                    {
                        writer.WriteLine("{0} ({1}) -> {2} ({3})", assignment.Key.FullName, assignment.Key.Email, assignment.Value.FullName, assignment.Value.Email);
                    }
                Console.WriteLine("Wrote {0} assignments to output file.", assignments.Count);
            }

            if (string.IsNullOrEmpty(arguments.OutputFile) && string.IsNullOrEmpty(arguments.SmtpServer))
                for (int i = 0; i < people.Count; i++)
                    Console.WriteLine("{0} -> {1}", people[i].FullName, shuffled[i].FullName);
//.........这里部分代码省略.........
开发者ID:ngollon,项目名称:wichtler,代码行数:101,代码来源:Program.cs


注:本文中的CommandLineParser.GetSyntax方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。