/// <inheritdoc /> internal CodeGenerationConsoleManager(TextWriter writer, TextWriter errorWriter = null) : base(ConsoleManagerName, writer, DefaultHelpPrototype, DefaultHelpDescription, errorWriter) { DebugMessagesSwitch = Options.AddSwitch("d|debug", "Debug messages should be written."); // We do not need any Clean Switches after all. Instead, we factor Cleaning in terms of an MSBuild Target. VersionSwitch = Options.AddSwitch("v|version", "Shows the application version."); GoogleOrToolsVersionSwitch = Options.AddSwitch("or-tools-version", "Shows the Google.OrTools version."); OutputDirectoryVar = Options.AddVariable <string>("output-directory|out-dir|od" , "The Output Directory where generate code will land."); GeneratedCodeRegistryFileVar = Options.AddVariable <string>("generated-code-registry-file|registry-file|rf" , "The Generated Code Registry File."); // TODO: TBD: may fill in some blanks with expected and required switches, responses, etc. Levels = new ErrorLevelCollection { // TODO: TBD: somehow isolate how much more of an error there might have been, i.e. internally reported exception is sufficient? { MustSpecifyOutputDirectory, () => !VersionSwitch && IsNullOrEmpty(OutputDirectoryVar), () => "Must specify an Output Directory." }, { MustSpecifyRegistryFileName, () => !VersionSwitch && !IsNullOrEmpty(OutputDirectoryVar) && IsNullOrEmpty(GeneratedCodeRegistryFileVar), () => "Must specify a Registry File Name." }, { ErrorGeneratingCode, () => !VersionSwitch && !(IsNullOrEmpty(OutputDirectoryVar) || IsNullOrEmpty(GeneratedCodeRegistryFileVar)) && GenerateCode() == ErrorGeneratingCode, () => "There was an error Generating the Code." }, DefaultErrorLevel }; }
internal ToolConsoleManager(TextWriter writer, TextWriter errorWriter = null) : base($"{typeof(ToolConsoleManager).Namespace}.Tool", writer , errorWriter: errorWriter) { PrivateFactories = new ManagerFactories(this); VersionSwitch = Options.AddSwitch("version", OnVersion, "Shows the version of the tool."); // TODO: TBD: do we need to do a $"nameof(Operation)" ? or would $"{Operation}" be sufficient? Operation = Options.AddVariable <OperationKind?>("operation".MaySpecify() , $"[ {nameof(Clean)} | {nameof(Generate)} ]" + $", specify the operation the tool should perform. Default Operation is {nameof(Generate)}."); /* While, technically, we `MustSpecify´ Project, Output, and so on, only when the * Tool Operation is Generate. However, as an OptionSet, we only `MaySpecify´ the * options. We will further vet the Error Levels independently further in. */ ProjectDirectory = Options.AddVariable <string>("p|project".MaySpecify(), "Project directory absolute path."); OutputDirectory = Options.AddVariable <string>("o|output".MaySpecify(), "Generated source files output directory."); IntermediateAssembliesRegistryFileName = Options.AddVariable <string>("a|assemblies".MaySpecify(), "JSON formatted intermediate assemblies registry file name."); IntermediateGeneratedRegistryFileName = Options.AddVariable <string>("g|generated".MaySpecify(), "JSON formatted intermediate generated registry file name."); SourcePathList = Options.AddVariableList <string>("src|source".MaySpecify(), "Source paths included during compilation."); ReferencePathList = Options.AddVariableList <string>("r|reference".MaySpecify(), "Paths to assemblies being referenced."); PreprocessorSymbolsList = Options.AddVariableList <string>("d|define".MaySpecify(), "Paths to preprocessor symbols."); GeneratorSearchPathList = Options.AddVariableList <string>("s|search".MaySpecify(), "Paths to folders that may contain generator assemblies."); // Which we should be able to unit test this as well, given our approach. ResponseFile = Options.AddVariable <string>("response".MaySpecify(), "Processes argument input from a new line delimited response file."); Levels = new ErrorLevelCollection { // TODO: TBD: and to be fair, we may consider an NConsole version that allows for null message factory... // No message should ever be relayed, this should allow --version to short-circuit the rest. { DefaultErrorLevel, () => VersionSwitch, () => Empty }, // TODO: TBD: might do the same for a Help `--help´ or `--?´ option as for Version `--version´... { 1, () => PrivateOperation == Generate && !SourcePathList.Values.Any(), () => NoSourceFilesSpecified }, { 2, () => IsNullOrEmpty(OutputDirectory.Value), () => OutputDirectoryMustBeSpecified }, { 3, () => ServiceException != null, RenderServiceException }, DefaultErrorLevel }; }
static int Main(string[] args) { try { var directories = new List<string>(); var files = new List<string>(); var programCommand = Command.None; Func<CompositionContractInfo, bool> contractPredicate = c => false; Func<CompositionInfo, PartDefinitionInfo, bool> partPredicate = (ci, p) => false; var verbose = false; var whitelist = new RejectionWhitelist(); var opts = new Options(); opts.Add<string>("dir", @"C:\MyApp\Parts", "Specify directories to search for parts.", d => directories.Add(d)); opts.Add<string>("file", "MyParts.dll", "Specify assemblies to search for parts.", f => files.Add(f)); opts.AddSwitch("verbose", "Print verbose information on each part.", () => verbose = true); var programCommandGroup = new ExclusiveGroup(); opts.Add<string>("type", "MyNamespace.MyType", "Print details of the given part type.", t => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p == ci.GetPartDefinitionInfo(t)); }, programCommandGroup); opts.Add<string>("importers", "MyContract", "List importers of the given contract.", i => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.ImportsContract(i)); }, programCommandGroup); opts.Add<string>("exporters", "MyContract", "List exporters of the given contract.", e => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.ExportsContract(e)); }, programCommandGroup); opts.AddSwitch("rejected", "List all rejected parts.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.IsRejected); }, programCommandGroup); opts.AddSwitch("causes", "List root causes - parts with errors not related to the rejection of other parts.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.IsPrimaryRejection); }, programCommandGroup); opts.Add<string>("whitelist", "RejectionWhitelist.txt", "Specify parts that may be validly rejected; requres the /rejected or /causes commands.", w => whitelist = new RejectionWhitelist(w)); opts.AddSwitch("parts", "List all parts found in the source assemblies.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => true); }, programCommandGroup); opts.AddSwitch("?", "Print usage.", () => programCommand = Command.PrintUsage, programCommandGroup); var contractsSubgroup = new InclusiveSubroup(programCommandGroup); opts.AddSwitch("imports", "Find imported contracts.", () => { programCommand = Command.PrintContracts; contractPredicate = AddToPredicate(contractPredicate, c => c.Importers.Any()); }, contractsSubgroup); opts.AddSwitch("exports", "Find exported contracts.", () => { programCommand = Command.PrintContracts; contractPredicate = AddToPredicate(contractPredicate, c => c.Exporters.Any()); }, contractsSubgroup); opts.Parse(args); return Run(directories, files, programCommand, contractPredicate, partPredicate, verbose, whitelist, opts); } catch (Exception ex) { Console.Write("Error: "); Console.WriteLine(ex.Message); return 1; } }
static int Main(string[] args) { try { var directories = new List <string>(); var files = new List <string>(); var programCommand = Command.None; Func <CompositionContractInfo, bool> contractPredicate = c => false; Func <CompositionInfo, PartDefinitionInfo, bool> partPredicate = (ci, p) => false; var verbose = false; var whitelist = new RejectionWhitelist(); var opts = new Options(); opts.Add <string>("dir", @"C:\MyApp\Parts", "Specify directories to search for parts.", d => directories.Add(d)); opts.Add <string>("file", "MyParts.dll", "Specify assemblies to search for parts.", f => files.Add(f)); opts.AddSwitch("verbose", "Print verbose information on each part.", () => verbose = true); var programCommandGroup = new ExclusiveGroup(); opts.Add <string>("type", "MyNamespace.MyType", "Print details of the given part type.", t => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p == ci.GetPartDefinitionInfo(t)); }, programCommandGroup); opts.Add <string>("importers", "MyContract", "List importers of the given contract.", i => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.ImportsContract(i)); }, programCommandGroup); opts.Add <string>("exporters", "MyContract", "List exporters of the given contract.", e => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.ExportsContract(e)); }, programCommandGroup); opts.AddSwitch("rejected", "List all rejected parts.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.IsRejected); }, programCommandGroup); opts.AddSwitch("causes", "List root causes - parts with errors not related to the rejection of other parts.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => p.IsPrimaryRejection); }, programCommandGroup); opts.Add <string>("whitelist", "RejectionWhitelist.txt", "Specify parts that may be validly rejected; requres the /rejected or /causes commands.", w => whitelist = new RejectionWhitelist(w)); opts.AddSwitch("parts", "List all parts found in the source assemblies.", () => { programCommand = Command.PrintParts; partPredicate = AddToPredicate(partPredicate, (ci, p) => true); }, programCommandGroup); opts.AddSwitch("?", "Print usage.", () => programCommand = Command.PrintUsage, programCommandGroup); var contractsSubgroup = new InclusiveSubroup(programCommandGroup); opts.AddSwitch("imports", "Find imported contracts.", () => { programCommand = Command.PrintContracts; contractPredicate = AddToPredicate(contractPredicate, c => c.Importers.Any()); }, contractsSubgroup); opts.AddSwitch("exports", "Find exported contracts.", () => { programCommand = Command.PrintContracts; contractPredicate = AddToPredicate(contractPredicate, c => c.Exporters.Any()); }, contractsSubgroup); opts.Parse(args); return(Run(directories, files, programCommand, contractPredicate, partPredicate, verbose, whitelist, opts)); } catch (Exception ex) { Console.Write("Error: "); Console.WriteLine(ex.Message); return(1); } }