private void checkHelpFlag <T>(string[] argv, CommandLine.ParserResult <T> parser) { // check if a "help" or a "-h" flag is used if (argv.Length > 1 && argv[1] == "help") { this.Usage.PrintHelp(parser, null, argv); } }
static void Main(string[] args) { CommandLine.ParserResult <Options> result = Parser.Default.ParseArguments <Options>(args); if (result.Tag == ParserResultType.Parsed) { var options = (Parsed <Options>)result; string inputFile = Path.GetFullPath(options.Value.InputFile); JToken json; string output; NBT nbt; NBTOnJson jsonToNBT = new NBTOnJson(inputFile); try { //read input json try { using (StreamReader sr = new StreamReader(inputFile)) using (JsonTextReader jr = new JsonTextReader(sr)) { json = JToken.ReadFrom(jr); } } catch (JsonReaderException e) { string errMsg = ""; errMsg += $"Json file error in <{inputFile}>\n"; errMsg += "=========Error details=========\n"; errMsg += $"{e.Message}\n"; errMsg += "==============================="; throw new JsonReaderException(errMsg); } //make nbt JToken nbtJson = json["nbt"]; if (nbtJson != null) { nbt = jsonToNBT.MakeNBT(nbtJson); } else { throw new JsonException("\"nbt\" is undefined."); } if (nbt == null) { nbt = NBTCompund.Empty; } //setting output format output = nbt.ToString(); if (!options.Value.OnlyNbt) { string command = (string)(JValue)json["command"]; if (command != null) { output = command.Replace("$nbt$", output); } else { throw new JsonException("\"command\" is not defined."); } } //setting output file string outputFile; string outputFileJson = (string)(JValue)json["output"]; string outputFileCommandLine = options.Value.OutputFile; if (outputFileJson == null && outputFileCommandLine == null) { //not defined output file Console.WriteLine(output); } else { if (outputFileJson != null && outputFileCommandLine == null) { //defined output file in only json outputFile = RelPath.GetRootedPathByFile(inputFile, outputFileJson); } else { //defined output file commandline outputFile = Path.GetFullPath(options.Value.OutputFile); } using (StreamWriter sw = new StreamWriter(outputFile)) { sw.WriteLine(output); } Console.WriteLine("Write file successful."); } if (options.Value.Clipboard) { Clipboard.SetText(output); Console.WriteLine("Copyed output to clipboard."); } } catch (Exception e) { Console.WriteLine(e.Message); return; } } }