Class ArgsBuilder
Helps to build command line arguments to be passed in to executables.
Each method accepts a condition argument to determine whether to add an argument or not,
and returns this instance of the ArgsBuilder for chaining.
public sealed class ArgsBuilder
- Inheritance
-
ArgsBuilder
- Inherited Members
Methods
AddArgument(string, object, bool)
Adds the specified key and value.
public ArgsBuilder AddArgument(string key, object value, bool condition = true)
Parameters
Returns
- ArgsBuilder
This instance.
Examples
var canAddArgument = true;
var args = ArgsBuilder.By("--", "=").AddArgument("arg1", "value1", canAddArgument);
// args: "--arg1=value1".
AddArguments(string, IEnumerable<object>, bool, bool)
Adds the specified list of values with the specified key, repeating key/value pair if multipleTimes is true.
public ArgsBuilder AddArguments(string key, IEnumerable<object> values, bool multipleTimes = false, bool condition = true)
Parameters
keystringThe key.
valuesIEnumerable<object>The values.
multipleTimesboolDetermines whether to add each argument with a separate key.
conditionboolA condition to add.
Returns
- ArgsBuilder
This instance.
Examples
var canAddArguments = true;
var args = ArgsBuilder.By("--", "=", valueSeparator: ",").AddArguments("key", new[] { "111", "222", false, canAddArguments);
var multipleArgs = ArgsBuilder.By("--", "=").AddArguments("key", new[] { "111", "222", true, canAddArguments);
// args: "--key=111,222"
// multipleArgs: "--key=111 --key=222".
AddKey(string, bool)
Adds the specified key.
public ArgsBuilder AddKey(string key, bool condition = true)
Parameters
Returns
- ArgsBuilder
This ArgsBuilder.
Examples
var canAddKey = true;
var args = ArgsBuilder.By("--", "=").AddKey("key1", canAddKey);
// args: "--key1".
AddKeys(IEnumerable<string>, bool)
Adds the specified list of keys.
public ArgsBuilder AddKeys(IEnumerable<string> keys, bool condition = true)
Parameters
keysIEnumerable<string>The keys.
conditionboolA condition to add.
Returns
- ArgsBuilder
This instance.
Examples
var canAddKeys = true;
var args = ArgsBuilder.By("--", "=").AddKeys(new[] { "key1", "key2" }, canAddKeys);
// args: "--key1 --key2".
AddPath(string, bool)
Adds the specified path.
public ArgsBuilder AddPath(string path, bool condition = true)
Parameters
Returns
- ArgsBuilder
This instance.
Examples
var canAddPath = true;
var args = ArgsBuilder.By("--", "=").AddPath("C:\My Folder\my file.txt", canAddPath);
// args: ""C:\My Folder\my file.txt"".
AddPath(string, string, bool)
Adds the specified path with the specified key.
public ArgsBuilder AddPath(string key, string path, bool condition = true)
Parameters
Returns
- ArgsBuilder
This instance.
Examples
var canAddPath = true;
var args = ArgsBuilder.By("--", "=").AddPath("key", "C:\My Folder\my file.txt", canAddPath);
// args: "--key="C:\My Folder\my file.txt"".
AddPaths(IEnumerable<string>, bool)
Adds the specified list of paths.
public ArgsBuilder AddPaths(IEnumerable<string> paths, bool condition = true)
Parameters
pathsIEnumerable<string>The paths.
conditionboolA condition to add.
Returns
- ArgsBuilder
This instance.
Examples
var canAddPaths = true;
var args = ArgsBuilder.By("--", "=").AddPaths(new [] { "C:\my file 1.txt", "C:\my file 2.txt", canAddPaths);
// args: ""C:\my file 1.txt" "C:\my file 2.txt"".
AddPaths(string, IEnumerable<string>, bool, bool)
Adds the specified list of paths with the specified key, repeating key/path pair if multipleTimes is true.
public ArgsBuilder AddPaths(string key, IEnumerable<string> paths, bool multipleTimes = false, bool condition = true)
Parameters
keystringThe key.
pathsIEnumerable<string>The paths.
multipleTimesboolDetermines whether to add each path with separate key.
conditionboolA condition to add.
Returns
- ArgsBuilder
This instance.
Examples
var canAddPaths = true;
var args = ArgsBuilder.By("--", "=", valueSeparator: ",").AddPaths("key", new[] { "C:\my file 1.txt", "C:\my file 2.txt", false, canAddPaths);
var multipleArgs = ArgsBuilder.By("--", "=").AddPaths("key", new[] { "C:\my file 1.txt", "C:\my file 2.txt", true, canAddPaths);
// args: "--key="C:\my file 1.txt","C:\my file 2.txt""
// multipleArgs: "--key="C:\my file 1.txt" --key="C:\my file 2.txt"".
AddValue(object, bool)
Adds the specified value.
public ArgsBuilder AddValue(object value, bool condition = true)
Parameters
Returns
- ArgsBuilder
This instance.
Examples
var canAddValue = true;
var args = ArgsBuilder.By("--", "=").AddValue("12345", canAddValue);
// args: "12345".
AddValues(IEnumerable<object>, bool)
Adds the specified list of values.
public ArgsBuilder AddValues(IEnumerable<object> values, bool condition = true)
Parameters
valuesIEnumerable<object>The values.
conditionboolA condition to add.
Returns
- ArgsBuilder
This instance.
Examples
var canAddValues = true;
var args = ArgsBuilder.By("--", "=").AddValues(new [] { "111", "222", canAddValues);
// args: "111 222".
By(string, string, string, string, string)
Creates a new ArgsBuilder with the specified separators.
public static ArgsBuilder By(string keyPrefix, string keyValueSeparator, string valueSeparator = ";", string pathWrapper = "\"", string argsSeparator = " ")
Parameters
keyPrefixstringPrepends each key.
keyValueSeparatorstringSeparates keys and values.
valueSeparatorstringSeparates multiple values or paths under a single key.
pathWrapperstringWraps paths using
AddPathorAddPathswhen paths have spaces etc.argsSeparatorstringSeparates key/value pairs (arguments).
Returns
- ArgsBuilder
A newly created ArgsBuilder instance.
ToString()
Returns a string that represents this instance.
public override string ToString()
Returns
Operators
implicit operator string(ArgsBuilder)
Performs an implicit conversion from ArgsBuilder to string.
public static implicit operator string(ArgsBuilder builder)
Parameters
builderArgsBuilderThe builder.
Returns
- string
The result of the conversion.