Created
April 7, 2022 08:46
-
-
Save abbottdev/7bbfd9e59752cd15a93d684e7bad616f to your computer and use it in GitHub Desktop.
Global option use case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.CommandLine; | |
public class GenerateCommand : Command | |
{ | |
public GenerateCommand(Option<string> envOption) : base("generate", "A generator description") | |
{ | |
this.AddCommand(new GenerateMessageCommand(envOption)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.CommandLine; | |
public class GenerateMessageCommand : Command | |
{ | |
public GenerateMessageCommand(Option<string> envOption) : base("message", "Generates messages") | |
{ | |
this.AddOption(envOption); | |
this.AddCommand(new Command("order", "Generates order messages")); | |
this.AddCommand(new GenerateStockMessageCommand(envOption)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.CommandLine; | |
public class GenerateStockMessageCommand : Command | |
{ | |
public GenerateStockMessageCommand(Option<string> env) : base("stock", "Generates stock messages") | |
{ | |
this.AddOption(env); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://aka.ms/new-console-template for more information | |
using System.CommandLine; | |
Console.WriteLine("Hello, World!"); | |
var globalOption = new Option<string>("--env", "An environment override"); | |
var rootCommand = new RootCommand | |
{ | |
new GenerateCommand(globalOption) | |
}; | |
rootCommand.AddGlobalOption(globalOption); | |
return rootCommand.Invoke(args); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment