예제 #1
0
        public DateTimePartialParameter(string str)
        {
            var splitOnDash = str.Split('-');

            int year;

            int.TryParse(splitOnDash[0], out year);
            if (year < 1000) // this is not 4 digits, its only value eg  4 digits 0982 is 4 digits.
            {
                _e = new ArgumentException(
                    $"Require Year parameter be a 4 Digit Year <YYYY> as part of format '{_format}'");
                return;
            }
            _year = year;

            if (splitOnDash.Length > 1)         // may have month specified
            {
                if (splitOnDash[1].Length == 0) // just a '-' is allowed with no value. just set year.
                {
                    return;
                }
                int month;
                if (!int.TryParse(splitOnDash[1], out month))
                {
                    //month = Convert.ToDateTime("01-" + p[1] + "-2000").Month;
                    DateTime tmp;
                    if (DateTime.TryParse("01-" + splitOnDash[1] + "-" + _year, out tmp))
                    {
                        month = tmp.Month;
                    }
                }
                if (month == 0 || month > 12)
                {
                    _e = new ArgumentException(
                        $"Require valid integer 1-12 or Month name for Month as part of format '{_format}'");
                    return;
                }
                _month = month;
            }


            if (splitOnDash.Length > 2)         // may have dayOfMonth specified
            {
                if (splitOnDash[2].Length == 0) // just a '-' is allowed with no value. just set year and month
                {
                    return;
                }

                // If 'T' is not used as seperator between Date and Time then error
                if (SeperatorIsNotValid(splitOnDash[2], 'T'))
                {
                    _e = new ArgumentException(
                        $"The separator between Date and Time must be 'T' as part of format '{_format}'");
                    return;
                }

                var splitOnT = splitOnDash[2].Split('T');

                int dayOfMonth = 0;
                int unValidatedDayOfMonth;
                if (int.TryParse(splitOnT[0], out unValidatedDayOfMonth))
                {
                    // check for valid day of month
                    DateTime tmpDateTime;
                    if (DateTime.TryParse(_year + "-" + _month + "-" + unValidatedDayOfMonth, out tmpDateTime))
                    {
                        dayOfMonth = tmpDateTime.Day;
                    }
                }

                if (dayOfMonth == 0 || dayOfMonth > 31)
                {
                    _e = new ArgumentException(
                        $"Require valid Day of Month integer range 1-31 for Day <DD> as part of format '{_format}'");
                    return;
                }
                _dayOfMonth = dayOfMonth;

                if (splitOnT.Length > 1 && splitOnT[1].Length > 0)
                {
                    var t = new TimePartialParameter(splitOnT[1], _format);
                    _hour   = t.Hour;
                    _minute = t.Minute;
                    _second = t.Second;
                }
            }
        }
