C
C#•2y ago
Pixel

[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
undisputed world champions
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
Pixel
PixelOP•2y ago
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
undisputed world champions
i just copied this from the documentation:
using System.CommandLine;

var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file to read and display on the console.");

var rootCommand = new RootCommand("Sample app for System.CommandLine");
rootCommand.AddOption(fileOption);

rootCommand.SetHandler((file) =>
{
ReadFile(file!);
},
fileOption);

return await rootCommand.InvokeAsync(args);


static void ReadFile(FileInfo file)
{
File.ReadLines(file.FullName).ToList()
.ForEach(line => Console.WriteLine(line));
}
using System.CommandLine;

var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file to read and display on the console.");

var rootCommand = new RootCommand("Sample app for System.CommandLine");
rootCommand.AddOption(fileOption);

rootCommand.SetHandler((file) =>
{
ReadFile(file!);
},
fileOption);

return await rootCommand.InvokeAsync(args);


static void ReadFile(FileInfo file)
{
File.ReadLines(file.FullName).ToList()
.ForEach(line => Console.WriteLine(line));
}
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 😉 )
Pixel
PixelOP•2y ago
im running vs code and using the dotnet cli
Pixel
PixelOP•2y ago
this is the output
Pixel
PixelOP•2y ago
expected output would only be this
Pixel
PixelOP•2y ago
Pixel
PixelOP•2y ago
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
undisputed world champions
can you show your program.cs? you can leave out the actual command code. just the setup of system.commandline
Pixel
PixelOP•2y ago
class openmodrepo
{
public static OLog LogSuccess;
public static OLog LogWarning;
public static OLog LogError;
public static JSONDatabase AccountDB;
public static Dictionary<string, object> Configuration;
public static async Task Main(string[] args)
{
// CommandLine setup
var ProgramRootCommand = new RootCommand("Open source .NET server.");




await ProgramRootCommand.InvokeAsync(args);
...
class openmodrepo
{
public static OLog LogSuccess;
public static OLog LogWarning;
public static OLog LogError;
public static JSONDatabase AccountDB;
public static Dictionary<string, object> Configuration;
public static async Task Main(string[] args)
{
// CommandLine setup
var ProgramRootCommand = new RootCommand("Open source .NET server.");




await ProgramRootCommand.InvokeAsync(args);
...
these are the referenced namespaces if that matters
using Routes;
using System;
using Database;
using OakLogger;
using System.IO;
using WatsonWebserver;
using System.CommandLine;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Collections.Generic;
using Routes;
using System;
using Database;
using OakLogger;
using System.IO;
using WatsonWebserver;
using System.CommandLine;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Collections.Generic;
undisputed world champions
i guess you do a ProgramRootCommand.SetHandler(DoSomethingCool); or similar somewhere?
Pixel
PixelOP•2y ago
these 2 are the only instances of ProgramRootCommand on this file
undisputed world champions
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 ^^
Pixel
PixelOP•2y ago
is the await ProgramRootCommand.InvokeAsync(args); supposed to be at the end?
undisputed world champions
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 registered
Pixel
PixelOP•2y ago
ah there we go that did it
undisputed world champions
nice 😄
Want results from more Discord servers?
Add your server