public static int Main(string[] args) { // Launch debugger if the "--debugger" flag is present in the command line arguments. // We want to do this as early as possible so just check the flag manually, rather than doing the // more robust argument parsing. if (args.Any(i => i == "--debugger")) { Debugger.Launch(); } // Default to GUI if there are no command line args or if the only args are flags rather than commands. if (args.All(a => a == "--verbose" || a == "--debug" || a == "--asroot" || a == "--show-console")) { var guiCommand = args.ToList(); guiCommand.Insert(0, "gui"); args = guiCommand.ToArray(); } Logging.Initialize(); log.Info("CKAN started."); // Force-allow TLS 1.2 for HTTPS URLs, because GitHub requires it. // This is on by default in .NET 4.6, but not in 4.5. ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; try { return(Execute(null, null, args)); } finally { RegistryManager.DisposeAll(); } }
public int RunSubCommand(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; User = new ConsoleUser(options.Headless); KSPManager 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(); }); RegistryManager.DisposeAll(); return(exitCode); }
/// <summary> /// Shared entry point for the application, used by real command line /// and by other parts of CKAN that want to launch the console UI. /// </summary> /// <param name="args">Command line arguments</param> /// <param name="manager">Game instance manager object potentially initialized by command line flags</param> /// <param name="debug">True if debug options should be available, false otherwise</param> /// <returns> /// Process exit status /// </returns> public static int Main_(string[] args, GameInstanceManager manager, bool debug = false) { Logging.Initialize(); new ConsoleCKAN(manager, debug); // Tell RegistryManager not to throw Dispose-related exceptions at exit RegistryManager.DisposeAll(); return(0); }
public static int Main(string[] args) { // Launch debugger if the "--debugger" flag is present in the command line arguments. // We want to do this as early as possible so just check the flag manually, rather than doing the // more robust argument parsing. if (args.Any(i => i == "--debugger")) { Debugger.Launch(); } if (args.Length == 1 && args.Any(i => i == "--verbose" || i == "--debug")) { // Start the gui with logging enabled #437 var guiCommand = args.ToList(); guiCommand.Insert(0, "gui"); args = guiCommand.ToArray(); } Logging.Initialize(); log.Info("CKAN started."); // Force-allow TLS 1.2 for HTTPS URLs, because GitHub requires it. // This is on by default in .NET 4.6, but not in 4.5. ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; // If we're starting with no options then invoke the GUI instead. if (args.Length == 0) { return(Gui(new GuiOptions(), args)); } try { return(Execute(null, null, args)); } finally { RegistryManager.DisposeAll(); } }
/// <summary> /// Run whatever action the user has provided /// </summary> /// <returns>The exit status that should be returned to the system.</returns> private static int RunSimpleAction(Options cmdline, CommonOptions options, string[] args, IUser user, KSPManager manager) { try { switch (cmdline.action) { case "gui": return(Gui(manager, (GuiOptions)options, args)); case "consoleui": return(ConsoleUi(manager, options, args)); case "prompt": return(new Prompt().RunCommand(manager, cmdline.options)); case "version": return(Version(user)); case "update": return((new Update(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "available": return((new Available(user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "add": case "install": Scan(GetGameInstance(manager), user, cmdline.action); return((new Install(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "scan": return(Scan(GetGameInstance(manager), user)); case "list": return((new List(user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "show": return((new Show(user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "replace": Scan(GetGameInstance(manager), user, cmdline.action); return((new Replace(manager, user)).RunCommand(GetGameInstance(manager), (ReplaceOptions)cmdline.options)); case "upgrade": Scan(GetGameInstance(manager), user, cmdline.action); return((new Upgrade(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "search": return((new Search(user)).RunCommand(GetGameInstance(manager), options)); case "uninstall": case "remove": return((new Remove(manager, user)).RunCommand(GetGameInstance(manager), cmdline.options)); case "import": return((new Import(manager, user)).RunCommand(GetGameInstance(manager), options)); case "clean": return(Clean(manager.Cache)); case "compare": return((new Compare(user)).RunCommand(cmdline.options)); default: user.RaiseMessage("Unknown command, try --help"); return(Exit.BADOPT); } } catch (NoGameInstanceKraken) { return(printMissingInstanceError(user)); } finally { RegistryManager.DisposeAll(); } }
// This is required by ISubCommand public int RunSubCommand(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; User = new ConsoleUser(options.Headless); 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 "rename": exitCode = RenameInstall((RenameOptions)suboptions); break; case "forget": exitCode = ForgetInstall((ForgetOptions)suboptions); break; case "use": case "default": exitCode = SetDefaultInstall((DefaultOptions)suboptions); break; default: User.RaiseMessage("Unknown command: ksp {0}", option); exitCode = Exit.BADOPT; break; } } }, () => { exitCode = MainClass.AfterHelp(); }); RegistryManager.DisposeAll(); return(exitCode); }
public int RunSubCommand(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; _user = new ConsoleUser(comOpts.Headless); _kspManager = new KSPManager(_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; 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 = MainClass.GetGameInstance(_kspManager); 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; } } }, () => { exitCode = MainClass.AfterHelp(); }); RegistryManager.DisposeAll(); return(exitCode); }