0

I would like to create a .NET C# application that takes a few parameters as arguments but deduced by name similar to how dism.exe accepts it's arguments.

So for example I can run it in the following fashion :

myapp.exe /host:<host.com> /db:<db_test> /user:<username> /pass:<password>
myapp.exe /help

or it could be

myapp.exe --host <host.com> --db <db_test> --user <username> --pass <password>
myapp.exe --help

I've tried

static void Main(string[] args)

but this expects parameters like

myapp.exe <host.com> <db_test> <username> <password>

So the user might easily confuse the order

Any ideas if this is possible?

gabtzi
  • 573
  • 3
  • 8
  • 24
  • 1
    You misunderstand how arguments are used and processed. It is entirely your job to do a string comparison on args[ix] to detect "/user" and then use args[ix+1] for its value. Pretty mechanical code, so there are libraries that help you do it. Go shopping by googling "c# parse command line args". – Hans Passant Apr 01 '18 at 08:36
  • There are libraries for exactly this purpose. I have closed your question as a dupe of one of the questions with a list of those libraries. If I misunderstood your question, please say so and I will reopen it again. – Heinzi Apr 01 '18 at 08:36
  • @Heinzi Perhaps this is not the place to bring this up but that duplicate is also woefully out of date *and* locked so even if one wanted to add more (it does not mention [Microsoft.Extensions.CommandLineUtils](https://www.nuget.org/packages/Microsoft.Extensions.CommandLineUtils/) for example) it cannot be changed. – Mike Zboray Apr 01 '18 at 08:42
  • Thanks for pointing me in the right direction on how I had to look for it. I'm coming from PHP frameworks where they handle this kind of processing internally so my searching kept bringing me to the same the same answers about adding string[] args to main. I basically needed to know if there was a built-in way that I was missing – gabtzi Apr 01 '18 at 08:44
  • @mikez: You are right. Do you have a better idea? Since library recommendations are off-topic on SO, I'm not sure what to do instead. – Heinzi Apr 01 '18 at 15:30

0 Answers0