[System.Commandline] how to exit on --help
By default System.Commandline adds a --help which is nice but unlike the behavior of most POSIX programs the program does not exit when the --help option is added.
How would i replicate this behavior?
17 Replies
are you only using the RootCommand?
has been a while since i used it, but i was only using nested-/sub-commands and don't remember having a problem like that
yeah i am
as per microsoft's guide i started out with a RootCommand, but beforea dding any other commands i've noticed this issue im having
i just copied this from the documentation:
it seems to exit as expected
so maybe there is something wrong with your code
or maybe you are confused, that vs is keeping the console window open until you hit any key? with this message "To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . ."
that only happens when you debug through vs. and the message also mentions where you can change this behavior (its usually nice so you can read the console output after the app exited 😉 )
im running vs code and using the dotnet cli
this is the output
expected output would only be this
this happens with both dotnet run and running the openmodrepo executable
yeah even after removing everything but a Console.ReadLine it still happens
maybe this is an issue with my environment? idk im on net 7.0.103
just tested net6.0, still happens
can you show your program.cs?
you can leave out the actual command code. just the setup of system.commandline
these are the referenced namespaces if that matters
i guess you do a
ProgramRootCommand.SetHandler(DoSomethingCool);
or similar somewhere?these 2 are the only instances of ProgramRootCommand on this file
or did you put you command code after the
await ProgramRootCommand.InvokeAsync(args);
?
that would explain the problem ^^ that code would be run after the help or any commands ^^is the
await ProgramRootCommand.InvokeAsync(args);
supposed to be at the end?put your code into a function called
DoSomethingCool
for example and do ProgramRootCommand.SetHandler(DoSomethingCool);
before you call await ProgramRootCommand.InvokeAsync(args);
yes you want to setup the RootCommand
with all the commands and arguments etc you want to handle and then call await ProgramRootCommand.InvokeAsync(args);
it will read all the arguments from the input and call the appropriate command you registeredah there we go that did it
nice 😄