public static void Run(string[] args, bool pauseOnExit, Microsoft.Extensions.CommandLineUtils.CommandLineApplication app, List <ICommandDefinition> primaryCommands, IEnumerable <ICommandDefinition> loadedComponents) { // add default commmands if (!loadedComponents.Any(pc => pc.Name.Equals("version", StringComparison.InvariantCultureIgnoreCase))) { primaryCommands.Add(new VersionPrimaryCommand()); } primaryCommands.AddRange(loadedComponents); primaryCommands.ToList().ForEach(pc => { var command = app.Command(pc.Name, pc.Config); pc.SubCommandDefinitions.ToList().ForEach(sc => command.Command(sc.Name, sc.Config)); }); //give people help with --help app.HelpOption("-? | -h | --help"); if (!args.Any()) { args = new string[] { "--help" }; } int result = 0; try { result = app.Execute(args); } catch (Exception ex) { Console.WriteLine(ex.Message); result = app.Execute(new string[] { "--help" }); } if (pauseOnExit) { Console.WriteLine("Hit Return to exit."); Console.ReadLine(); } Environment.Exit(result); }
static void Main(string[] args) { ILoggerFactory loggerFactory = new LoggerFactory() .AddConsole() .AddDebug(); var logger = loggerFactory.CreateLogger <Program>(); var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); app.Name = "dotnet UpdateVersionConsole.dll"; var csprojoption = app.Option("--CSProjPath", "Full path of the CS Project File", Microsoft.Extensions.CommandLineUtils.CommandOptionType.SingleValue); var buildnumber = app.Option("--BuildNumber", "Build number which needs to appended to project version", Microsoft.Extensions.CommandLineUtils.CommandOptionType.SingleValue); //give people help with --help app.HelpOption("-? | -h | --help"); app.OnExecute(() => { string csprojFilePath = ""; int intBuildNumber = 0; if (!csprojoption.HasValue()) { logger.LogError("Please specific the CSProj file path. Use --help for more info"); return(1); } else { csprojFilePath = csprojoption.Value(); var ext = Path.GetExtension(csprojFilePath); if (ext != ".csproj") { logger.LogError("Please specific file with .csproj extension. Use --help for more info"); return(11); } if (!File.Exists(csprojFilePath)) { logger.LogError("CSProj file doesn't exist. Please specify valid file. Use --help for more info"); return(12); } } if (!buildnumber.HasValue()) { logger.LogError("Please give build number which needs to be appended. Use --help for more info"); return(2); } else { if (!int.TryParse(buildnumber.Value(), out intBuildNumber)) { logger.LogError("Please give build number as whole number. Use --help for more info"); return(21); } } UpdateCSProjVersion.UpdateBuildNumber(csprojFilePath, intBuildNumber.ToString()); return(0); }); app.Execute(args); }
private static async Task Main(string[] args) { // should subscribe to some cancel event like keystroke var cancellationTokenSource = new CancellationTokenSource(); // parse command line arguments var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var workerOption = app.Option("--worker", "Run the process as worker", Microsoft.Extensions.CommandLineUtils.CommandOptionType.NoValue); app.HelpOption("--help"); app.ShowHelp(); app.Execute(args); if (workerOption.HasValue()) { // if command line argument --worker has been set, we should only process the request until there are somethong to process Console.WriteLine("Executed as worker"); var workerService = container.Resolve <IWorkerService>("Worker"); await workerService.ProcessAsync(cancellationTokenSource.Token); // terminate the process when we are finished return; } Console.WriteLine("Executed as host"); var host = container.Resolve <IWorkerService>("Host"); await host.ProcessAsync(cancellationTokenSource.Token); }
private static async Task MainAsync(string[] args) { var result = 0; Console.WriteLine("Message Sender Data Generator: usage: dotnet DataGenNetCore.dll sendmessages (slow|fast|faster|insane)"); Console.WriteLine("Press ESC to stop"); var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var sendmessages = app.Command("sendmessages", config => { config.OnExecute(() => { config.ShowHelp(); //show help for catapult return(1); //return error since we didn't do anything }); config.HelpOption("-? | -h | --help"); //show help on --help }); sendmessages.Command("help", config => { config.Description = "get help!"; config.OnExecute(() => { sendmessages.ShowHelp("sendmessages"); return(1); }); }); sendmessages.Command("slow", config => { config.Description = "run message sending with 1 second thread sleep (slow)"; config.HelpOption("-? | -h | --help"); config.OnExecute(async() => { Console.WriteLine("using slow mode (1 message every five seconds using thread.sleep)"); await sendMessages("slow"); return(0); }); }); sendmessages.Command("fast", config => { config.Description = "run message sending with no thread sleep (fast)"; config.HelpOption("-? | -h | --help"); config.OnExecute(async() => { Console.WriteLine("using fast mode (single thread, no delay)"); await sendMessages("fast"); return(0); }); }); sendmessages.Command("faster", config => { config.Description = "run parallel for loop message sending (fastest)"; config.HelpOption("-? | -h | --help"); config.OnExecute(async() => { Console.WriteLine("using faster mode (10 threads via parallel.for, no delay)"); await sendMessages("faster"); return(0); }); }); sendmessages.Command("insane", config => { config.Description = "run parallel for loop message sending (insane)"; config.HelpOption("-? | -h | --help"); config.OnExecute(async() => { Console.WriteLine("using insane mode (100 threads via parallel.for, no delay)"); await sendMessages("insane"); return(0); }); }); //give people help with --help app.HelpOption("-? | -h | --help"); try { var connectionStringBuilder = new EventHubsConnectionStringBuilder(secrets.EventHubConnectionString()) { EntityPath = secrets.EventHubPath() }; hubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); app.Execute(args); } catch (Exception e) { app.HelpOption("-? | -h | --help"); Console.WriteLine("Error occurred: {0}", e.Message); } Environment.Exit(result); }
static int Main(string[] args) { if (System.Console.BufferWidth == System.Console.WindowWidth) { System.Console.SetBufferSize(System.Console.BufferWidth * 2, System.Console.BufferHeight); } // download everything local to the site var commandLineApplication = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication(); var siteArgument = commandLineApplication.Argument("site", "The site to scrape"); var folderArgument = commandLineApplication.Argument("folder", "The folder to scrape to"); var updateOption = commandLineApplication.Option("-u|--update", "Whether to update or ignore existing files", Microsoft.Extensions.CommandLineUtils.CommandOptionType.NoValue); commandLineApplication.HelpOption("-h|--help"); commandLineApplication.OnExecute(() => { var site = siteArgument.Value; var local = folderArgument.Value; update = updateOption.HasValue(); Task task; using (var client = new System.Net.Http.HttpClient(new System.Net.Http.HttpClientHandler { AllowAutoRedirect = false, AutomaticDecompression = System.Net.DecompressionMethods.None })) { var cancellationTokenSource = new System.Threading.CancellationTokenSource(); var baseUri = new Uri(site); var basePath = GetFileName(local, baseUri); if (!update && System.IO.File.Exists(basePath)) { // just use the current var links = GetLinks(System.IO.File.ReadAllText(basePath), baseUri); task = ProcessUris(client, local, update ? links : links.OrderBy(_ => System.IO.File.Exists(GetFileName(local, _)), new ExistsComparer()), cancellationTokenSource.Token); } else { task = Process(client, local, baseUri, cancellationTokenSource.Token); } while (!(task.IsFaulted || task.IsCompleted || task.IsCanceled)) { System.Threading.Thread.Sleep(100); if (Console.KeyAvailable) { var key = Console.ReadKey(); switch (key.Key) { case ConsoleKey.Escape: cancellationTokenSource.Cancel(); break; case ConsoleKey.UpArrow: Delay++; break; case ConsoleKey.DownArrow: if (Delay > 0) { Delay--; } break; } } } if (task != null && task.IsFaulted) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("{0}ERROR! - {1}", currentIndent, task.Exception.ToString()); Console.ForegroundColor = DefaultConsoleColor; return(task.Exception.HResult); } } return(0); }); return(commandLineApplication.Execute(args)); }