Пример #1
0
        public override void Execute()
        {
            base.Execute();

            List <string>      singleOptionList = KillProcessOptions.GetSingleOptions();
            CommandLineOptions cloptions        = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray());

            options = ParseOptions(cloptions);
            CheckOptions(options);

            if (options.IsSetHelp)
            {
                RaiseCommandLineUsage(this, KillProcessOptions.Usage);
            }
            else if (options.IsSetVersion)
            {
                RaiseCommandLineUsage(this, Version);
            }
            else
            {
                StartKillProcess();
            }

            Terminate();
        }
Пример #2
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);
        }
Пример #3
0
        private static void CheckOptions(KillProcessCommandLineOptions checkedOptions)
        {
            if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
            {
                if (!checkedOptions.IsSetPid && !checkedOptions.IsSetName)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "must specify a option."));
                }

                Process p = Process.GetCurrentProcess();

                if (checkedOptions.IsSetPid && checkedOptions.IsSetName)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "can only set pid or name."));
                }
                if (checkedOptions.IsSetPid && string.IsNullOrEmpty(checkedOptions.Pid))
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "invalid process pid format."));
                }
                if (checkedOptions.IsSetPid)
                {
                    int pid = 0;
                    if (!int.TryParse(checkedOptions.Pid, out pid))
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "invalid process pid number."));
                    }
                    else
                    {
                        if (pid == p.Id)
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "cannot kill current process by pid."));
                        }
                    }
                }
                if (checkedOptions.IsSetName && string.IsNullOrEmpty(checkedOptions.Name))
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "invalid process name."));
                }
                if (checkedOptions.IsSetName)
                {
                    if (checkedOptions.Name == p.ProcessName)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "cannot kill current process by name."));
                    }
                }
            }
        }
Пример #4
0
        public override void Execute()
        {
            base.Execute();

              List<string> singleOptionList = KillProcessOptions.GetSingleOptions();
              CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray());
              options = ParseOptions(cloptions);
              CheckOptions(options);

              if (options.IsSetHelp)
              {
            RaiseCommandLineUsage(this, KillProcessOptions.Usage);
              }
              else if (options.IsSetVersion)
              {
            RaiseCommandLineUsage(this, Version);
              }
              else
              {
            StartKillProcess();
              }

              Terminate();
        }
Пример #5
0
        private static void CheckOptions(KillProcessCommandLineOptions checkedOptions)
        {
            if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
              {
            if (!checkedOptions.IsSetPid && !checkedOptions.IsSetName)
            {
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "must specify a option."));
            }

            Process p = Process.GetCurrentProcess();

            if (checkedOptions.IsSetPid && checkedOptions.IsSetName)
            {
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "can only set pid or name."));
            }
            if (checkedOptions.IsSetPid && string.IsNullOrEmpty(checkedOptions.Pid))
            {
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "invalid process pid format."));
            }
            if (checkedOptions.IsSetPid)
            {
              int pid = 0;
              if (!int.TryParse(checkedOptions.Pid, out pid))
              {
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "invalid process pid number."));
              }
              else
              {
            if (pid == p.Id)
            {
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                "Option used in invalid context -- {0}", "cannot kill current process by pid."));
            }
              }
            }
            if (checkedOptions.IsSetName && string.IsNullOrEmpty(checkedOptions.Name))
            {
              throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "invalid process name."));
            }
            if (checkedOptions.IsSetName)
            {
              if (checkedOptions.Name == p.ProcessName)
              {
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "cannot kill current process by name."));
              }
            }
              }
        }
Пример #6
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;
        }