Пример #1
0
        static void Main(string[] args)
        {
            // Check if we need to print help by checking for -h or --help at the first or second index
            // Also, when fewer arguments are available, print the help
            int shortIndex = Array.IndexOf(args, "-h");
            int longIndex  = Array.IndexOf(args, "--help");

            if (args.Length < 2 || shortIndex == 0 || shortIndex == 1 || longIndex == 0 || longIndex == 1)
            {
                PrintHelp();
                return;
            }

            int  commandIndex = 1;
            byte runOnDays    = 0;

            if (args[1] == "--days")
            {
                commandIndex += 2;
                string[] splitDays = args[2].Split(",");

                for (int i = 0; i < splitDays.Length; i++)
                {
                    if (splitDays[i].Length == 1)
                    {
                        // Attempt to parse it as an integer
                        int dayIndex;
                        if (int.TryParse(splitDays[i], out dayIndex))
                        {
                            Day?foundIntDay = DayUtils.FindDay(dayIndex);
                            if (foundIntDay.HasValue)
                            {
                                runOnDays |= (byte)foundIntDay.Value;
                                continue;
                            }
                        }
                    }
                    // Attempt to find the day as valid string
                    Day?foundStrDay = DayUtils.FindDay(splitDays[i]);
                    if (!foundStrDay.HasValue)
                    {
                        Console.Error.WriteLine($"Invalid day: {splitDays[i]}");
                        Environment.Exit(1);
                    }
                    runOnDays |= (byte)foundStrDay.Value;
                }
            }

            string commandFileName  = args[commandIndex++];
            string commandArguments = CommandAction.BuildArguments(args, commandIndex);

            IntervalRange[] intervals = IntervalRange.GetIntervals(args[0], Console.Error);
            if (intervals == null)
            {
                Environment.Exit(1);
            }

            IntervalScheduler intervalScheduler = new IntervalScheduler(
                new CommandAction(commandFileName, commandArguments), intervals);

            if (runOnDays != 0)
            {
                intervalScheduler.SetDayFlags(runOnDays);
            }

            intervalScheduler.RunScheduler(CancellationToken.None);
        }