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
        public static KillProcessOptionType GetOptionType(string option)
        {
            KillProcessOptionType optionType = KillProcessOptionType.None;

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

            return(optionType);
        }