Nuke.Cola
Loading...
Searching...
No Matches
Arguments.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using Nuke.Common;
6using Nuke.Common.Utilities;
7using Nuke.Common.Utilities.Collections;
8
9namespace Nuke.Cola;
10
11/// <summary>
12/// Extension class for dealing with passing arguments from the user through nuke to a tool
13/// </summary>
14public static class Arguments
15{
16 /// <summary>
17 /// Unescape argument input which is passed by the user through a parameter
18 /// </summary>
19 public static string? ProcessArgument(string? arg)
20 {
21 if (string.IsNullOrWhiteSpace(arg)) return arg;
22
23 arg = arg.TrimMatchingDoubleQuotes()
24 .Replace("''", "\"") // sequence for double quotes
25 .Replace("~-", "-"); // sequence for -
26 if (arg[0] == '~')
27 arg = string.Concat("-", arg.AsSpan(1));
28 return arg;
29 }
30
31 /// <summary>
32 /// Unescape multiple argument input which is passed by the user through a parameter
33 /// </summary>
34 public static IEnumerable<string?> AsArguments(this IEnumerable<string>? args)
35 => args?.Select(ProcessArgument) ?? Enumerable.Empty<string?>();
36
37 /// <summary>
38 /// Unescape multiple argument input which is passed by the user through a parameter
39 /// </summary>
40 public static string AppendAsArguments(this IEnumerable<string>? input, bool leadingSpace = true)
41 => (input?.IsEmpty() ?? true)
42 ? ""
43 : (leadingSpace ? " " : "") + string.Join(' ', input?.Select(ProcessArgument) ?? Enumerable.Empty<string>());
44
45 /// <summary>
46 /// Gets an optionally named block of arguments. An argument block starts with "-->" (+ optional
47 /// name) and either ends at the start of another argument block or ends at the last argument.
48 /// <para>
49 /// For example
50 /// </para>
51 /// <code>
52 /// > nuke display-args --param1 foo --> -d foo bar /switch="asdasd"
53 /// args: -d foo bar /switch="asdasd"
54 /// > nuke display-args --param1 foo -->b1 -d foo bar -->b2 /switch="asdasd"
55 /// b1 args: -d foo bar
56 /// b2 args: /switch="asdasd"
57 /// </code>
58 /// </summary>
59 /// <remarks>
60 /// Nuke allows unknown arguments for its reflection system, but it doesn't know it should stop
61 /// processing arguments inside an argument block. If the build has similarly named arguments
62 /// it may interfere with other parameters. Design your build usage with this in mind.
63 /// </remarks>
64 public static IEnumerable<string> GetBlock(string name = "", IEnumerable<string>? from = null)
65 {
66 var args = from ?? EnvironmentInfo.CommandLineArguments;
67 return args
68 .SkipUntil(a => a == "-->" + name)
69 .Skip(1)
70 .TakeUntil(a => a.StartsWith("-->"));
71 }
72
73 /// <summary>
74 /// Gets an optionally named block of arguments. An argument block starts with "-->" (+ optional
75 /// name) and either ends at the start of another argument block or ends at the last argument.
76 /// <para>
77 /// For example
78 /// </para>
79 /// <code>
80 /// > nuke display-args --param1 foo --> -d foo bar /switch="asdasd"
81 /// args: -d foo bar /switch="asdasd"
82 /// > nuke display-args --param1 foo -->b1 -d foo bar -->b2 /switch="asdasd"
83 /// b1 args: -d foo bar
84 /// b2 args: /switch="asdasd"
85 /// </code>
86 /// </summary>
87 /// <remarks>
88 /// Nuke allows unknown arguments for its reflection system, but it doesn't know it should stop
89 /// processing arguments inside an argument block. If the build has similarly named arguments
90 /// it may interfere with other parameters. Design your build usage with this in mind.
91 /// </remarks>
92 public static IEnumerable<string> GetArgumentBlock(this IEnumerable<string> from, string name = "") => GetBlock(name, from);
93}
Extension class for dealing with passing arguments from the user through nuke to a tool.
Definition Arguments.cs:15
static IEnumerable< string > GetBlock(string name="", IEnumerable< string >? from=null)
Gets an optionally named block of arguments. An argument block starts with "-->" (+ optional name) an...
Definition Arguments.cs:64
static IEnumerable< string?> AsArguments(this IEnumerable< string >? args)
Unescape multiple argument input which is passed by the user through a parameter.
static IEnumerable< string > GetArgumentBlock(this IEnumerable< string > from, string name="")
Gets an optionally named block of arguments. An argument block starts with "-->" (+ optional name) an...
static string AppendAsArguments(this IEnumerable< string >? input, bool leadingSpace=true)
Unescape multiple argument input which is passed by the user through a parameter.
static ? string ProcessArgument(string? arg)
Unescape argument input which is passed by the user through a parameter.
Definition Arguments.cs:19