Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. CommandLineJsonParser
  2. .Create("my app", "it is app")
  3. .AddArg("[paths]", "Raw Data or Config Files")
  4. .Add("-s|--subject <SUBJECT>", "The subject")
  5. .Add("-n|--count <N>", "Repeat")
  6. .Build(json)(new[] { @"config1.json", @"config2.json", @"raw1.raw", @"raw2.raw", "-s", "\"sbj\"", "-n", "3" });
  7. //.Build(json)(new[] { @"-h"});
  8.  
  9.  
  10. public class CommandLineJsonParser
  11. {
  12. List<CommandOption> dict;
  13. CommandArgument args;
  14. CommandLineApplication app;
  15. JObject json;
  16.  
  17. public static CommandLineJsonParser Create(string name, string description)
  18. {
  19.  
  20. var dst = new CommandLineJsonParser();
  21.  
  22. dst.app = new CommandLineApplication();
  23. dst.app.Name = name;
  24. dst.app.Description = description;
  25. dst.app.HelpOption();
  26. dst.dict = new List<CommandOption>();
  27.  
  28. dst.json = new JObject();
  29.  
  30. return dst;
  31. }
  32.  
  33. public CommandLineJsonParser AddArg(string name, string description)
  34. {
  35. args = app.Argument(name, description, multipleValues: true);
  36. return this;
  37. }
  38.  
  39. public CommandLineJsonParser Add(string temp, string description)
  40. {
  41. dict.Add(app.Option(temp, description, CommandOptionType.SingleValue));
  42. return this;
  43. }
  44.  
  45. public Func<string[], int> Build(JObject json)
  46. {
  47. app.OnExecute(() =>
  48. {
  49. foreach (var file in args.Values)
  50. {
  51. //json.Add("Arg", JToken.Parse($"{{\"{key}\" : \"{i.Value}\"}}")));
  52. switch (Path.GetExtension(file).ToLower())
  53. {
  54. case ".json":
  55. case ".yaml":
  56. case ".yml":
  57. Console.WriteLine($"CONFIGFILE : {file}");
  58. json.Merge(
  59. JObject.Parse(File.ReadAllText(file)),
  60. new JsonMergeSettings
  61. {
  62. MergeArrayHandling = MergeArrayHandling.Union
  63. }
  64. );
  65. break;
  66. default:
  67. Console.WriteLine($"DATAFILE : {file}");
  68. json.Merge(
  69. JObject.Parse($"{{ \"FILENAME\" : [\"{file}\"] }}"),
  70. new JsonMergeSettings
  71. {
  72. MergeArrayHandling = MergeArrayHandling.Union
  73. }
  74. );
  75. break;
  76. }
  77. }
  78.  
  79. foreach (var i in dict)
  80. {
  81. switch (i.OptionType)
  82. {
  83. case CommandOptionType.SingleValue:
  84. var val = i.HasValue() ? i.Value() : "null";
  85. Console.WriteLine($"{i.ShortName} , {i.LongName}, {i.ValueName}");
  86.  
  87. json.Merge(
  88. JObject.Parse($"{{ \"{i.ValueName}\" : {val} }}"),
  89. new JsonMergeSettings
  90. {
  91. MergeArrayHandling = MergeArrayHandling.Union
  92. }
  93. );
  94. break;
  95. default:
  96. break;
  97.  
  98. }
  99. }
  100.  
  101. Console.WriteLine(json.ToString());
  102. return 0;
  103. });
  104.  
  105. return (e) => app.Execute(e);
  106. }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement