public override void Execute() { base.Execute(); List <string> singleOptionList = SplitOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, SplitOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartSplit(); } Terminate(); }
private static void CheckOptions(SplitCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (!checkedOptions.IsSetFile || string.IsNullOrEmpty(checkedOptions.File)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a file.")); } if (!checkedOptions.IsSetPrefix || string.IsNullOrEmpty(checkedOptions.Prefix)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a prefix.")); } foreach (var c in EscapeChars) { if (checkedOptions.Prefix.Contains(c)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "bad prefix format.")); } } if (string.IsNullOrEmpty(checkedOptions.Directory)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a output folder.")); } if (checkedOptions.Bytes <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify the split bytes.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = SplitOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, SplitOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartSplit(); } Terminate(); }
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); }
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; }