This is a very basic requirement of programming. How to parse the command line arguments like a pro? With the advent of the command line trend, it is good to know how to parse it or exactly what library to parse the command line arguments. Writing own parser would not be required as there are a lot of parsers available. We may have to do if we are in enterprise boundaries where open source libraries or any third parties are not allowed.
Different technologies and languages take different approaches. Let us see how .Net can parse the arguments. Below are the main points we normally consider before selecting a command-line parser
- Ability to specify default values
- Ability to print the command line help
- Support for customizing switches
- Support for parsing collections
- Support for attribute-based configuration than coding all the rules.
- Ability to pass delegates for different subcommands than we writing if and switches based on the parsed object.
- The new subcommand support similar to dotnet.exe
- Immutability - The functional programming influences the command line parser selection. Whether we need to mutate state when each option is discovered or get it at once and preferably accept delegates to execute code.
CommandLine parsing libraries
Let us see some libraries.
Name | GitHub stats (As of 2020-06-14) | Features | Comments |
Fluent Command Line Parser | Stars - 468 stars Forks - 76 Last commit - 9 months ago Last release - 2013-04-23 | As the name says, it has a fluent API to set up the parsing logic. It supports .Net versions Full Framework No support for standard and core | Not in active maintenance |
CommandLineParser | Stars - 2.2K Forks - 273 Last commit - last month Last release - 2020-05-01 | A lot of features. Supports attribute-based rules, accepts delegate and subcommand. Max .Net versions .Net standard 2.0 .Net 4.6.1 | |
CommandLineUtils | Stars - 1.4K Forks - 173 Last commit - Yesterday Last release - 2020-03-29 | It seems more modern. Have conventions when using attributes, support for prompt and masked inputs. Supports builder API as well as attribute-based. Max .Net versions .Net standard 2.0 .Net 4.5.0 | This is a fork from Microsoft repo |
command-line-api | Stars - 1.4k Forks - 132 Last commit - 2 days ago Last release - 2020-04-15 | It is from dotnet team. Supports async invocation. | The library is still in prerelease beta state. |
Till the native command-line-api is matured it is better to use either CommandLineParser or the CommandLineUtils. The original CommandLineUtils was from Microsoft through the open-source community. It was in Microsoft.Extensions namespace. But they discontinued. So really doubtful about this new command-line-api unless it completes at least it's first 2 years.
No comments:
Post a Comment