dotnet / dotnet/command-line-api
The getting started tutorial should be clear on best practices to show default help when no options are provided
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
I am not an experienced CLI app developer so I may be missing something fundamental here, but I was surprised that simply running the barebones example in the [System.CommandLine tutorial](https://learn.microsoft.com/en-us/dotnet/standard/commandline/get-started-tutorial#create-the-app) returns the following:
```
dotnet run
Unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object.
at scl.Program.ReadFile(FileInfo file) in C:\dev\cli\test-cli\Program.cs:line 27
at scl.Program.<>c.b__0_0(FileInfo file) in C:\dev\cli\test-cli\Program.cs:line 18
at System.CommandLine.Handler.<>c__DisplayClass2_0`1.b__0(InvocationContext context)
at System.CommandLine.Invocation.AnonymousCommandHandler.Invoke(InvocationContext context)
at System.CommandLine.Invocation.AnonymousCommandHandler.InvokeAsync(InvocationContext context)
at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass19_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__18_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<b__5_0>d.MoveNext()
--- End of stack trace from previous location ---
at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<b__0>d.MoveNext()
```
This is understandable behavior given that the code does try to access the null `FileInfo` object which of course throws an exception. However, given that `--file` is an *option* I was somehow expecting that running the root command without any arguments would simply display the help output.
At the time I thought maybe this is just the beginning and that guidelines on how to do this would show up by the end of the tutorial, but was surprised to find it not mentioned at all.
Is this somehow not a fairly common use case that simply calling the executable without any arguments would bring up the default help? Why require that the user passes `scl --help`? In many cases users are not even aware this option exists. I would imagine that surprising them with a gigantic stack trace with the first exception encountered while running the command is very likely to scare them into not using the tool at all.
If there are indeed guidelines for doing this, I think they should be included in the Getting Started tutorial, since even after several hours of searching I cannot find any recommendations or examples on this anywhere online.
Contributor guide
Assessment
This issue has not been assessed yet.