1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Security ;
4
5
using System . Text ;
@@ -62,40 +63,52 @@ public static IEnumerable<string> ToArguments(this string cli)
62
63
/// Joins a collection of arguments into a single command-line string, quoting arguments as needed.
63
64
/// </summary>
64
65
/// <param name="cli">The arguments to join into a command-line string.</param>
66
+ /// <param name="option"></param>
65
67
/// <returns>A single string representing the command-line.</returns>
66
- public static string ToArguments ( this IEnumerable < string > cli )
68
+ public static string ToArguments ( this IEnumerable < string > cli , ArgumentBuilderOption ? option = null )
67
69
{
68
- return string . Join ( " " , cli ? . Select ( arg => arg . ToQuoteMarkArguments ( ) ) ?? [ ] ) ;
70
+ option ??= ArgumentBuilderOption . Default ;
71
+
72
+ return string . Join ( option . Separator , cli ? . Select ( arg => arg . ToArguments ( option ) ) ?? [ ] ) ;
69
73
}
70
74
71
75
/// <summary>
72
76
/// Adds quotes to an argument string if necessary, with options for handling existing quotes.
73
77
/// </summary>
74
78
/// <param name="arg">The argument string to process.</param>
75
- /// <param name="quoteType">Specifies how to handle existing quotes within the argument. </param>
79
+ /// <param name="option"> </param>
76
80
/// <returns>The argument string, possibly with added quotes.</returns>
77
- public static string ToQuoteMarkArguments ( this string arg , QuoteReplace quoteType = QuoteReplace . None )
81
+ public static string ToArguments ( this string arg , ArgumentBuilderOption ? option = null )
78
82
{
79
83
if ( string . IsNullOrEmpty ( arg ) )
80
84
{
81
85
return arg ;
82
86
}
83
87
84
- switch ( quoteType )
88
+ option ??= ArgumentBuilderOption . Default ;
89
+
90
+ string quote = ArgumentBuilderOption . Quote ;
91
+
92
+ switch ( option . QuoteReplace )
85
93
{
86
94
case QuoteReplace . DoubleQuote :
87
- arg = arg . Replace ( "\" " , "\" \" " ) ;
95
+ arg = arg . Replace ( ArgumentBuilderOption . Quote , ArgumentBuilderOption . DoubleQuote ) ;
96
+ quote = ArgumentBuilderOption . DoubleQuote ;
88
97
break ;
89
98
90
99
case QuoteReplace . BackSlashQuote :
91
- arg = arg . Replace ( "\" " , "\\ \" " ) ;
100
+ arg = arg . Replace ( ArgumentBuilderOption . Quote , ArgumentBuilderOption . BackSlashQuote ) ;
101
+ quote = ArgumentBuilderOption . BackSlashQuote ;
92
102
break ;
93
103
}
94
104
95
- if ( ! ( arg . StartsWith ( "\" " ) && arg . EndsWith ( "\" " ) ) // If the argument is not already quoted
96
- && arg . Contains ( ' ' ) || arg . Contains ( '\" ' ) ) // And if the argument contains spaces or quotes
105
+ // If the argument is not already quoted
106
+ // if the argument contains spaces
107
+ // If the argument is a valid URI
108
+ if ( ( ! ( arg . StartsWith ( quote ) && arg . EndsWith ( quote ) ) && arg . Contains ( ' ' ) )
109
+ || ( option . IsQuoteScheme && Uri . TryCreate ( arg , UriKind . Absolute , out _ ) ) )
97
110
{
98
- arg = $ " \" { arg } \" " ;
111
+ arg = quote + arg + quote ;
99
112
}
100
113
return arg ;
101
114
}
@@ -109,18 +122,12 @@ public static (string, IEnumerable<string>) ToFileNameWithArguments(this string
109
122
{
110
123
IEnumerable < string > args = cli . ToArguments ( ) ;
111
124
112
- if ( args . Count ( ) == 0 )
113
- {
114
- return ( cli , [ ] ) ;
115
- }
116
- else if ( args . Count ( ) == 1 )
117
- {
118
- return ( args . First ( ) , [ ] ) ;
119
- }
120
- else
125
+ return args . Count ( ) switch
121
126
{
122
- return ( args . First ( ) , args . Skip ( 1 ) ) ;
123
- }
127
+ 0 => ( cli , [ ] ) ,
128
+ 1 => ( args . First ( ) , [ ] ) ,
129
+ _ => ( args . First ( ) , args . Skip ( 1 ) ) ,
130
+ } ;
124
131
}
125
132
126
133
/// <summary>
@@ -139,25 +146,4 @@ public static SecureString ToSecureString(this string str)
139
146
secureString . MakeReadOnly ( ) ;
140
147
return secureString ;
141
148
}
142
-
143
- /// <summary>
144
- /// Options for handling quotes within argument strings.
145
- /// </summary>
146
- public enum QuoteReplace
147
- {
148
- /// <summary>
149
- /// No special handling for quotes
150
- /// </summary>
151
- None ,
152
-
153
- /// <summary>
154
- /// Replace each quote with two quotes
155
- /// </summary>
156
- DoubleQuote ,
157
-
158
- /// <summary>
159
- /// Escape each quote with a backslash
160
- /// </summary>
161
- BackSlashQuote ,
162
- }
163
149
}
0 commit comments