Provides a simple and easy API to pass JSON command line arguments. and yes, it can parse arguments which are generated like so too!
JsonCommandLine library works little bit differently compared to other CommandLine parsers.
Every argument must have a base Command
keyword. It can have multiple parameters.
Command keyword is used to identify the argument and the parameters are used to pass values with reference to command keyword.
Parameters
are stored in Dictionary<string,string>()
while BaseArgument
is a single string
static void Main(string[] args) {
// parsing args
using (CommandLineParser parser = new CommandLineParser(Environment.CommandLine)) {
// check if this args contain values are generated by JsonCommandLine
// if its not, we can't parse it
if (!parser.IsJsonType) {
return;
}
// parses the arguments
var arguments = parser.Parse();
if (arguments.ArgumentsExist) {
foreach(var arg in arguments.ArgumentCollection) {
// the base command
Console.WriteLine(arg.BaseCommand);
foreach(var param in arg.Parameters) {
// parameters of the above base command
Console.WriteLine($"{param.Key} -> {param.Value}");
}
}
}
}
// generating args
// initiate an argument with base command
CommandLineArgument argument = new CommandLineArgument("update");
// add parameters to the base command
argument.TryAddParameter("enableTrace", "false");
argument.TryAddParameter("generateBackup", "true");
// generate builder to chain multiple arguments into single instance
argument.GenerateArgumentBuilder()
// add another argument
.Add(new CommandLineArgument("restart", new Dictionary<string, string>() {
{"path", "foo/bar/word.exe" }
}))
// maybe add another ?
.Add(new CommandLineArgument("shutdownTimed", new Dictionary<string, string>() {
{"delaySeconds", "10" }
}))
// either build as Arguments object which contains all chained command line arguments
// .BuildAsArgument();
// or
// as resultant command line argument string which will be passed to the next process as args.
// .Build();
.Build()
// we call AsArgument() on Arguments object to turn the chained arguments into resultant args string
// above BuildAsArgument() function does the same, just that it directly converts it to string args.
.AsArgument();
}
- Dependencies
- Newtonsoft.Json (>= 12.0.3)