Page 31 - MSDN Magazine, March 2019
P. 31

• Directives for parse and debug.
• The configuration of the help and version options. • Tab completion and typo corrections.
There are also separate extension methods for each feature if finer-grain control is necessary. There are also numerous other configuration capabilities exposed by the Core API. These include:
• Handling tokens that are explicitly unmatched by the configuration. • Suggestion handlers that enable tab completion, returning a list of possible values given the current command-line string
and the location of the cursor.
• Hidden commands that you don’t want to be discoverable us-
ing tab completion or help.
In addition, while there are lots of knobs and buttons to control
the command-line parsing with System.CommandLine, it also pro- vides a method-first approach. In fact, this is what’s used internally to bind to the Main like method. With the method-first approach you can use a method like Convert at the bottom of Figure 4 to configure the parser (as shown in Figure 5).
In this case, notice that the Convert method is used for the initial configuration and then you navigate the root command’s object model to add aliases. The Children indexable property contains all the options and commands attached to the root command.
Wrapping Up
I’m very excited about the functionality available in System.Com- mandLine. The fact that achieving the simple scenarios explored
here requires so little code is marvelous. Furthermore, the amount of functionality achieved—including things like tab completion, argument conversion and automated testing support, just to name a few—means that with little effort you can have a fully functioning command-line support in all of your dotnet applications.
Finally, System.CommandLine is open source. That means if there’s functionality missing that you require, you can develop the enhancement and submit it back to the community as a pull request. A couple things I would personally love to see added are support for not always specifying the option or command names on the command line and relying instead on the position of the arguments to imply what the names are. Additionally, it would be great if you could declaratively add an additional alias (such as short aliases) when using the Main like or method-first approach. n
Mark Michaelis is founder of IntelliTect, where he serves as its chief techni- cal architect and trainer. He has been a Microsoft MVP for more than two decades, and a Microsoft Regional Director since 2007. Michaelis serves on sev- eral Microsoft software design review teams, including C#, Microsoft Azure, SharePoint and Visual Studio ALM. He speaks at developer conferences and has written numerous books including his most recent, “Essential C# 7.0 (6th Edition)” (itl.tc/EssentialCSharp). Contact him on Facebook at facebook.com/ Mark.Michaelis, on his blog at IntelliTect.com/Mark, on Twitter: @markmichaelis or via e-mail at mark@IntelliTect.com.
Thanks to the following Microsoft technical experts for reviewing this article: Kevin Bost, Kathleen Dollard, Jon Sequeira
Untitled-2 1
1/8/19 10:52 AM


































































































   29   30   31   32   33