-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Prompt.cs
66 lines (61 loc) · 2.32 KB
/
Prompt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using CommandLine;
using CommandLine.Text;
using log4net;
namespace CKAN.CmdLine
{
public class Prompt
{
public Prompt() { }
public int RunCommand(KSPManager manager, object raw_options)
{
CommonOptions opts = raw_options as CommonOptions;
// Print an intro if not in headless mode
if (!(opts?.Headless ?? false))
{
Console.WriteLine("Welcome to CKAN!");
Console.WriteLine("");
if (DateTime.Now.Month == 4 && DateTime.Now.Day == 1)
{
Console.WriteLine("Happy April Fools' Day! You may want to try the consoleui command.");
Console.WriteLine("");
}
Console.WriteLine("To get help, type help and press enter.");
Console.WriteLine("");
}
bool done = false;
while (!done)
{
// Prompt if not in headless mode
if (!(opts?.Headless ?? false))
{
Console.Write(
manager.CurrentInstance != null
? $"CKAN {Meta.GetVersion()}: KSP {manager.CurrentInstance.Version()} ({manager.CurrentInstance.Name})> "
: $"CKAN {Meta.GetVersion()}> "
);
}
// Get input
string command = Console.ReadLine();
if (command == null || command == exitCommand)
{
done = true;
}
else if (command != "")
{
// Parse input as if it was a normal command line,
// but with a persistent KSPManager object.
int cmdExitCode = MainClass.Execute(manager, opts, command.Split(' '));
if ((opts?.Headless ?? false) && cmdExitCode != Exit.OK)
{
// Pass failure codes to calling process in headless mode
// (in interactive mode the user can see the error and try again)
return cmdExitCode;
}
}
}
return Exit.OK;
}
private const string exitCommand = "exit";
}
}