private static RemoveCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); RemoveCommandLineOptions targetOptions = new RemoveCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { RemoveOptionType optionType = RemoveOptions.GetOptionType(arg); if (optionType == RemoveOptionType.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 RemoveOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case RemoveOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case RemoveOptionType.Help: targetOptions.IsSetHelp = true; break; case RemoveOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { foreach (var item in commandLineOptions.Parameters) { targetOptions.Files.Add(item); } } return targetOptions; }
private static MoveDateCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); MoveDateCommandLineOptions targetOptions = new MoveDateCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { MoveDateOptionType optionType = MoveDateOptions.GetOptionType(arg); if (optionType == MoveDateOptionType.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 MoveDateOptionType.RegexPattern: targetOptions.RegexPattern = commandLineOptions.Arguments[arg]; break; case MoveDateOptionType.InputDirectory: targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case MoveDateOptionType.Help: targetOptions.IsSetHelp = true; break; case MoveDateOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }
/// <summary> /// Parses the passed command line arguments and returns the result /// in a CommandLineOptions object. /// </summary> /// <param name="args">Array of command line arguments</param> /// <param name="list">Array of command line arguments</param> /// <returns>Object containing the parsed command line</returns> public static CommandLineOptions Parse(string[] args, params string[] singleOptionList) { if (args == null) throw new ArgumentNullException("args"); CommandLineOptions cmdOptions = new CommandLineOptions(); int index = 0; if (index < args.Length) { string token = NextToken(args, ref index); while (!string.IsNullOrEmpty(token)) { if (IsArgument(token)) { string arg = token.TrimStart(OptionStartWithChars).TrimEnd(OptionEqualChar); string value = string.Empty; if (arg.Contains(OptionEqualChar)) { // arg was specified with an '=' sign, so we need // to split the string into the arg and value, but only // if there is no space between the '=' and the arg and value. string[] r = arg.Split(new char[] { OptionEqualChar }, 2); if (r.Length == 2) { arg = r[0]; value = r[1]; } } // single option do not need a following parameter bool isSingleOption = false; if (singleOptionList != null) { for (int i = 0; i < singleOptionList.Length; i++) { if (arg == singleOptionList[i]) { isSingleOption = true; break; } } } // find following parameter while (!isSingleOption && string.IsNullOrEmpty(value)) { index++; if (index < args.Length) { string next = NextToken(args, ref index); if (!string.IsNullOrEmpty(next)) { if (IsArgument(next)) { // push the token back onto the stack so // it gets picked up on next pass as an arg index--; value = MagicOptionValue; break; } else if (next != OptionEqualChar.ToString()) { // save the value (trimming any '=' from the start) value = next.TrimStart(OptionEqualChar); } } } else { index--; value = MagicOptionValue; break; } } // save the pair if (cmdOptions.Arguments.ContainsKey(arg)) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "option with the same argument.")); if (value == MagicOptionValue) { cmdOptions.Arguments.Add(arg, string.Empty); } else { cmdOptions.Arguments.Add(arg, value.TrimStart('\'').TrimEnd('\'')); } } else { // save stand-alone parameter if (cmdOptions.Parameters.Contains(token)) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "option with the same argument.")); cmdOptions.Parameters.Add(token.TrimStart('\'').TrimEnd('\'')); } index++; if (index < args.Length) { token = NextToken(args, ref index); } else { break; } } } return cmdOptions; }
private static SortCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); SortCommandLineOptions targetOptions = new SortCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { SortOptionType optionType = SortOptions.GetOptionType(arg); if (optionType == SortOptionType.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 SortOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case SortOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case SortOptionType.ReverseOrder: targetOptions.IsSetReverseOrder = true; break; case SortOptionType.Help: targetOptions.IsSetHelp = true; break; case SortOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = commandLineOptions.Parameters.First(); } } return targetOptions; }
private static EnvironmentCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); EnvironmentCommandLineOptions targetOptions = new EnvironmentCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { EnvironmentOptionType optionType = EnvironmentOptions.GetOptionType(arg); if (optionType == EnvironmentOptionType.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 EnvironmentOptionType.Help: targetOptions.IsSetHelp = true; break; case EnvironmentOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }
private static HeadCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); HeadCommandLineOptions targetOptions = new HeadCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { HeadOptionType optionType = HeadOptions.GetOptionType(arg); if (optionType == HeadOptionType.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 HeadOptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case HeadOptionType.Number: long outputLines = 0; if (!long.TryParse(commandLineOptions.Arguments[arg], out outputLines)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid output lines number.")); } if (outputLines <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid output lines number.")); } targetOptions.Number = outputLines; break; case HeadOptionType.Help: targetOptions.IsSetHelp = true; break; case HeadOptionType.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 GrepCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); GrepCommandLineOptions targetOptions = new GrepCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { GrepOptionType optionType = GrepOptions.GetOptionType(arg); if (optionType == GrepOptionType.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 GrepOptionType.RegexPattern: targetOptions.IsSetRegexPattern = true; targetOptions.RegexPattern = commandLineOptions.Arguments[arg]; break; case GrepOptionType.File: targetOptions.IsSetPath = true; targetOptions.FilePaths.Add(commandLineOptions.Arguments[arg]); break; case GrepOptionType.FixedStrings: targetOptions.IsSetFixedStrings = true; break; case GrepOptionType.IgnoreCase: targetOptions.IsSetIgnoreCase = true; break; case GrepOptionType.InvertMatch: targetOptions.IsSetInvertMatch = true; break; case GrepOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case GrepOptionType.Count: targetOptions.IsSetCount = true; break; case GrepOptionType.FilesWithoutMatch: targetOptions.IsSetFilesWithoutMatch = true; break; case GrepOptionType.FilesWithMatchs: targetOptions.IsSetFilesWithMatchs = true; break; case GrepOptionType.NoMessages: targetOptions.IsSetNoMessages = true; break; case GrepOptionType.WithFileName: targetOptions.IsSetWithFileName = true; break; case GrepOptionType.NoFileName: targetOptions.IsSetNoFileName = true; break; case GrepOptionType.LineNumber: targetOptions.IsSetLineNumber = true; break; case GrepOptionType.Directory: targetOptions.IsSetDirectory = true; break; case GrepOptionType.ExcludeFiles: targetOptions.IsSetExcludeFiles = true; targetOptions.ExcludeFilesPattern = commandLineOptions.Arguments[arg]; break; case GrepOptionType.ExcludeDirectories: targetOptions.IsSetExcludeDirectories = true; targetOptions.ExcludeDirectoriesPattern = commandLineOptions.Arguments[arg]; break; case GrepOptionType.IncludeFiles: targetOptions.IsSetIncludeFiles = true; targetOptions.IncludeFilesPattern = commandLineOptions.Arguments[arg]; break; case GrepOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case GrepOptionType.Help: targetOptions.IsSetHelp = true; break; case GrepOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetRegexPattern) { targetOptions.IsSetRegexPattern = true; targetOptions.RegexPattern = commandLineOptions.Parameters.First(); for (int i = 1; i < commandLineOptions.Parameters.Count; i++) { targetOptions.IsSetPath = true; targetOptions.FilePaths.Add(commandLineOptions.Parameters.ElementAt(i)); } } else { if (!targetOptions.IsSetPath) { targetOptions.IsSetPath = true; foreach (var item in commandLineOptions.Parameters) { targetOptions.FilePaths.Add(item); } } } } return targetOptions; }
private static ChecksumCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); ChecksumCommandLineOptions targetOptions = new ChecksumCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { ChecksumOptionType optionType = ChecksumOptions.GetOptionType(arg); if (optionType == ChecksumOptionType.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 ChecksumOptionType.Algorithm: targetOptions.IsSetAlgorithm = true; if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC32" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC64" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"MD5" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA1" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA256" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA384" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA512" || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"RIPEMD160" ) { targetOptions.Algorithm = commandLineOptions.Arguments[arg]; } else { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid algorithm, support CRC32, CRC64, MD5, SHA1, SHA256, SHA384, SHA512, RIPEMD160.")); } break; case ChecksumOptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case ChecksumOptionType.Text: targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Arguments[arg]; break; case ChecksumOptionType.Help: targetOptions.IsSetHelp = true; break; case ChecksumOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }
private static ExtractCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); ExtractCommandLineOptions targetOptions = new ExtractCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { ExtractOptionType optionType = ExtractOptions.GetOptionType(arg); if (optionType == ExtractOptionType.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 ExtractOptionType.RegexPattern: targetOptions.RegexPattern = commandLineOptions.Arguments[arg]; break; case ExtractOptionType.InputDirectory: targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case ExtractOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case ExtractOptionType.InputFileExtensionFilter: targetOptions.InputFileExtensionFilter.AddRange( commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList()); break; case ExtractOptionType.OutputDirectory: targetOptions.OutputDirectory = commandLineOptions.Arguments[arg]; break; case ExtractOptionType.OutputFileExtension: targetOptions.OutputFileExtension = commandLineOptions.Arguments[arg].Trim(); break; case ExtractOptionType.Exclude: targetOptions.Excludes.AddRange( commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList()); break; case ExtractOptionType.Help: targetOptions.IsSetHelp = true; break; case ExtractOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }
private static KillProcessCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); KillProcessCommandLineOptions targetOptions = new KillProcessCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { KillProcessOptionType optionType = KillProcessOptions.GetOptionType(arg); if (optionType == KillProcessOptionType.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 KillProcessOptionType.Pid: targetOptions.IsSetPid = true; targetOptions.Pid = commandLineOptions.Arguments[arg]; break; case KillProcessOptionType.Name: targetOptions.IsSetName = true; targetOptions.Name = commandLineOptions.Arguments[arg]; break; case KillProcessOptionType.Help: targetOptions.IsSetHelp = true; break; case KillProcessOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.Pid)) { targetOptions.IsSetPid = true; targetOptions.Pid = commandLineOptions.Parameters.First(); } } return targetOptions; }
private static ReplaceCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); ReplaceCommandLineOptions targetOptions = new ReplaceCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { ReplaceOptionType optionType = ReplaceOptions.GetOptionType(arg); if (optionType == ReplaceOptionType.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 ReplaceOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.FromText: targetOptions.FromText = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.ToText: targetOptions.ToText = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.InputDirectory: targetOptions.IsSetInputDirectory = true; targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case ReplaceOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case ReplaceOptionType.Extension: targetOptions.Extensions.AddRange( commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList()); break; case ReplaceOptionType.Help: targetOptions.IsSetHelp = true; break; case ReplaceOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = 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; }
private static ListDirectoryCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); ListDirectoryCommandLineOptions targetOptions = new ListDirectoryCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { ListDirectoryOptionType optionType = ListDirectoryOptions.GetOptionType(arg); if (optionType == ListDirectoryOptionType.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 ListDirectoryOptionType.InputDirectory: targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case ListDirectoryOptionType.Directory: targetOptions.IsSetDirectory = true; break; case ListDirectoryOptionType.File: targetOptions.IsSetFile = true; break; case ListDirectoryOptionType.List: targetOptions.IsSetList = true; break; case ListDirectoryOptionType.Help: targetOptions.IsSetHelp = true; break; case ListDirectoryOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputDirectory)) { targetOptions.InputDirectory = commandLineOptions.Parameters.First(); } } // set default the current directory if (string.IsNullOrEmpty(targetOptions.InputDirectory)) { targetOptions.InputDirectory = @"."; } // set default options if (!targetOptions.IsSetDirectory && !targetOptions.IsSetFile) { targetOptions.IsSetDirectory = true; targetOptions.IsSetFile = true; } return targetOptions; }
private static TailCommandLineOptions ParseOptions(CommandLineOptions options) { if (options == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); TailCommandLineOptions targetOptions = new TailCommandLineOptions(); if (options.Arguments.Count > 0) { foreach (var arg in options.Arguments.Keys) { TailOptionType optionType = TailOptions.GetOptionType(arg); if (optionType == TailOptionType.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 TailOptionType.Retry: targetOptions.IsSetRetry = true; break; case TailOptionType.Follow: targetOptions.IsSetFollow = true; targetOptions.File = options.Arguments[arg]; break; case TailOptionType.FollowRetry: targetOptions.IsSetFollow = true; targetOptions.IsSetRetry = true; targetOptions.File = options.Arguments[arg]; break; case TailOptionType.OutputLines: long outputLines = 0; if (!long.TryParse(options.Arguments[arg], out outputLines)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid output lines option value.")); } if (outputLines <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid output lines option value.")); } targetOptions.OutputLines = outputLines; break; case TailOptionType.SleepInterval: long sleepInterval = 0; if (!long.TryParse(options.Arguments[arg], out sleepInterval)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid sleep interval option value.")); } if (sleepInterval <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid sleep interval option value.")); } targetOptions.SleepInterval = sleepInterval; break; case TailOptionType.Help: targetOptions.IsSetHelp = true; break; case TailOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (!targetOptions.IsSetHelp && !targetOptions.IsSetVersion) { if (string.IsNullOrEmpty(targetOptions.File)) { if (options.Parameters.Count <= 0) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must follow a file.")); if (options.Parameters.Count > 1) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "can only follow one file.")); targetOptions.File = options.Parameters.First(); } else { if (options.Parameters.Count > 0) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "can only follow one file.")); } if (targetOptions.IsSetRetry && !targetOptions.IsSetFollow) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "keep trying to open a file should follow a file name explicitly.")); } return targetOptions; }
private static SelectCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); SelectCommandLineOptions targetOptions = new SelectCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { SelectOptionType optionType = SelectOptions.GetOptionType(arg); if (optionType == SelectOptionType.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 SelectOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case SelectOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case SelectOptionType.Extension: targetOptions.IsSetExtension = true; targetOptions.Extension = commandLineOptions.Arguments[arg]; break; case SelectOptionType.Output: targetOptions.IsSetOutput = true; targetOptions.Output = commandLineOptions.Arguments[arg]; break; case SelectOptionType.Copy: targetOptions.IsSetCopy = true; break; case SelectOptionType.Move: targetOptions.IsSetMove = true; break; case SelectOptionType.KeepDepth: targetOptions.IsSetKeepDepth = true; int depth = 0; if (!int.TryParse(commandLineOptions.Arguments[arg], out depth)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid depth.")); } if (depth <= 0 || depth > 10) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid depth [1, 9].")); } targetOptions.KeepDepth = depth; break; case SelectOptionType.Help: targetOptions.IsSetHelp = true; break; case SelectOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.Directory)) { targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Parameters.First(); } } if (targetOptions.IsSetOutput && !targetOptions.IsSetMove) { targetOptions.IsSetCopy = true; } return targetOptions; }
private static AddTextCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); AddTextCommandLineOptions targetOptions = new AddTextCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { AddTextOptionType optionType = AddTextOptions.GetOptionType(arg); if (optionType == AddTextOptionType.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 AddTextOptionType.File: targetOptions.File = commandLineOptions.Arguments[arg]; break; case AddTextOptionType.Text: targetOptions.Text = commandLineOptions.Arguments[arg]; break; case AddTextOptionType.FromFile: targetOptions.IsSetFromFile = true; targetOptions.FromFile = commandLineOptions.Arguments[arg]; break; case AddTextOptionType.Top: targetOptions.IsSetTop = true; break; case AddTextOptionType.Bottom: targetOptions.IsSetBottom = true; break; case AddTextOptionType.Help: targetOptions.IsSetHelp = true; break; case AddTextOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }
private static Base64CommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); Base64CommandLineOptions targetOptions = new Base64CommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { Base64OptionType optionType = Base64Options.GetOptionType(arg); if (optionType == Base64OptionType.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 Base64OptionType.Decode: targetOptions.IsSetDecode = true; break; case Base64OptionType.Encoding: targetOptions.IsSetEncoding = true; if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII") { targetOptions.Encoding = Encoding.ASCII; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7") { targetOptions.Encoding = Encoding.UTF7; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8") { targetOptions.Encoding = Encoding.UTF8; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE") { targetOptions.Encoding = Encoding.Unicode; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32") { targetOptions.Encoding = Encoding.UTF32; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE") { targetOptions.Encoding = Encoding.BigEndianUnicode; } else { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid encoding, support ASCII, UTF7, UTF8, UTF32, Unicode, BigEndianUnicode.")); } break; case Base64OptionType.Text: targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Arguments[arg]; break; case Base64OptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case Base64OptionType.Help: targetOptions.IsSetHelp = true; break; case Base64OptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetText) { targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Parameters.First(); } } return targetOptions; }
private static CopySameCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); CopySameCommandLineOptions targetOptions = new CopySameCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { CopySameOptionType optionType = CopySameOptions.GetOptionType(arg); if (optionType == CopySameOptionType.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 CopySameOptionType.Source: targetOptions.IsSetSourceFolder = true; targetOptions.SourceFolder = commandLineOptions.Arguments[arg]; break; case CopySameOptionType.Destination: targetOptions.IsSetDestinationFolder = true; targetOptions.DestinationFolder = commandLineOptions.Arguments[arg]; break; case CopySameOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case CopySameOptionType.Help: targetOptions.IsSetHelp = true; break; case CopySameOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }