Skip to content

Commit da2db78

Browse files
list options of cli tool (#469)
1 parent e689e74 commit da2db78

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

Diff for: src/Runner.Client/Program.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using GitHub.Services.WebApi;
2727
using System.CommandLine.Builder;
2828
using System.CommandLine.Binding;
29+
using System.CommandLine.Parsing;
2930
using System.Security.Cryptography;
3031
using System.Security.Cryptography.X509Certificates;
3132
using GitHub.Runner.Common;
@@ -169,6 +170,7 @@ private class Parameters {
169170
public string[] LocalRepositories { get; set; }
170171
public bool Trace { get; set; }
171172
public bool Json { get; set; }
173+
public bool ListOptions { get; set; }
172174
public Parameters ShallowCopy()
173175
{
174176
return (Parameters) this.MemberwiseClone();
@@ -1006,7 +1008,7 @@ static int Main(string[] args)
10061008
var verboseOpt = new Option<bool>(
10071009
new[] {"-v", "--verbose"},
10081010
"Print more details like server / runner logs to stdout");
1009-
var parallelOpt = new Option<int?>(
1011+
var parallelOpt = new Option<int>(
10101012
"--parallel",
10111013
description: "Run n parallel runners");
10121014
var noCopyGitDirOpt = new Option<bool>(
@@ -1080,6 +1082,9 @@ static int Main(string[] args)
10801082
var localrepositoriesOpt = new Option<string[]>(
10811083
"--local-repository",
10821084
description: "Redirect dependent repositories to the local filesystem. E.g `--local-repository org/name@ref=/path/to/repository`");
1085+
var listOptionsOpt = new Option<bool>(
1086+
"--list-options",
1087+
description: "Print a json structure of compatible options");
10831088
var rootCommand = new RootCommand
10841089
{
10851090
workflowOption,
@@ -1139,6 +1144,7 @@ static int Main(string[] args)
11391144
shaOpt,
11401145
refOpt,
11411146
localrepositoriesOpt,
1147+
listOptionsOpt,
11421148
};
11431149

11441150
rootCommand.Description = "Run your workflows locally.";
@@ -1178,6 +1184,15 @@ static int Main(string[] args)
11781184
// Note that the parameters of the handler method are matched according to the names of the options
11791185
Func<Parameters, Task<int>> handler = async (parameters) =>
11801186
{
1187+
if(parameters.ListOptions) {
1188+
var options = new JArray();
1189+
foreach(var opt in rootCommand.Options) {
1190+
options.Add(JObject.FromObject(new { name = opt.Name, aliases = opt.Aliases, required = opt.IsRequired, @default = (opt as IValueDescriptor).HasDefaultValue ? (opt as IValueDescriptor).GetDefaultValue() : null, description = opt.Description, @type = TypeHelper.GetTypeName(opt.ValueType), minValues = opt.Arity.MinimumNumberOfValues, maxValues = opt.Arity.MaximumNumberOfValues }));
1191+
}
1192+
Console.WriteLine(options);
1193+
return 0;
1194+
}
1195+
11811196
var expandAzurePipeline = string.Equals(parameters.Event, "azexpand", StringComparison.OrdinalIgnoreCase);
11821197
if(parameters.Parallel == null && !parameters.StartServer && !parameters.List && !expandAzurePipeline) {
11831198
parameters.Parallel = 1;
@@ -2538,6 +2553,7 @@ await Task.WhenAny(Task.Run(() => {
25382553
parameters.Interactive = bindingContext.ParseResult.GetValueForOption(interactiveOpt);
25392554
parameters.Trace = bindingContext.ParseResult.GetValueForOption(traceOpt);
25402555
parameters.Json = bindingContext.ParseResult.GetValueForOption(jsonOpt);
2556+
parameters.ListOptions = bindingContext.ParseResult.GetValueForOption(listOptionsOpt);
25412557
parameters.Quiet = bindingContext.ParseResult.GetValueForOption(quietOpt);
25422558
parameters.Privileged = bindingContext.ParseResult.GetValueForOption(privilegedOpt);
25432559
parameters.Userns = bindingContext.ParseResult.GetValueForOption(usernsOpt);
@@ -2546,7 +2562,7 @@ await Task.WhenAny(Task.Run(() => {
25462562
parameters.KeepContainer = bindingContext.ParseResult.GetValueForOption(keepContainerOpt);
25472563
parameters.Directory = bindingContext.ParseResult.GetValueForOption(DirectoryOpt);
25482564
parameters.Verbose = bindingContext.ParseResult.GetValueForOption(verboseOpt);
2549-
parameters.Parallel = bindingContext.ParseResult.GetValueForOption(parallelOpt);
2565+
parameters.Parallel = bindingContext.ParseResult.HasOption(parallelOpt) ? bindingContext.ParseResult.GetValueForOption<int>(parallelOpt) : null;
25502566
parameters.NoCopyGitDir = bindingContext.ParseResult.GetValueForOption(noCopyGitDirOpt);
25512567
parameters.KeepRunnerDirectory = bindingContext.ParseResult.GetValueForOption(keepRunnerDirectoryOpt);
25522568
parameters.RunnerDirectory = bindingContext.ParseResult.GetValueForOption(runnerDirectoryOpt);

Diff for: src/Runner.Client/TypeHelper.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.IO.Compression;
5+
using System.Net;
6+
using System.Net.Http;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using GitHub.Runner.Common;
10+
using GitHub.Runner.Sdk;
11+
using System.Net.Mime;
12+
using System.Linq;
13+
using GitHub.DistributedTask.ObjectTemplating;
14+
using GitHub.Runner.Worker;
15+
16+
namespace Runner.Client
17+
{
18+
public class TypeHelper {
19+
public static string GetTypeName(System.Type t) {
20+
if(t == typeof(bool)){
21+
return "bool";
22+
} else if(t == typeof(string)) {
23+
return "string";
24+
} else if(t == typeof(string[])) {
25+
return "stringArray";
26+
} else if(t == typeof(int)) {
27+
return "int";
28+
}
29+
throw new InvalidOperationException(t.Name);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)