static string ParseArgs(string[] args) { CMDLineParser parser = new CMDLineParser(); CMDLineParser.Option typeOpt = parser.AddStringParameter("--type", "can bet realty_all, country, default is realty_all", false); try { //parse the command line parser.Parse(args); } catch (Exception ex) { //show available options Console.Write(parser.HelpMessage()); Console.WriteLine(); Console.WriteLine("Error: " + ex.Message); throw; } if (typeOpt.isMatched) { ParseType = typeOpt.Value.ToString(); } var freeArgs = parser.RemainingArgs(); return(String.Join(" ", freeArgs).Trim(new char[] { '"' })); }
static string ParseArgs(string[] args) { CMDLineParser parser = new CMDLineParser(); CMDLineParser.Option outputOpt = parser.AddStringParameter("-o", "use file for output", false); CMDLineParser.Option licenseOpt = parser.AddStringParameter("-license", "", false); CMDLineParser.Option mainLogOpt = parser.AddStringParameter("-log", "", false); CMDLineParser.Option skipLoggingOpt = parser.AddBoolSwitch("-skip-logging", ""); CMDLineParser.Option verboseOpt = parser.AddStringParameter("-v", "verbose level: debug, info, error", false); CMDLineParser.Option columnsOnlyOpt = parser.AddBoolSwitch("-columnsonly", ""); CMDLineParser.Option checkJsonOpt = parser.AddBoolSwitch("-checkjson", ""); CMDLineParser.Option adapterOpt = parser.AddStringParameter("-adapter", "can be aspose,npoi, microsoft or prod, by default is aspose", false); CMDLineParser.Option maxRowsToProcessOpt = parser.AddStringParameter("-max-rows", "max rows to process from the input file", false); CMDLineParser.Option dumpColumnOpt = parser.AddStringParameter("-dump-column", "dump column identified by enum DeclarationField and exit", false); CMDLineParser.Option dumpHtmlOpt = parser.AddStringParameter("-dump-html", "dump table to html", false); CMDLineParser.Option tolokaFileNameOpt = parser.AddStringParameter("-toloka", "generate toloka html", false); CMDLineParser.Option skipRelativeOrphanOpt = parser.AddBoolSwitch("-skip-relative-orphan", ""); CMDLineParser.Option apiValidationOpt = parser.AddBoolSwitch("-api-validation", "validate JSON output by API call"); CMDLineParser.Option buildTrigramsOpt = parser.AddBoolSwitch("-build-trigrams", "build trigrams"); CMDLineParser.Option checkPredictorOpt = parser.AddBoolSwitch("-check-predictor", "calc predictor precision"); CMDLineParser.Option docFileIdOpt = parser.AddStringParameter("-docfile-id", "document id to initialize document/documentfile_id", false); CMDLineParser.Option convertedFileStorageUrlOpt = parser.AddStringParameter("-converted-storage-url", "document id to initialize document/documentfile_id for example http://disclosures.ru:8091, the default value is read from env variable DECLARATOR_CONV_URL", false); CMDLineParser.Option fioOnlyOpt = parser.AddBoolSwitch("-fio-only", ""); CMDLineParser.Option useDecimalRawNormalizationOpt = parser.AddBoolSwitch("-decimal-raw-normalization", "print raw floats in Russian traditional format"); CMDLineParser.Option disclosuresOpt = parser.AddBoolSwitch("-disclosures", "use disclosures output format: save sheet id to each each section, do not produce many output files but one"); CMDLineParser.Option versionOpt = parser.AddBoolSwitch("-version", "print version"); parser.AddHelpOption(); try { //parse the command line parser.Parse(args); } catch (Exception ex) { //show available options Console.Write(parser.HelpMessage()); Console.WriteLine(); Console.WriteLine("Error: " + ex.Message); throw; } if (versionOpt.isMatched) { PrintVersion(); System.Environment.Exit(0); } if (licenseOpt.isMatched) { AsposeLicense.SetLicense(licenseOpt.Value.ToString()); if (!AsposeLicense.Licensed) { throw new SmartParserException("Not valid aspose licence " + licenseOpt.Value.ToString()); } } Smart.Parser.Lib.Parser.InitializeSmartParser(); if (maxRowsToProcessOpt.isMatched) { MaxRowsToProcess = System.Convert.ToInt32(maxRowsToProcessOpt.Value.ToString()); } if (docFileIdOpt.isMatched) { UserDocumentFileId = System.Convert.ToInt32(docFileIdOpt.Value.ToString()); } if (disclosuresOpt.isMatched) { DeclarationSerializer.SmartParserJsonFormat = SmartParserJsonFormatEnum.Disclosures; } string logFileName = ""; if (mainLogOpt.isMatched) { logFileName = Path.GetFullPath(mainLogOpt.Value.ToString()); } Logger.Setup(logFileName, skipLoggingOpt.isMatched); if (outputOpt.isMatched) { OutFile = outputOpt.Value.ToString(); } Logger.LogLevel verboseLevel = Logger.LogLevel.Info; if (verboseOpt.isMatched) { switch (verboseOpt.Value.ToString()) { case "info": verboseLevel = Logger.LogLevel.Info; break; case "error": verboseLevel = Logger.LogLevel.Error; break; case "debug": verboseLevel = Logger.LogLevel.Debug; break; default: { throw new Exception("unknown verbose level " + verboseOpt.Value.ToString()); } } } Logger.SetLoggingLevel(verboseLevel); SkipRelativeOrphan = skipRelativeOrphanOpt.isMatched; ValidateByApi = apiValidationOpt.isMatched; if (adapterOpt.isMatched) { AdapterFamily = adapterOpt.Value.ToString(); if (AdapterFamily != "aspose" && AdapterFamily != "npoi" && AdapterFamily != "microsoft" && AdapterFamily != "prod") { throw new Exception("unknown adapter family " + AdapterFamily); } } if (dumpColumnOpt.isMatched) { ColumnToDump = (DeclarationField)Enum.Parse(typeof(DeclarationField), dumpColumnOpt.Value.ToString()); } if (dumpHtmlOpt.isMatched) { HtmlFileName = dumpHtmlOpt.Value.ToString(); } if (convertedFileStorageUrlOpt.isMatched) { IAdapter.ConvertedFileStorageUrl = convertedFileStorageUrlOpt.Value.ToString(); } if (tolokaFileNameOpt.isMatched) { TolokaFileName = tolokaFileNameOpt.Value.ToString(); } if (useDecimalRawNormalizationOpt.isMatched) { Smart.Parser.Lib.Parser.UseDecimalRawNormalization = true; } ColumnsOnly = columnsOnlyOpt.isMatched; ColumnOrdering.SearchForFioColumnOnly = fioOnlyOpt.isMatched; CheckJson = checkJsonOpt.isMatched; BuildTrigrams = buildTrigramsOpt.isMatched; ColumnPredictor.CalcPrecision = checkPredictorOpt.isMatched; var freeArgs = parser.RemainingArgs(); return(String.Join(" ", freeArgs).Trim(new char[] { '"' })); }
public WorkInstruction(string[] CommandlineArguments) { //Set default values this.Execute = false; this.GenerateReport = false; this.DestinationFilepath = ""; this.DestinationFormat = OutputFormatEnum.Unknown; CMDLineParser parser = new CMDLineParser(); parser.throwInvalidOptionsException = true; //Add -Help option CMDLineParser.Option HelpOption = parser.AddBoolSwitch("-Help", "Displays help"); HelpOption.AddAlias("/?"); //Add -Run option CMDLineParser.Option RunOption = parser.AddBoolSwitch("-Run", "Required. Execute all files in compilation path"); //Add -Path parameter CompilationPathOption PathParameter = new CompilationPathOption("-Path", "Compilation path to load scripts from", false); parser.AddOption(PathParameter); //Add -Format parameter ReportFormatOption FormatParameter = new ReportFormatOption("-Format", "Format of the report that should be generated (HTML, XML, JSON)", false); parser.AddOption(FormatParameter); //Add -Filename parameter FilenameOption FilenameParameter = new FilenameOption("-Filename", "Filename of the generated report", false); parser.AddOption(FilenameParameter); //Add -Text parameter CMDLineParser.Option TextParameter = parser.AddStringParameter("-Text", "Additional text to be included in generated report", false); //FilenameParameter.AddAlias("/Text"); bool commandLineParsed = false; try { parser.Parse(CommandlineArguments); commandLineParsed = true; } catch (CMDLineParser.CMDLineParserException ex) { //That didn't worked... Console.WriteLine(ex.Message); Console.WriteLine("Use /? for help"); Console.WriteLine(); } if (commandLineParsed) { if (HelpOption.isMatched) { Console.WriteLine(parser.HelpMessage()); } else { if (RunOption.isMatched == false) { //No -Run command, nothing to do. Console.WriteLine("Missing -RUN option"); Console.WriteLine(parser.HelpMessage()); } else { this.Execute = true; //Check for PATH parameter is set and use default path if not this.CompilationPath = OptionIsMatchedAndNotEmpty(PathParameter) ? PathParameter.Value.ToString() : Xteq5UIConstant.DefaultCompilationFolder; //Check for FILENAME parameter if we should generate a report. Only if this is set, check the additonal parameters for the report if (OptionIsMatchedAndNotEmpty(FilenameParameter)) { this.GenerateReport = true; this.DestinationFilepath = FilenameParameter.Value.ToString(); //Check for the FORMAT parameter and use HTML if not set string reportFormatString = OptionIsMatchedAndNotEmpty(FormatParameter) ? FormatParameter.Value.ToString() : "HTML"; //This direct cast without any error checking is OK because FORMATPARAMETER already tried to parse it and will only be set if the value is OK this.DestinationFormat = OutputFormatConverter.ParseReportFormat(reportFormatString); this.UserText = OptionIsMatchedAndNotEmpty(TextParameter) ? TextParameter.Value.ToString() : ""; } //All done! } } } }