Provides primitives for command line manipulation.
$ dotnet add package Gapotchenko.FX.Diagnostics.CommandLineThe module provides primitives for command line manipulation.
CommandLineBuilder class allows you to dynamically build a command line.
It provides the built-in support for characters than need escaping.
Semantically CommandLineBuilder is similar to StringBuilder class:
using Gapotchenko.FX.Diagnostics;
var clb = new CommandLineBuilder();
clb.AppendParameter("/b");
clb.AppendFileName(@"C:\Temp\Test 1.txt");
clb.AppendFileName(@"C:\Temp\Test 2.txt");
Console.WriteLine(clb.ToString());
The code above produces the following output:
/b "C:\Temp\Test 1.txt" "C:\Temp\Test 2.txt"
Note how some command-line parameters were automatically quoted because they contained whitespace characters.
CommandLineBuilder supports a fluent interface, just like conventional StringBuilder.
So the code can be rewritten as:
var clb = new CommandLineBuilder()
.AppendParameter("/b")
.AppendFileName(@"C:\Temp\Test 1.txt")
.AppendFileName(@"C:\Temp\Test 2.txt");
Console.WriteLine(clb.ToString());
The resulting command line can be used in various places, most notably for starting a new process:
using System.Diagnostics;
Process.Start("copy", clb.ToString());
CommandLine static class provides operations for command line manipulation.
CommandLine.Build method allows you to quickly build a command-line string from a specified list of arguments.
Basically, this is a shortcut to CommandLineBuilder class in a handy functional form:
string commandLine = CommandLine.Build("/b", @"C:\Temp\Test 1.txt", @"C:\Temp\Test 2.txt");
Such a form is very useful in something like this:
Process.Start(
"cmd",
CommandLine.Build(
"/C", "copy",
"/b", @"C:\Temp\Test 1.txt", @"C:\Temp\Test 2.txt"));
Another cool thing: if you want to exclude some arguments from the command line then you can just make them null:
// 'mode' will have a non-null value if there is a need to specify it.
string? mode = binary ? "/b" : null;
string commandLine = CommandLine.Build(mode, @"C:\Temp\Test 1.txt", @"C:\Temp\Test 2.txt");
Console.WriteLine(commandLine);The code above produces the following outputs depending on the value of binary flag:
"C:\Temp\Test 1.txt" "C:\Temp\Test 2.txt"
/b "C:\Temp\Test 1.txt" "C:\Temp\Test 2.txt"
This is a neat departure from a traditional .NET convention where it always throws ArgumentNullException.
Instead, Gapotchenko.FX uses a slightly different philosophy.
It does the best job possible under existing conditions by following common-sense expectations.
CommandLine.Split provides the inverse operation to CommandLine.Build.
It allows you to split a command-line string into a list of arguments using the rules of a host operating system:
using Gapotchenko.FX.Diagnostics;
string commandLine = "/b \"C:\\Temp\\Test 1.txt\" \"C:\\Temp\\Test 2.txt\"";
foreach (string arg in CommandLine.Split(commandLine))
Console.WriteLine(arg);The code above produces the following output:
/b
C:\Temp\Test 1.txt
C:\Temp\Test 2.txt
Gapotchenko.FX.Diagnostics.CommandLineGapotchenko.FX.Diagnostics.CommandLineBuilderLet's continue with a look at some other modules provided by Gapotchenko.FX:
Or look at the full list of modules.