Skip to content

Commit

Permalink
Read command-line args from the environment
Browse files Browse the repository at this point in the history
  • Loading branch information
calebkiage committed Mar 6, 2024
1 parent 433673e commit 32f0645
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
27 changes: 0 additions & 27 deletions src/Kiota.Builder/Refiners/CliRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,10 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance
RenameDuplicateIndexerNavProperties(generatedCode);
// Must be called after the more specific RenameDuplicateIndexerNavProperties
RenameMatchingSubsequentNavCommands(generatedCode);
AddArgsParameterToClientConstructor(generatedCode);
cancellationToken.ThrowIfCancellationRequested();
CreateCommandBuilders(generatedCode);
}, cancellationToken);
}
private static void AddArgsParameterToClientConstructor(CodeElement currentElement)
{
if (currentElement is CodeClass currentClass && currentClass.Kind is CodeClassKind.RequestBuilder)
{
var clientConstructor = currentClass.UnorderedMethods.SingleOrDefault(static m => m.Kind is CodeMethodKind.ClientConstructor);
if (clientConstructor is { } m)
{
m.AddParameter(new CodeParameter
{
Name = "args",
Optional = false,
Kind = CodeParameterKind.Custom,
Type = new CodeType
{
Name = "string",
IsExternal = true,
IsNullable = false,
CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array
}
});
return;
}
}

CrawlTree(currentElement, AddArgsParameterToClientConstructor);
}
private static void RemoveBackwardCompatibleIndexers(CodeElement currentElement)
{
//TODO remove for v2
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ private void WriteRequestBuilderBody(CodeClass parentClass, CodeMethod codeEleme
}
private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod method, LanguageWriter writer)
{
if (method.Parameters.Any(static p => string.Equals(p.Name, "args", StringComparison.Ordinal))) {
writer.WriteLine("Arguments = args.TakeWhile(static x => !string.IsNullOrEmpty(x) && !x.StartsWith('-')).ToArray();");
if (parentClass.StartBlock?.Inherits?.Name?.Contains("CliRequestBuilder", StringComparison.Ordinal) == true) {
writer.WriteLine("Arguments = Environment.GetCommandLineArgs().Skip(1).TakeWhile(static x => !string.IsNullOrEmpty(x) && !x.StartsWith('-')).ToArray();");
}
if (parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) is not CodeProperty requestAdapterProperty) return;
var pathParametersProperty = parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters);
Expand Down

0 comments on commit 32f0645

Please sign in to comment.