Exceptions running the finished app.
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 15h 21m
- Merged PRs (30d)
- 370
Description
### Type of issue
Code doesn't work
### Description
1. In finished app, several `Possible null`:
```cs
static int Main(string[] args)
{
Option fileOption = new("--file")
{
Description = "An option whose argument is parsed as a FileInfo",
Required = true,
DefaultValueFactory = result =>
{
if (result.Tokens.Count == 0)
{
return new FileInfo("sampleQuotes.txt");
}
string filePath = result.Tokens.Single().Value;
if (!File.Exists(filePath))
{
result.AddError("File does not exist");
return null; // ⚠️ CS8603: Possible null reference return.
}
else
{
return new FileInfo(filePath);
}
}
};
...
readCommand.SetAction(parseResult => ReadFile(
parseResult.GetValue(fileOption), // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.ReadFile(FileInfo file, ...)'.
parseResult.GetValue(delayOption),
parseResult.GetValue(fgcolorOption),
parseResult.GetValue(lightModeOption)));
deleteCommand.SetAction(parseResult => DeleteFromFile(
parseResult.GetValue(fileOption), // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.DeleteFromFile(FileInfo file, ...)'.
parseResult.GetValue(searchTermsOption))); // ⚠️ CS8603: Possible null argument for parameter 'searchTerms' in 'void Program.DeleteFromFile(FileInfo file, ...)'.
addCommand.SetAction(parseResult => AddToFile(
parseResult.GetValue(fileOption), // ⚠️ CS8603: Possible null argument for parameter 'file' in 'void Program.AddToFile(FileInfo file, ...)'.
parseResult.GetValue(quoteArgument), // ⚠️ CS8603: Possible null argument for parameter 'quote' in 'void Program.AddToFile(FileInfo file, ...)'.
parseResult.GetValue(bylineArgument)) // ⚠️ CS8603: Possible null argument for parameter 'byline' in 'void Program.AddToFile(FileInfo file, ...)'.
);
```
2. File Not Found Exception in `ReadFile()`:
```powershell
scl quotes read --file nofile
```
```cs
internal static void ReadFile(FileInfo file, int delay, ConsoleColor fgColor, bool lightMode)
{
Console.BackgroundColor = lightMode ? ConsoleColor.White : ConsoleColor.Black;
Console.ForegroundColor = fgColor;
foreach (string line in File.ReadLines(file.FullName)) // ❌ System.IO.FileNotFoundException: 'Could not find the file '...\nofile'.'
{
Console.WriteLine(line);
Thread.Sleep(TimeSpan.FromMilliseconds(delay * line.Length));
}
}
```
Note:
fileOption.DefaultValueFactory is never executed.
3. IO Exception:
```powershell
scl quotes delete --search-terms David "You can do" Antoine "Perfection is achieved"
```
```cs
internal static void DeleteFromFile(FileInfo file, string[] searchTerms)
{
Console.WriteLine("Deleting from file");
var lines = File.ReadLines(file.FullName).Where(line => searchTerms.All(s => !line.Contains(s))); // ❌ System.IO.IOException: 'The process cannot access the file '...\sampleQuotes.txt' because it is being used by another process.'
File.WriteAllLines(file.FullName, lines);
}
```
Solution:
```cs
File.WriteAllLines(file.FullName, lines.ToList());
```
### Page URL
https://learn.microsoft.com/en-us/dotnet/standard/commandline/get-started-tutorial
### Content source URL
https://github.com/dotnet/docs/blob/main/docs/standard/commandline/get-started-tutorial.md
### Document Version Independent Id
fa160b1d-66ed-8c5d-37cd-b7e02443e9f1
### Platform Id
0f144444-1ce3-cdf7-87a6-c659d44e08d3
### Article author
@tdykstra
### Metadata
* ID: 7705dc41-7297-0807-7e9c-2e6548c26b84
* PlatformId: 0f144444-1ce3-cdf7-87a6-c659d44e08d3
* Service: **dotnet-fundamentals**
[Related Issues](https://github.com/dotnet/docs/issues?q=is%3Aissue+is%3Aopen+fa160b1d-66ed-8c5d-37cd-b7e02443e9f1)
Contributor guide
Assessment
This issue has not been assessed yet.