Skip to content

Commit

Permalink
Guide users in case of misspelled commands or options
Browse files Browse the repository at this point in the history
When we get an unrecognized command or option, search one with a similar name and show it to the user.
Here is an example from a test:

```
elm-fullstack run-server  --remove-previous-process
```

This command-line now leads to the following output:
```
Specify --help for a list of available options and commands.

Unrecognized option '--remove-previous-process'

Did you mean 'delete-previous-process'?
```
  • Loading branch information
Viir committed May 28, 2020
1 parent a9e04a4 commit 839a8b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Kalmit.PersistentProcess.WebHost
{
public class Program
{
static public string AppVersionId => "2020-05-27";
static public string AppVersionId => "2020-05-28";
}
}
39 changes: 38 additions & 1 deletion implement/elm-fullstack/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,44 @@ CommandOption verboseLogOptionFromCommand(CommandLineApplication command) =>
return 1;
});

return app.Execute(args);
int executeAndGuideInCaseOfException()
{
try
{
return app.Execute(args);
}
catch (CommandParsingException ex)
{
DotNetConsoleWriteProblemCausingAbort(ex.Message);

if (ex is UnrecognizedCommandParsingException uex && uex.NearestMatches.Any())
{
DotNetConsoleWriteProblemCausingAbort("\nDid you mean '" + uex.NearestMatches.FirstOrDefault() + "'?");
}

return 430;
}
}

return executeAndGuideInCaseOfException();
}

static public void DotNetConsoleWriteLineUsingColor(string line, ConsoleColor color)
{
var colorBefore = Console.ForegroundColor;

Console.ForegroundColor = color;

Console.WriteLine(line);

Console.ForegroundColor = colorBefore;
}

static public void DotNetConsoleWriteProblemCausingAbort(string line)
{
Console.WriteLine("");

DotNetConsoleWriteLineUsingColor(line, ConsoleColor.Yellow);
}

public class DeployAppReport
Expand Down
4 changes: 2 additions & 2 deletions implement/elm-fullstack/elm-fullstack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>elm_fullstack</RootNamespace>
<AssemblyName>elm-fullstack</AssemblyName>
<AssemblyVersion>2020.0527.0.0</AssemblyVersion>
<FileVersion>2020.0527.0.0</FileVersion>
<AssemblyVersion>2020.0528.0.0</AssemblyVersion>
<FileVersion>2020.0528.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down

0 comments on commit 839a8b8

Please sign in to comment.