Exemplo n.º 1
0
        public int RunSubCommand(SubCommandOptions options)
        {
            var exitCode = 0;

            Parser.Default.ParseArgumentsStrict(options.options.ToArray(), new CompatOptions(), (option, suboptions) =>
            {
                switch (option)
                {
                    case "list":
                        {
                            var ksp = _kspManager.CurrentInstance;

                            const string versionHeader = "Version";
                            const string actualHeader = "Actual";

                            var output = ksp
                                .GetCompatibleVersions()
                                .Select(i => new
                                {
                                    Version = i,
                                    Actual = false
                                })
                                .ToList();

                            output.Add(new
                            {
                                Version = ksp.Version(),
                                Actual = true
                            });

                            output = output
                                .OrderByDescending(i => i.Actual)
                                .ThenByDescending(i => i.Version)
                                .ToList();

                            var versionWidth = Enumerable
                                .Repeat(versionHeader, 1)
                                .Concat(output.Select(i => i.Version.ToString()))
                                .Max(i => i.Length);

                            var actualWidth = Enumerable
                                .Repeat(actualHeader, 1)
                                .Concat(output.Select(i => i.Actual.ToString()))
                                .Max(i => i.Length);

                            const string columnFormat = "{0}  {1}";

                            _user.RaiseMessage(string.Format(columnFormat,
                                versionHeader.PadRight(versionWidth),
                                actualHeader.PadRight(actualWidth)
                            ));

                            _user.RaiseMessage(string.Format(columnFormat,
                                new string('-', versionWidth),
                                new string('-', actualWidth)
                            ));

                            foreach (var line in output)
                            {
                                _user.RaiseMessage(string.Format(columnFormat,
                                   line.Version.ToString().PadRight(versionWidth),
                                   line.Actual.ToString().PadRight(actualWidth)
                               ));
                            }
                        }
                        break;
                    case "add":
                        {
                            var ksp = _kspManager.CurrentInstance;
                            var addOptions = (CompatAddOptions)suboptions;

                            KspVersion kspVersion;
                            if (KspVersion.TryParse(addOptions.Version, out kspVersion))
                            {
                                var newCompatibleVersion = ksp.GetCompatibleVersions();
                                newCompatibleVersion.Add(kspVersion);
                                ksp.SetCompatibleVersions(newCompatibleVersion);
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;
                    case "forget":
                        {
                            var ksp = _kspManager.CurrentInstance;
                            var addOptions = (CompatForgetOptions)suboptions;

                            KspVersion kspVersion;
                            if (KspVersion.TryParse(addOptions.Version, out kspVersion))
                            {
                                if (kspVersion != ksp.Version())
                                {
                                    var newCompatibleVersion = ksp.GetCompatibleVersions();
                                    newCompatibleVersion.RemoveAll(i => i == kspVersion);
                                    ksp.SetCompatibleVersions(newCompatibleVersion);
                                }
                                else
                                {
                                    _user.RaiseError("ERROR: Cannot forget actual KSP version.");
                                    exitCode = Exit.ERROR;
                                }
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;
                    default:
                        exitCode = Exit.BADOPT;
                        break;
                }
            });

            return exitCode;
        }
Exemplo n.º 2
0
        public int RunSubCommand(GameInstanceManager manager, CommonOptions opts, SubCommandOptions options)
        {
            var exitCode = Exit.OK;

            Parser.Default.ParseArgumentsStrict(options.options.ToArray(), new CompatOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions comOpts = (CommonOptions)suboptions;
                    comOpts.Merge(opts);
                    _user       = new ConsoleUser(comOpts.Headless);
                    _kspManager = manager ?? new GameInstanceManager(_user);
                    exitCode    = comOpts.Handle(_kspManager, _user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        {
                            var ksp = MainClass.GetGameInstance(_kspManager);

                            const string versionHeader = "Version";
                            const string actualHeader  = "Actual";

                            var output = ksp
                                         .GetCompatibleVersions()
                                         .Select(i => new
                            {
                                Version = i,
                                Actual  = false
                            })
                                         .ToList();

                            output.Add(new
                            {
                                Version = ksp.Version(),
                                Actual  = true
                            });

                            output = output
                                     .OrderByDescending(i => i.Actual)
                                     .ThenByDescending(i => i.Version)
                                     .ToList();

                            var versionWidth = Enumerable
                                               .Repeat(versionHeader, 1)
                                               .Concat(output.Select(i => i.Version.ToString()))
                                               .Max(i => i.Length);

                            var actualWidth = Enumerable
                                              .Repeat(actualHeader, 1)
                                              .Concat(output.Select(i => i.Actual.ToString()))
                                              .Max(i => i.Length);

                            const string columnFormat = "{0}  {1}";

                            _user.RaiseMessage(string.Format(columnFormat,
                                                             versionHeader.PadRight(versionWidth),
                                                             actualHeader.PadRight(actualWidth)
                                                             ));

                            _user.RaiseMessage(string.Format(columnFormat,
                                                             new string('-', versionWidth),
                                                             new string('-', actualWidth)
                                                             ));

                            foreach (var line in output)
                            {
                                _user.RaiseMessage(string.Format(columnFormat,
                                                                 line.Version.ToString().PadRight(versionWidth),
                                                                 line.Actual.ToString().PadRight(actualWidth)
                                                                 ));
                            }
                        }
                        break;

                    case "add":
                        {
                            var ksp        = MainClass.GetGameInstance(_kspManager);
                            var addOptions = (CompatAddOptions)suboptions;

                            GameVersion GameVersion;
                            if (GameVersion.TryParse(addOptions.Version, out GameVersion))
                            {
                                var newCompatibleVersion = ksp.GetCompatibleVersions();
                                newCompatibleVersion.Add(GameVersion);
                                ksp.SetCompatibleVersions(newCompatibleVersion);
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;

                    case "forget":
                        {
                            var ksp        = MainClass.GetGameInstance(_kspManager);
                            var addOptions = (CompatForgetOptions)suboptions;

                            GameVersion GameVersion;
                            if (GameVersion.TryParse(addOptions.Version, out GameVersion))
                            {
                                if (GameVersion != ksp.Version())
                                {
                                    var newCompatibleVersion = ksp.GetCompatibleVersions();
                                    newCompatibleVersion.RemoveAll(i => i == GameVersion);
                                    ksp.SetCompatibleVersions(newCompatibleVersion);
                                }
                                else
                                {
                                    _user.RaiseError("ERROR: Cannot forget actual KSP version.");
                                    exitCode = Exit.ERROR;
                                }
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Invalid KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        break;

                    default:
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Exemplo n.º 3
0
        public int RunSubCommand(SubCommandOptions options)
        {
            var exitCode = 0;

            Parser.Default.ParseArgumentsStrict(options.options.ToArray(), new CompatOptions(), (option, suboptions) =>
            {
                switch (option)
                {
                case "list":
                    {
                        var ksp = _kspManager.CurrentInstance;

                        const string versionHeader = "Version";
                        const string actualHeader  = "Actual";

                        var output = ksp
                                     .GetCompatibleVersions()
                                     .Select(i => new
                        {
                            Version = i,
                            Actual  = false
                        })
                                     .ToList();

                        output.Add(new
                        {
                            Version = ksp.Version(),
                            Actual  = true
                        });

                        output = output
                                 .OrderByDescending(i => i.Actual)
                                 .ThenByDescending(i => i.Version)
                                 .ToList();

                        var versionWidth = Enumerable
                                           .Repeat(versionHeader, 1)
                                           .Concat(output.Select(i => i.Version.ToString()))
                                           .Max(i => i.Length);

                        var actualWidth = Enumerable
                                          .Repeat(actualHeader, 1)
                                          .Concat(output.Select(i => i.Actual.ToString()))
                                          .Max(i => i.Length);

                        const string columnFormat = "{0}  {1}";

                        _user.RaiseMessage(string.Format(columnFormat,
                                                         versionHeader.PadRight(versionWidth),
                                                         actualHeader.PadRight(actualWidth)
                                                         ));

                        _user.RaiseMessage(string.Format(columnFormat,
                                                         new string('-', versionWidth),
                                                         new string('-', actualWidth)
                                                         ));

                        foreach (var line in output)
                        {
                            _user.RaiseMessage(string.Format(columnFormat,
                                                             line.Version.ToString().PadRight(versionWidth),
                                                             line.Actual.ToString().PadRight(actualWidth)
                                                             ));
                        }
                    }
                    break;

                case "add":
                    {
                        var ksp        = _kspManager.CurrentInstance;
                        var addOptions = (CompatAddOptions)suboptions;

                        KspVersion kspVersion;
                        if (KspVersion.TryParse(addOptions.Version, out kspVersion))
                        {
                            var newCompatibleVersion = ksp.GetCompatibleVersions();
                            newCompatibleVersion.Add(kspVersion);
                            ksp.SetCompatibleVersions(newCompatibleVersion);
                        }
                        else
                        {
                            _user.RaiseError("ERROR: Invalid KSP version.");
                            exitCode = Exit.ERROR;
                        }
                    }
                    break;

                case "forget":
                    {
                        var ksp        = _kspManager.CurrentInstance;
                        var addOptions = (CompatForgetOptions)suboptions;

                        KspVersion kspVersion;
                        if (KspVersion.TryParse(addOptions.Version, out kspVersion))
                        {
                            if (kspVersion != ksp.Version())
                            {
                                var newCompatibleVersion = ksp.GetCompatibleVersions();
                                newCompatibleVersion.RemoveAll(i => i == kspVersion);
                                ksp.SetCompatibleVersions(newCompatibleVersion);
                            }
                            else
                            {
                                _user.RaiseError("ERROR: Cannot forget actual KSP version.");
                                exitCode = Exit.ERROR;
                            }
                        }
                        else
                        {
                            _user.RaiseError("ERROR: Invalid KSP version.");
                            exitCode = Exit.ERROR;
                        }
                    }
                    break;

                default:
                    exitCode = Exit.BADOPT;
                    break;
                }
            });

            return(exitCode);
        }