Questions tagged [system.commandline]
27 questions
12
votes
2 answers
My System.CommandLine app won't build! It can't find a CommandHandler. Do I need to write it?
I am using VS 2022, .Net 6.0, and trying to build my first app using System.CommandLine.
Problem: when I build it, I get an error
The name 'CommandHandler' does not exist in the current context
The code I'm trying to build is the sample app from…

Hawkins Dale
- 123
- 1
- 6
6
votes
1 answer
How do you tell if an option was specified when using System.CommandLine?
With a root command:
new RootCommand
{
new Option("--myoption")
};
how do you tell the difference between
./myapp
and
./myapp --myoption ""
?
I initially assumed that the option would be null if it wasn't specified, but it's not, it's…

Mark Raymond
- 906
- 8
- 22
4
votes
1 answer
Using System.CommandLine with custom Main() signature
I'm trying to use System.CommandLine and I've installed the nuget package:
Install-Package System.CommandLine -Version 2.0.0-beta1.21308.1
According to this Microsoft article, I should be able to just write a Main() method with my signature and it…

sashoalm
- 75,001
- 122
- 434
- 781
2
votes
0 answers
How do I hide an alias of a command line parameter using System.CommandLine?
I'm using System.CommandLine to handle the command line parameters for an application.
I have a required parameter defined as:
var foo = new Option("--foo", "...") { IsRequired = true };
For reasons, I need to change the name of this…

adrianbanks
- 81,306
- 22
- 176
- 206
2
votes
1 answer
How do I tell if --help command was used in System.CommandLine?
I know how to tell if an option that I've created was used and how to retrieve its value:
// Add the option:
var outputOption = new Option(new[] { "-o", "--output" }, "Path to desired output folder.");
var rootCommand = new RootCommand("File…

hribayz
- 139
- 9
2
votes
2 answers
System.CommandLine Command custom validator: how to search
I am writing a C# console application project (.NET 7.0) using Visual Studio for macOS 2022 v17.4 and System.CommandLine (2.0.0-beta4.22272.1).
I am trying to add a custom validator to one of my Commands, and found the following code snippet on the…

R.J. Dunnill
- 2,049
- 3
- 10
- 21
2
votes
2 answers
How to specify C# System.Commandline behaviour when no arguments are provided?
In my console application, whichever handler I specify list (parameter 3 in this case) will be executed when no console arguments are provided. The handler is called with the boolean parameter set to false. But it makes more sense to me for it to…

joon
- 832
- 13
- 31
2
votes
1 answer
System.CommandLine argument not being passed to handler
I'm trying to get to grips with System.CommandLine.
I can parse boolean options on the commandline such as -x and I can pass string options, eg -f myfile however what I'm having trouble with is the string argument that can optionally be passed to…

Craig Graham
- 1,161
- 11
- 35
2
votes
2 answers
System.CommandLine parsed values don't match input
I am trying to use System.CommandLine and I haven't been able to get my handler to see any of the values that I'm passing in. I've tried the simplest command line program just to see if any values make it through and so far I haven't been…

TJ Rockefeller
- 3,178
- 17
- 43
1
vote
1 answer
How to exclude some enum values in System.CommandLine?
I have a System.CommandLine option like this (LogLevel is an enum):
private static readonly Option option = new(new[] { "--logLevel" })
{
Description = "The severity (log level)."
};
The automatically generated help…

hardfork
- 2,470
- 1
- 23
- 43
1
vote
1 answer
Is System.CommandLine F#-friendly?
For F# apps, I've traditionally used different functional-friendly command line parsers like Argu and CommandLineParser.
Now that Microsoft has come up with System.CommandLine (with potentially brings better support and documentation), is it usable…

psfinaki
- 1,814
- 15
- 29
1
vote
1 answer
Using System.CommandLIne to parse options and commands with options
Scenario: I want to parse a console app's command line that has a number of options (that don't relate to a command), and a command that has a number of options. I've simplified what I'm trying to into a fictitious example. E.g. myapp --i infile.dat…

Andy Johnson
- 7,938
- 4
- 33
- 50
1
vote
1 answer
Command handler receives wrong arguments when it's a let binding but works as lambda
I'm trying to use F# and System.CommandLine to make a little CLI tool. A command can have a handler callback that is called when the command is used. Usually one can define several flags for a command and the name of the flag is used to bind to an…

Timo
- 9,269
- 2
- 28
- 58
1
vote
0 answers
System.CommandLine , exceptions are never caught
we are using the following piece of code which uses the system.commandline nuget pkg, the problem that we are seeing is that any exception that happens in the cmd.Invoke(args) is not being caught in the catch block, and the finally statement is…

nen
- 621
- 2
- 10
- 26
1
vote
3 answers
How can I define an enum as CLI option with System.CommandLine.DragonFruit?
I'd like to define an enum as CLI option in C# System.CommandLine.DragonFruit. Is there "builtin" support for this use case? In detail I need an equivalent to Python's click implementation of this:
@click.option('-m', '--mode', required=True,…

thinwybk
- 4,193
- 2
- 40
- 76