public void OnExecute(Func <Task <int> > func) { Application.OnExecute(() => { DebugAttach(); return(func()); }); }
static void Main(string[] args) { var app = new CLU.CommandLineApplication(); app.Name = "AppendDividerToFiles"; app.Description = ".Net Core console app. Appends a divider string to all files in a given directory."; var targetFolderArgument = app.Argument("TargetFolder", "The folder containing the files you wish to target"); var dividerOption = app.Option("-d|--divider", "The divider to append.", CLU.CommandOptionType.SingleValue); app.HelpOption("-?|-h|--help"); app.OnExecute(() => { int result = 0; if (string.IsNullOrEmpty(targetFolderArgument.Value)) { Console.WriteLine("Error: A target folder must be specified!"); result = 1; } else if (!Directory.Exists(targetFolderArgument.Value)) { Console.WriteLine("Error: Target folder not found!"); result = 1; } else { string divider = (dividerOption.HasValue()) ? dividerOption.Value() : DEFAULT_DIVIDER; DoWork(targetFolderArgument.Value, divider); } return result; }); var result = app.Execute(args); }
static void Main(string[] args) { var app = new CLU.CommandLineApplication(); app.Name = "CreateHotfixFromMacroDeveloperToolOutput"; app.Description = ".Net Core console app. Reads a Protocol Sync SQL file containing output from the Macro Developer Tool split by a divider string, reorganises the SQL and outputs to a specified file."; var targetFileArgument = app.Argument("TargetFile", "The path and filename of the file containing the Macro Developer Tool output"); var outputFileArgument = app.Argument("OutputFile", "The path and filename of the file where output will be written to"); var dividerArgument = app.Argument("Divider", "String used to divide discrete blocks of SQL"); var overwriteOption = app.Option("-o|--overwrite", "Flag indicating that existing file matching the output file should be overwritten", CLU.CommandOptionType.NoValue); app.HelpOption("-?|-h|--help"); app.OnExecute(() => { int result = 0; if (string.IsNullOrEmpty(targetFileArgument.Value)) { Console.WriteLine("Error: A target file must be specified!"); result = 1; } else if (!File.Exists(targetFileArgument.Value)) { Console.WriteLine("Error: Target file not found!"); result = 1; } else if (string.IsNullOrEmpty(outputFileArgument.Value)) { Console.WriteLine("Error: An output file must be specified!"); result = 1; } else if (File.Exists(outputFileArgument.Value) && !overwriteOption.HasValue()) { Console.WriteLine("Error: File matching output file argument already exists and overwrite option has not been specified!"); result = 1; } else if (string.IsNullOrEmpty(dividerArgument.Value)) { Console.WriteLine("Error: The divider string must be specified!"); result = 1; } else { DoWork(targetFileArgument.Value, outputFileArgument.Value, dividerArgument.Value); } return(result); }); var result = app.Execute(args); }
static void Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication { Name = "VSS C# Project Tool", Description = "A tool to help with parsing a collection of C# projects, and filtering based on the contents." }; app.HelpOption("-?|-h|--help"); var directories = app.Option("-d|--directory", "Directories to search (allows multiple)", CommandOptionType.MultipleValue); var isRecursive = app.Option("-r|--recursive", "Recursive search, defaults to false", CommandOptionType.NoValue); app.OnExecute(() => { app.ShowHelp(); return(1); }); TagsCommand(app, directories, isRecursive); ReferencesCommand(app, directories, isRecursive); app.Execute(args); }
static int Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue); var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue); bool certExists() { return(File.Exists(cert.Value())); } bool coreExists() { return(File.Exists(coreDll.Value())); } bool verifyTopOptions() { return(!string.IsNullOrWhiteSpace(cert.Value()) && !string.IsNullOrWhiteSpace(coreDll.Value()) && certExists() && coreExists()); } app.Command("interactive", config => { string buff = "", licensetype = "", name = "", email = "", businessname = ""; bool valid_guid = false, valid_installid = false; Guid guid = new Guid(), installid = new Guid(); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } WriteLine("Interactive license mode..."); while (licensetype == "") { WriteLine("What would you like to generate, a [u]ser license or an [o]rg license?"); buff = Console.ReadLine(); if (buff == "u") { licensetype = "user"; WriteLineOver("Okay, we will generate a user license."); while (valid_guid == false) { WriteLine("Please provide the user's guid — refer to the Readme for details on how to retrieve this. [GUID]:"); buff = Console.ReadLine(); if (Guid.TryParse(buff, out guid)) { valid_guid = true; } else { WriteLineOver("The user-guid provided does not appear to be valid."); } } } else if (buff == "o") { licensetype = "org"; WriteLineOver("Okay, we will generate an organization license."); while (valid_installid == false) { WriteLine("Please provide your Bitwarden Install-ID — refer to the Readme for details on how to retrieve this. [Install-ID]:"); buff = Console.ReadLine(); if (Guid.TryParse(buff, out installid)) { valid_installid = true; } else { WriteLineOver("The install-id provided does not appear to be valid."); } } while (businessname == "") { WriteLineOver("Please enter a business name, default is BitBetter. [Business Name]:"); buff = Console.ReadLine(); if (buff == "") { businessname = "BitBetter"; } else if (checkBusinessName(buff)) { businessname = buff; } } } else { WriteLineOver("Unrecognized option \'" + buff + "\'. "); } } while (name == "") { WriteLineOver("Please provide the username this license will be registered to. [username]:"); buff = Console.ReadLine(); if (checkUsername(buff)) { name = buff; } } while (email == "") { WriteLineOver("Please provide the email address for the user " + name + ". [email]"); buff = Console.ReadLine(); if (checkEmail(buff)) { email = buff; } } if (licensetype == "user") { WriteLineOver("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", User-GUID: \"" + guid + "\"? Y/n"); buff = Console.ReadLine(); if (buff == "" || buff == "y" || buff == "Y") { GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, guid, null); } else { WriteLineOver("Exiting..."); return(0); } } else if (licensetype == "org") { WriteLineOver("Confirm creation of \"organization\" license for business name: \"" + businessname + "\", username: \"" + name + "\", email: \"" + email + "\", Install-ID: \"" + installid + "\"? Y/n"); buff = Console.ReadLine(); if (buff == "" || buff == "y" || buff == "Y") { GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, installid, businessname, null); } else { WriteLineOver("Exiting..."); return(0); } } return(0); }); }); app.Command("user", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var userIdArg = config.Argument("User ID", "your user id"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.ShowHelp("user"); return(1); } if (string.IsNullOrWhiteSpace(userIdArg.Value) || !Guid.TryParse(userIdArg.Value, out Guid userId)) { config.Error.WriteLine($"User ID not provided"); config.ShowHelp("user"); return(1); } GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, userId, key.Value); return(0); }); }); app.Command("org", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var installId = config.Argument("InstallId", "your installation id (GUID)"); var businessName = config.Argument("BusinessName", "name For the organization (optional)"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value) || string.IsNullOrWhiteSpace(installId.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.ShowHelp("org"); return(1); } if (!Guid.TryParse(installId.Value, out Guid installationId)) { config.Error.WriteLine("Unable to parse your installation id as a GUID"); config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}"); config.ShowHelp("org"); return(1); } GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, businessName.Value, key.Value); return(0); }); }); app.OnExecute(() => { app.ShowHelp(); return(10); }); app.HelpOption("-? | -h | --help"); try { return(app.Execute(args)); } catch (Exception e) { Console.Error.WriteLine("Oops: {0}", e); return(100); } }
static int Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue); var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue); bool certExists() { return(File.Exists(cert.Value())); } bool coreExists() { return(File.Exists(coreDll.Value())); } bool verifyTopOptions() { return(!string.IsNullOrWhiteSpace(cert.Value()) && !string.IsNullOrWhiteSpace(coreDll.Value()) && certExists() && coreExists()); } app.Command("user", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var userIdArg = config.Argument("User ID", "your user id"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.ShowHelp("user"); return(1); } if (string.IsNullOrWhiteSpace(userIdArg.Value) || !Guid.TryParse(userIdArg.Value, out Guid userId)) { config.Error.WriteLine($"User ID not provided"); config.ShowHelp("user"); return(1); } GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, userId, key.Value); return(0); }); }); app.Command("org", config => { var name = config.Argument("Name", "your name"); var email = config.Argument("Email", "your email"); var installId = config.Argument("InstallId", "your installation id (GUID)"); var key = config.Argument("Key", "your key id (optional)"); var help = config.HelpOption("--help | -h | -?"); config.OnExecute(() => { if (!verifyTopOptions()) { if (!coreExists()) { config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}"); } if (!certExists()) { config.Error.WriteLine($"Cant find certificate at: {cert.Value()}"); } config.ShowHelp(); return(1); } else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value) || string.IsNullOrWhiteSpace(installId.Value)) { config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.ShowHelp("org"); return(1); } if (!Guid.TryParse(installId.Value, out Guid installationId)) { config.Error.WriteLine("Unable to parse your installation id as a GUID"); config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}"); config.ShowHelp("org"); return(1); } GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, key.Value); return(0); }); }); app.OnExecute(() => { app.ShowHelp(); return(10); }); app.HelpOption("-? | -h | --help"); try { return(app.Execute(args)); } catch (Exception e) { Console.Error.WriteLine("Oops: {0}", e); return(100); } }
//<summary> //The constructor reads the arguments given to the program and checks if all the necesary ones are present before calling the connectNStream function.</summary> static void Main(string[] args) { var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); app.Name = ""; app.Description = "Exports the Logs to a json file."; app.HelpOption("-?|-h|--help"); app.ExtendedHelpText = "following arguments are required -ApiKey, -LogId, -DateFrom and -DateTo"; var apiKey = app.Option("-ApiKey|--ApiKey <ApiKey>", "Defines the API key. Ex \" -ApiKey 8890a01cdd9c403aac9a6f89829867f3\"", CommandOptionType.SingleValue); var filename = app.Option("-Filename|--Filename <Filename>", "Defines the path and filename of the file to export to. Ex \" -Filename C:\\myDirectory\\myFile.json\"", CommandOptionType.SingleValue); var logId = app.Option("-LogId|--LogId <LogId>", "Defines the log ID. Ex \" -LogId cfc710e7-06fb-4d30-be24-a96d5caf3d19\"", CommandOptionType.SingleValue); var dateFrom = app.Option("-DateFrom|--DateFrom <DateFrom>", "Defines the Date from which the logs start. Ex. \" -DateFrom 2017-10-24\"", CommandOptionType.SingleValue); var dateTo = app.Option("-DateTo|--DateTo <DateTo>", "Defines the Date to which the logs ends. Ex. \" -DateTo 2018-11-30\"", CommandOptionType.SingleValue); var query = app.Option("-Query|--Query <Query>", "Defines the query that is passed to the API", CommandOptionType.SingleValue); var includeHeadersOption = app.Option("-IncludeHeaders|--IncludeHeaders", "Option for including the headers in output", CommandOptionType.NoValue); app.OnExecute(() => { int check = 0; if (apiKey.HasValue()) { check++; } else { Console.WriteLine("You need to define an API key. Ex \" -ApiKey 8890a01cdd9c403aac9a6f89829867f3\""); } if (logId.HasValue()) { check++; } else { Console.WriteLine("You need to define a log ID. Ex \" -LogId cfc710e7-06fb-4d30-be24-a96d5caf3d19\""); } if (check == 2) { connectNStream(filename, apiKey.Value(), logId.Value(), dateFrom.Value(), dateTo.Value(), query.Value(), includeHeadersOption.HasValue()); } return(0); }); try { var result = app.Execute(args); Environment.Exit(result); } catch (CommandParsingException cpe) { Console.WriteLine("Couldn't pass your command try using -h to find out more"); Console.WriteLine(cpe.Message); } catch (Exception e) { Console.WriteLine("An unexpected error occured try redefining your arguments or find help using -h"); Console.WriteLine(e.Message); } }
public static void OnExecute(this CommandLineApplication app, Action action) => app.OnExecute(() => { action(); return(0); });