Example #1
0
        /// <summary>
        /// Execute a cache subcommand
        /// </summary>
        /// <param name="mgr">KSPManager object containing our instances and cache</param>
        /// <param name="opts">Command line options object</param>
        /// <param name="unparsed">Raw command line options</param>
        /// <returns>
        /// Exit code for shell environment
        /// </returns>
        public int RunSubCommand(KSPManager mgr, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args = unparsed.options.ToArray();

            int exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new CacheSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user     = new ConsoleUser(options.Headless);
                    manager  = mgr ?? new KSPManager(user);
                    exitCode = options.Handle(manager, user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        exitCode = ListCacheDirectory((CommonOptions)suboptions);
                        break;

                    case "set":
                        exitCode = SetCacheDirectory((SetOptions)suboptions);
                        break;

                    case "clear":
                        exitCode = ClearCacheDirectory((CommonOptions)suboptions);
                        break;

                    case "reset":
                        exitCode = ResetCacheDirectory((CommonOptions)suboptions);
                        break;

                    case "showlimit":
                        exitCode = ShowCacheSizeLimit((CommonOptions)suboptions);
                        break;

                    case "setlimit":
                        exitCode = SetCacheSizeLimit((SetLimitOptions)suboptions);
                        break;

                    default:
                        user.RaiseMessage("Unknown command: cache {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Example #2
0
        /// <summary>
        /// Run the subcommand
        /// </summary>
        /// <param name="mgr">Manager to provide game instances</param>
        /// <param name="opts">Command line parameters paritally handled by parser</param>
        /// <param name="unparsed">Command line parameters not yet handled by parser</param>
        /// <returns>
        /// Exit code
        /// </returns>
        public int RunSubCommand(GameInstanceManager mgr, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args     = unparsed.options.ToArray();
            int      exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new FilterSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user     = new ConsoleUser(options.Headless);
                    manager  = mgr ?? new GameInstanceManager(user);
                    exitCode = options.Handle(manager, user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        exitCode = ListFilters((FilterListOptions)suboptions, option);
                        break;

                    case "add":
                        exitCode = AddFilters((FilterAddOptions)suboptions, option);
                        break;

                    case "remove":
                        exitCode = RemoveFilters((FilterRemoveOptions)suboptions, option);
                        break;

                    default:
                        user.RaiseMessage("Unknown command: filter {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Example #3
0
        /// <summary>
        /// Run the subcommand
        /// </summary>
        /// <param name="mgr">Manager to provide game instances</param>
        /// <param name="opts">Command line parameters paritally handled by parser</param>
        /// <param name="unparsed">Command line parameters not yet handled by parser</param>
        /// <returns>
        /// Exit code
        /// </returns>
        public int RunSubCommand(GameInstanceManager mgr, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args     = unparsed.options.ToArray();
            int      exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new MarkSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user     = new ConsoleUser(options.Headless);
                    manager  = mgr ?? new GameInstanceManager(user);
                    exitCode = options.Handle(manager, user);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "auto":
                        exitCode = MarkAuto((MarkAutoOptions)suboptions, true, option, "auto-installed");
                        break;

                    case "user":
                        exitCode = MarkAuto((MarkAutoOptions)suboptions, false, option, "user-selected");
                        break;

                    default:
                        user.RaiseMessage("Unknown command: mark {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Example #4
0
        /// <summary>
        /// Run the subcommand
        /// </summary>
        /// <param name="mgr">Manager to provide game instances</param>
        /// <param name="opts">Command line parameters paritally handled by parser</param>
        /// <param name="unparsed">Command line parameters not yet handled by parser</param>
        /// <returns>
        /// Exit code
        /// </returns>
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args     = unparsed.options.ToArray();
            int      exitCode = Exit.OK;

            Parser.Default.ParseArgumentsStrict(args, new AuthTokenSubOptions(), (string option, object suboptions) =>
            {
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    user = new ConsoleUser(options.Headless);
                    if (manager == null)
                    {
                        manager = new KSPManager(user);
                    }
                    exitCode = options.Handle(manager, user);
                    if (exitCode == Exit.OK)
                    {
                        switch (option)
                        {
                        case "list":
                            exitCode = listAuthTokens(options);
                            break;

                        case "add":
                            exitCode = addAuthToken((AddAuthTokenOptions)options);
                            break;

                        case "remove":
                            exitCode = removeAuthToken((RemoveAuthTokenOptions)options);
                            break;
                        }
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Example #5
0
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            int exitCode = Exit.OK;

            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(unparsed.options.ToArray(), new RepairSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    User = new ConsoleUser(options.Headless);
                    if (manager == null)
                    {
                        manager = new KSPManager(User);
                    }
                    exitCode = options.Handle(manager, User);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "registry":
                        exitCode = Registry(MainClass.GetGameInstance(manager));
                        break;

                    default:
                        User.RaiseMessage("Unknown command: repair {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }
Example #6
0
        public static int Execute(KSPManager manager, CommonOptions opts, string[] args)
        {
            // We shouldn't instantiate Options if it's a subcommand.
            // It breaks command-specific help, for starters.
            try
            {
                switch (args[0])
                {
                case "repair":
                    return((new Repair()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "ksp":
                    return((new KSP()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "compat":
                    return((new Compat()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "repo":
                    return((new Repo()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "authtoken":
                    return((new AuthToken()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "cache":
                    return((new Cache()).RunSubCommand(manager, opts, new SubCommandOptions(args)));

                case "mark":
                    return((new Mark()).RunSubCommand(manager, opts, new SubCommandOptions(args)));
                }
            }
            catch (NoGameInstanceKraken)
            {
                return(printMissingInstanceError(new ConsoleUser(false)));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            Options cmdline;

            try
            {
                cmdline = new Options(args);
            }
            catch (BadCommandKraken)
            {
                return(AfterHelp());
            }
            finally
            {
                log.Info("CKAN exiting.");
            }

            // Process commandline options.
            CommonOptions options = (CommonOptions)cmdline.options;

            options.Merge(opts);
            IUser user = new ConsoleUser(options.Headless);

            if (manager == null)
            {
                manager = new KSPManager(user);
            }
            else
            {
                manager.User = user;
            }

            try
            {
                int exitCode = options.Handle(manager, user);
                if (exitCode != Exit.OK)
                {
                    return(exitCode);
                }
                // Don't bother with instances or registries yet because some commands don't need them.
                return(RunSimpleAction(cmdline, options, args, user, manager));
            }
            finally
            {
                log.Info("CKAN exiting.");
            }
        }
Example #7
0
        // This is required by ISubCommand
        public int RunSubCommand(KSPManager manager, CommonOptions opts, SubCommandOptions unparsed)
        {
            string[] args = unparsed.options.ToArray();

            #region Aliases

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "use":
                    args[i] = "default";
                    break;

                default:
                    break;
                }
            }

            #endregion

            int exitCode = Exit.OK;
            // Parse and process our sub-verbs
            Parser.Default.ParseArgumentsStrict(args, new KSPSubOptions(), (string option, object suboptions) =>
            {
                // ParseArgumentsStrict calls us unconditionally, even with bad arguments
                if (!string.IsNullOrEmpty(option) && suboptions != null)
                {
                    CommonOptions options = (CommonOptions)suboptions;
                    options.Merge(opts);
                    User     = new ConsoleUser(options.Headless);
                    Manager  = manager ?? new KSPManager(User);
                    exitCode = options.Handle(Manager, User);
                    if (exitCode != Exit.OK)
                    {
                        return;
                    }

                    switch (option)
                    {
                    case "list":
                        exitCode = ListInstalls();
                        break;

                    case "add":
                        exitCode = AddInstall((AddOptions)suboptions);
                        break;

                    case "clone":
                        exitCode = CloneInstall((CloneOptions)suboptions);
                        break;

                    case "rename":
                        exitCode = RenameInstall((RenameOptions)suboptions);
                        break;

                    case "forget":
                        exitCode = ForgetInstall((ForgetOptions)suboptions);
                        break;

                    case "use":
                    case "default":
                        exitCode = SetDefaultInstall((DefaultOptions)suboptions);
                        break;

                    case "fake":
                        exitCode = FakeNewKSPInstall((FakeOptions)suboptions);
                        break;

                    default:
                        User.RaiseMessage("Unknown command: ksp {0}", option);
                        exitCode = Exit.BADOPT;
                        break;
                    }
                }
            }, () => { exitCode = MainClass.AfterHelp(); });
            return(exitCode);
        }