-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (38 loc) · 1.37 KB
/
Copy pathProgram.cs
File metadata and controls
44 lines (38 loc) · 1.37 KB
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
using System.CommandLine;
using ShellSyntaxTree.Cli.Sample.Commands;
// System.CommandLine 2.0.7 (stable) — the 2.0.0-beta5.25558.101 referenced in
// internal notes was never published to nuget.org. The 2.x stable API exposes
// SetAction(parseResult => ...) and parseResult.GetValue(arg) instead of the
// older SetHandler(...) pattern used in pre-beta5 builds.
var commandArg = new Argument<string>("command")
{
Description = "The shell command (or single line) to analyze.",
};
var shellOption = new Option<string>("--shell")
{
Description = "Which parser to use: 'bash' (default) or 'pwsh'.",
DefaultValueFactory = _ => "bash",
};
shellOption.AcceptOnlyFromAmong("bash", "pwsh");
var explainCmd = new Command("explain", "Pretty-print the parsed AST.")
{
commandArg,
shellOption,
};
explainCmd.SetAction(parseResult => ExplainCommand.Run(
parseResult.GetValue(commandArg) ?? string.Empty,
parseResult.GetValue(shellOption) ?? "bash"));
var auditCmd = new Command("audit", "Run the built-in policy against the command.")
{
commandArg,
shellOption,
};
auditCmd.SetAction(parseResult => AuditCommand.Run(
parseResult.GetValue(commandArg) ?? string.Empty,
parseResult.GetValue(shellOption) ?? "bash"));
var root = new RootCommand("ShellSyntaxTree sample CLI — bash & PowerShell")
{
explainCmd,
auditCmd,
};
return root.Parse(args).Invoke();