예제 #2
0
파일: CDEArgs.cs 프로젝트: CDEApp/CDE
        public CDEArgs()
        {
            // Every Option except "<>" needs to set _currentlist to its own list, or null.
            // This improves the handling of non matching parameters.
            _os = new OptionSet
            {
                //{
                //    "config=", "Load parameters from {File}(s)", o => {
                //        // import the config file.
                //        // # start of line is comment ignore.
                //        // new lines are considered same as white space for parameter identification.
                //        // extra white space is ignored at start or end of lines.
                //    }
                //},

                // Modes below here in this section
                {
                    "scan=", "Mode: scans one or more {Path}(s) creating catalogs", o => {
                        Mode         = Modes.Scan;
                        _currentList = _scanParameters;
                        _scanParameters.Add(o);
                    }
                },
                {
                    "find=", "Mode: find entries matching {String}(s)", o => {
                        Mode         = Modes.Find;
                        _currentList = _findParameters;
                        _findParameters.Add(o);
                    }
                },
                {
                    "hash", "Mode: collect minimal set of hashes for dupes", o => {
                        Mode         = Modes.Hash;
                        _currentList = null;
                    }
                },
                {
                    "dupes", "Mode: find duplicate files, depends on hashes", o => {
                        Mode         = Modes.Dupes;
                        _currentList = null;
                    }
                },
                {
                    "dump", "Mode: output to console catalog entries", o => {
                        Mode         = Modes.Dump;
                        _currentList = null;
                    }
                },
                {
                    "loadWait", "Mode: load catalogs and wait till enter pressed", o => {
                        Mode         = Modes.LoadWait;
                        _currentList = null;
                    }
                },
                {
                    "h|help", "Mode: show this message and exit", o => {
                        Mode         = Modes.Help;
                        _currentList = null;
                    }
                },
                {
                    "v|version", "Mode: show version", o => {
                        Mode         = Modes.Version;
                        _currentList = null;
                    }
                },

                // Options below here in this section
                {
                    "bp|basePath=", "Set one or more base {Path}(s)", o => {
                        if (!AllowStartPath.Contains(_mode))
                        {
                            throw new OptionException("The -basePath option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _currentList = _basePaths;
                        _currentList.Add(o);
                    }
                },
                {
                    "grep", "Enable regular expressions for String find.", o => {
                        if (!AllowGrep.Contains(_mode))
                        {
                            throw new OptionException("The -grep option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _grepEnabled = o != null;
                        _currentList = null;
                    }
                },
                {
                    "repl", "Enable prompting for more find searches.", o => {
                        if (!AllowRepl.Contains(_mode))
                        {
                            throw new OptionException("The -repl option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _replEnabled = o != null;
                        _currentList = null;
                    }
                },
                {
                    "path", "Include paths when searching in find.", o => {
                        if (!AllowPath.Contains(_mode))
                        {
                            throw new OptionException("The -path option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _pathEnabled = o != null;
                        _currentList = null;
                    }
                },
                {
                    "hashAll", "In hash mode hash all files", o => {
                        if (!AllowHashAll.Contains(_mode))
                        {
                            throw new OptionException("The -hashAll option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _hashAllEnabled = o != null;
                        _currentList    = null;
                    }
                },
                {
                    "e|exclude=", "regex {String}(s) to exclude from processing", o => {
                        if (!AllowExclude.Contains(_mode))
                        {
                            throw new OptionException("The -exclude option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _currentList = _exclude;
                        _currentList.Add(o);
                    }
                },
                {
                    "i|include=", "regex {String}(s) to include it in processing", o => {
                        if (!AllowInclude.Contains(_mode))
                        {
                            throw new OptionException("The -include option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _currentList = _include;
                        _currentList.Add(o);
                    }
                },
                {
                    "minSize=", "Minimum file size to include it in processing", o => {
                        if (!AllowMinSize.Contains(_mode))
                        {
                            throw new OptionException("The -minSize option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _minSize     = SizeOption(o);
                        _currentList = null;
                    }
                },
                {
                    "maxSize=", "Maximum file size to include it in processing", o => {
                        if (!AllowMaxSize.Contains(_mode))
                        {
                            throw new OptionException("The -maxSize option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        _maxSize     = SizeOption(o);
                        _currentList = null;
                    }
                },
                {
                    "minDate=", "Minimum DateTime on entry to include it in processing", o => {
                        if (!AllowMinDate.Contains(_mode))
                        {
                            throw new OptionException("The -minDate option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        try
                        {
                            var d = new DateTimePartialParameter(o);
                            _minDate = d.GetDate();
                        }
                        catch (ArgumentException ae)
                        {
                            throw new OptionException(ae.Message, _mode.ToString());
                        }

                        _currentList = null;
                    }
                },
                {
                    "maxDate=", "Maximum DateTime on entry to include it in processing", o => {
                        if (!AllowMaxDate.Contains(_mode))
                        {
                            throw new OptionException("The -maxDate option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }

                        try
                        {
                            var d = new DateTimePartialParameter(o);
                            _maxDate = d.GetDate();
                        }
                        catch (ArgumentException ae)
                        {
                            throw new OptionException(ae.Message, _mode.ToString());
                        }
                        _currentList = null;
                    }
                },
                {
                    "minTime=", "Minimum Time on entry ignore Date to include it in processing", o => {
                        if (!AllowMinTime.Contains(_mode))
                        {
                            throw new OptionException("The -minTime option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }
                        try
                        {
                            var d = new TimePartialParameter(o);
                            _minTime = new DateTime(1, 1, 1, d.Hour, d.Minute, d.Second);
                        }
                        catch (ArgumentException ae)
                        {
                            throw new OptionException(ae.Message, _mode.ToString());
                        }
                        _currentList = null;
                    }
                }, {                   // unsure if leaving this here for releases
                    "alternate", "an alternate data model (testing)", o => {
                        if (!AllowAlternate.Contains(_mode))
                        {
                            throw new OptionException("The -alt option is not supported in mode '-" + _mode.ToString().ToLower() + "'.", o);
                        }
                        _alternate   = o != null;
                        _currentList = null;
                    }
                },
                // to collection multi value parameter values.
                {
                    "<>", "", o => {
                        if (_currentList == null)
                        {
                            throw new OptionException("Error unmatched parameter: '" + o + "'", o);
                        }
                        _currentList.Add(o);
                    }
                },
            };
        }