Example #1
0
        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;
        }
Example #2
0
        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;
        }
Example #3
0
        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 ProcessStatusCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              ProcessStatusCommandLineOptions targetOptions = new ProcessStatusCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              ProcessStatusOptionType optionType = ProcessStatusOptions.GetOptionType(arg);
              if (optionType == ProcessStatusOptionType.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 ProcessStatusOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case ProcessStatusOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

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

              SyncCopyCommandLineOptions targetOptions = new SyncCopyCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              SyncCopyOptionType optionType = SyncCopyOptions.GetOptionType(arg);
              if (optionType == SyncCopyOptionType.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 SyncCopyOptionType.FromDirectory:
              targetOptions.FromDirectory = commandLineOptions.Arguments[arg];
              break;
            case SyncCopyOptionType.ToDirectory:
              targetOptions.ToDirectory = commandLineOptions.Arguments[arg];
              break;
            case SyncCopyOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case SyncCopyOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case SyncCopyOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              return targetOptions;
        }
Example #6
0
        /// <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;
        }
Example #7
0
        private static RenameCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              RenameCommandLineOptions targetOptions = new RenameCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              RenameOptionType optionType = RenameOptions.GetOptionType(arg);
              if (optionType == RenameOptionType.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 RenameOptionType.RegexPattern:
              targetOptions.RegexPattern = commandLineOptions.Arguments[arg];
              break;
            case RenameOptionType.InputDirectory:
              targetOptions.InputDirectory = commandLineOptions.Arguments[arg];
              break;
            case RenameOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case RenameOptionType.OutputPattern:
              targetOptions.OutputPattern = commandLineOptions.Arguments[arg];
              break;
            case RenameOptionType.Folder:
              targetOptions.IsSetFolder = true;
              break;
            case RenameOptionType.Exclude:
              targetOptions.Excludes.AddRange(
                commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
              break;
            case RenameOptionType.PadString:
              targetOptions.IsSetPadString = true;
              targetOptions.PadString = commandLineOptions.Arguments[arg];
              break;
            case RenameOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case RenameOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              return targetOptions;
        }
Example #8
0
        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;
        }
Example #9
0
        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 RemoveDirectoryCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              RemoveDirectoryCommandLineOptions targetOptions = new RemoveDirectoryCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              RemoveDirectoryOptionType optionType = RemoveDirectoryOptions.GetOptionType(arg);
              if (optionType == RemoveDirectoryOptionType.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 RemoveDirectoryOptionType.Directory:
              targetOptions.Directory = commandLineOptions.Arguments[arg];
              break;
            case RemoveDirectoryOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case RemoveDirectoryOptionType.RegexPattern:
              targetOptions.RegexPattern = commandLineOptions.Arguments[arg];
              break;
            case RemoveDirectoryOptionType.FixedString:
              targetOptions.IsSetFixedString = true;
              break;
            case RemoveDirectoryOptionType.Empty:
              targetOptions.IsSetEmpty = true;
              break;
            case RemoveDirectoryOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case RemoveDirectoryOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              return targetOptions;
        }
Example #11
0
        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;
        }
Example #12
0
        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;
        }
Example #13
0
        private static JoinCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              JoinCommandLineOptions targetOptions = new JoinCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              JoinOptionType optionType = JoinOptions.GetOptionType(arg);
              if (optionType == JoinOptionType.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 JoinOptionType.OutputFile:
              targetOptions.OutputFile = commandLineOptions.Arguments[arg];
              break;
            case JoinOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case JoinOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              if (commandLineOptions.Parameters.Count > 0)
              {
            foreach (var item in commandLineOptions.Parameters)
            {
              targetOptions.InputFiles.Add(item);
            }
              }

              return targetOptions;
        }
Example #14
0
        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;
        }
Example #15
0
        private static FindCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              FindCommandLineOptions targetOptions = new FindCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              FindOptionType optionType = FindOptions.GetOptionType(arg);
              if (optionType == FindOptionType.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 FindOptionType.RegexPattern:
              targetOptions.IsSetRegexPattern = true;
              targetOptions.RegexPattern = commandLineOptions.Arguments[arg];
              break;
            case FindOptionType.Directory:
              targetOptions.IsSetDirectory = true;
              targetOptions.Directory = commandLineOptions.Arguments[arg];
              break;
            case FindOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case FindOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case FindOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

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

            if (commandLineOptions.Parameters.Count >= 2)
            {
              if (!targetOptions.IsSetRegexPattern)
              {
            targetOptions.IsSetRegexPattern = true;
            targetOptions.RegexPattern = commandLineOptions.Parameters.ElementAt(1);
              }
            }
              }

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

              TailCommandLineOptions targetOptions = new TailCommandLineOptions();

              if (commandLineOptions.Arguments.Count > 0)
              {
            foreach (var arg in commandLineOptions.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 = commandLineOptions.Arguments[arg];
              break;
            case TailOptionType.FollowRetry:
              targetOptions.IsSetFollow = true;
              targetOptions.IsSetRetry = true;
              targetOptions.File = commandLineOptions.Arguments[arg];
              break;
            case TailOptionType.OutputLines:
              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 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(commandLineOptions.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 (commandLineOptions.Parameters.Count <= 0)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must follow a file."));
              if (commandLineOptions.Parameters.Count > 1)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "can only follow one file."));

              targetOptions.File = commandLineOptions.Parameters.First();
            }
            else
            {
              if (commandLineOptions.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 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;
        }
Example #18
0
        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;
        }