Main() and command-line arguments
When building console applications that take parameters, you can use the arguments passed to
Main(string[] args)
.
What would be a simple way to do something like example.exe --username=drpadawan --password=dontstealmypasswordplease
.
I have looked at some nuget packages but I feel it must be possible without a third party package.7 Replies
Honestly, this is one of those things where I would always use a package. It's just so much simpler and you can get to the important stuff.
Consider Dragonfruit or CommandLine.
GitHub
command-line-api/Your-first-app-with-System-CommandLine-DragonFruit...
Command line parsing, invocation, and rendering of terminal output. - command-line-api/Your-first-app-with-System-CommandLine-DragonFruit.md at main · dotnet/command-line-api
Without Dragonfruit: https://docs.microsoft.com/en-us/dotnet/standard/commandline/
System.CommandLine overview
Learn how to develop and use command-line apps that are based on the System.CommandLine library
Dragonfruit is just a layer on top of System.CommandLine that negates the need to parse the arguments, such that you just create a type definition and it automatically parses args into that type, requiring less code.
If your program is a little more complicated, you might consider using an entire CLI host - such as CliFx or Spectre.Console.Cli
this is significantly more complicated than just a main method, but if you support 10 subcommands and 30+ commandline switches, its amazing.
It's just simple string manipulation -
something like
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View