-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitSubcommandsExample.cs
184 lines (156 loc) · 6.09 KB
/
GitSubcommandsExample.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace Argparse.Examples;
// I was getting errors in build...
#if COMPILE
internal static class GitSubcommandsExample {
record GitConfig {
public bool Help = false;
public bool Version = false;
}
record GitCloneConfig : GitConfig {
public string? Repository;
}
record GitPushConfig : GitConfig {
public string? Repository;
public string? BranchOrRefspec;
public bool Tags = false;
}
record GitLogConfig : GitConfig {
public bool prettyPrint = false;
}
static readonly ArgumentMultiplicity ZeroOrOne = new ArgumentMultiplicity.SpecificCount(Number: 1, IsRequired: false);
public static void Run(string[] arguments)
{
Parser<GitConfig> GitParser = ConfigureParser();
GitParser.ParseAndRun(arguments);
}
private static Parser<GitConfig> ConfigureParser()
{
var helpFormatter = new DefaultHelpFormatter<GitConfig>();
static Flag<T> MakeHelpFlag<T>() where T : GitConfig
{
return new Flag<T>()
{
Names = new() { "-h", "--help" },
Description = "Print help",
Action = (config) => { config.Help = true; }
};
}
/// PUSH
var PushParser = new Parser<GitPushConfig>(() => new GitPushConfig())
{
Names = new() {"push"},
Description = "Pushes to a remote repository",
Run = (config, parser) =>
{
if (config.Help)
{
helpFormatter.PrintHelp(parser, Console.Out);
return;
}
Console.WriteLine("Pushing to remote repository");
if (config.Tags) Console.WriteLine("Pushing tags");
if (config.Repository is not null) Console.WriteLine("Pushing to repository: " + config.Repository);
if (config.BranchOrRefspec is not null) Console.WriteLine("Pushing branch or refspec: " + config.BranchOrRefspec);
}
};
PushParser.AddFlag(MakeHelpFlag<GitPushConfig>());
PushParser.AddFlag(new Flag<GitPushConfig>()
{
Names = new() { "-t", "--tags" },
Description = "Pushes tags",
Action = (config) => { config.Tags = true; }
});
PushParser.AddArgument(new Argument<GitPushConfig, string>()
{
ValuePlaceholder = "repository",
Description = "Repository to push to",
Action = (config, value) => { config.Repository = value; },
Converter = ConverterFactory.CreateStringConverter(),
Multiplicity = ZeroOrOne
});
PushParser.AddArgument(new Argument<GitPushConfig, string>()
{
ValuePlaceholder = "branch or refspec",
Description = "Branch or refspec to push",
Action = (config, value) => { config.BranchOrRefspec = value; },
Converter = ConverterFactory.CreateStringConverter(),
Multiplicity = ZeroOrOne
});
/// CLONE
var CloneParser = new Parser<GitCloneConfig>(() => new GitCloneConfig())
{
Names = new() { "clone" },
Description = "Clones a remote repository",
Run = (config, parser) =>
{
if (config.Help)
{
helpFormatter.PrintHelp(parser, Console.Out);
return;
}
Console.WriteLine("Cloning remote repository");
if (config.Repository is not null) Console.WriteLine("Cloning repository: " + config.Repository);
}
};
CloneParser.AddFlag(MakeHelpFlag<GitCloneConfig>());
CloneParser.AddArgument(new Argument<GitCloneConfig, string>()
{
ValuePlaceholder = "repository",
Description = "Repository to clone",
Action = (config, value) => { config.Repository = value; },
Converter = ConverterFactory.CreateStringConverter(),
});
/// LOG
var LogParser = new Parser<GitLogConfig>(() => new GitLogConfig())
{
Names = new() { "log" },
Description = "Shows the commit log",
Run = (config, parser) =>
{
if (config.Help)
{
helpFormatter.PrintHelp(parser, Console.Out);
return;
}
Console.WriteLine("Showing commit log");
if (config.prettyPrint) Console.WriteLine("Pretty printing. Hi, pretty :)");
}
};
LogParser.AddFlag(MakeHelpFlag<GitLogConfig>());
LogParser.AddFlag(new Flag<GitLogConfig>()
{
Names = new() { "-p", "--pretty" },
Description = "Pretty prints the log",
Action = (config) => { config.prettyPrint = true; }
});
/// GIT
var GitParser = new Parser<GitConfig>(() => new GitConfig())
{
Names = new() { "git" },
Description = "A git clone written in C#",
Run = (config, parser) =>
{
if (config.Help)
{
helpFormatter.PrintHelp(parser, Console.Out);
return;
}
Console.WriteLine("Running git");
if (config.Help) Console.WriteLine();
if (config.Version) Console.WriteLine("version XXX");
}
};
GitParser.AddFlag(MakeHelpFlag<GitConfig>());
GitParser.AddFlag(new Flag<GitConfig>()
{
Names = new() { "-v", "--version" },
Description = "Shows the version",
Action = (config) => { config.Version = true; }
});
GitParser.AddSubparser( PushParser);
GitParser.AddSubparser( CloneParser);
GitParser.AddSubparser( LogParser);
return GitParser;
}
}
#endif