Exemple #1
0
        public static SplitOptionType GetOptionType(string option)
        {
            SplitOptionType optionType = SplitOptionType.None;

            foreach (var pair in Options)
            {
                foreach (var item in pair.Value)
                {
                    if (item == option)
                    {
                        optionType = pair.Key;
                        break;
                    }
                }
            }

            return(optionType);
        }
        private static SplitCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            SplitCommandLineOptions targetOptions = new SplitCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    SplitOptionType optionType = SplitOptions.GetOptionType(arg);
                    if (optionType == SplitOptionType.None)
                    {
                        throw new CommandLineException(
                                  string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}",
                                                string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg)));
                    }

                    switch (optionType)
                    {
                    case SplitOptionType.File:
                        targetOptions.IsSetFile = true;
                        targetOptions.File      = commandLineOptions.Arguments[arg];
                        break;

                    case SplitOptionType.Prefix:
                        targetOptions.IsSetPrefix = true;
                        targetOptions.Prefix      = commandLineOptions.Arguments[arg];
                        break;

                    case SplitOptionType.SuffixLength:
                        int suffixLength = 0;
                        if (!int.TryParse(commandLineOptions.Arguments[arg], out suffixLength))
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid suffix length."));
                        }
                        if (suffixLength <= 0 || suffixLength > 10)
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid suffix length."));
                        }
                        targetOptions.SuffixLength = suffixLength;
                        break;

                    case SplitOptionType.Bytes:
                        targetOptions.Bytes = GetBytesSize(commandLineOptions.Arguments[arg]);
                        break;

                    case SplitOptionType.Directory:
                        targetOptions.Directory = commandLineOptions.Arguments[arg];
                        break;

                    case SplitOptionType.Timestamp:
                        targetOptions.IsSetTimestamp = true;
                        break;

                    case SplitOptionType.Overwrite:
                        targetOptions.IsSetOverwrite = true;
                        break;

                    case SplitOptionType.Help:
                        targetOptions.IsSetHelp = true;
                        break;

                    case SplitOptionType.Version:
                        targetOptions.IsSetVersion = true;
                        break;
                    }
                }
            }

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (!targetOptions.IsSetFile)
                {
                    targetOptions.IsSetFile = true;
                    targetOptions.File      = commandLineOptions.Parameters.First();
                }
            }

            return(targetOptions);
        }