Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CommandLineJsonParser
- .Create("my app", "it is app")
- .AddArg("[paths]", "Raw Data or Config Files")
- .Add("-s|--subject <SUBJECT>", "The subject")
- .Add("-n|--count <N>", "Repeat")
- .Build(json)(new[] { @"config1.json", @"config2.json", @"raw1.raw", @"raw2.raw", "-s", "\"sbj\"", "-n", "3" });
- //.Build(json)(new[] { @"-h"});
- public class CommandLineJsonParser
- {
- List<CommandOption> dict;
- CommandArgument args;
- CommandLineApplication app;
- JObject json;
- public static CommandLineJsonParser Create(string name, string description)
- {
- var dst = new CommandLineJsonParser();
- dst.app = new CommandLineApplication();
- dst.app.Name = name;
- dst.app.Description = description;
- dst.app.HelpOption();
- dst.dict = new List<CommandOption>();
- dst.json = new JObject();
- return dst;
- }
- public CommandLineJsonParser AddArg(string name, string description)
- {
- args = app.Argument(name, description, multipleValues: true);
- return this;
- }
- public CommandLineJsonParser Add(string temp, string description)
- {
- dict.Add(app.Option(temp, description, CommandOptionType.SingleValue));
- return this;
- }
- public Func<string[], int> Build(JObject json)
- {
- app.OnExecute(() =>
- {
- foreach (var file in args.Values)
- {
- //json.Add("Arg", JToken.Parse($"{{\"{key}\" : \"{i.Value}\"}}")));
- switch (Path.GetExtension(file).ToLower())
- {
- case ".json":
- case ".yaml":
- case ".yml":
- Console.WriteLine($"CONFIGFILE : {file}");
- json.Merge(
- JObject.Parse(File.ReadAllText(file)),
- new JsonMergeSettings
- {
- MergeArrayHandling = MergeArrayHandling.Union
- }
- );
- break;
- default:
- Console.WriteLine($"DATAFILE : {file}");
- json.Merge(
- JObject.Parse($"{{ \"FILENAME\" : [\"{file}\"] }}"),
- new JsonMergeSettings
- {
- MergeArrayHandling = MergeArrayHandling.Union
- }
- );
- break;
- }
- }
- foreach (var i in dict)
- {
- switch (i.OptionType)
- {
- case CommandOptionType.SingleValue:
- var val = i.HasValue() ? i.Value() : "null";
- Console.WriteLine($"{i.ShortName} , {i.LongName}, {i.ValueName}");
- json.Merge(
- JObject.Parse($"{{ \"{i.ValueName}\" : {val} }}"),
- new JsonMergeSettings
- {
- MergeArrayHandling = MergeArrayHandling.Union
- }
- );
- break;
- default:
- break;
- }
- }
- Console.WriteLine(json.ToString());
- return 0;
- });
- return (e) => app.Execute(e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement