Skip to content

Commit 7ce195e

Browse files
committed
Add manual System.CommandLine parsing
1 parent 5f35e97 commit 7ce195e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

BooleanExpressionParser/Program.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.CommandLine;
12
using System.Text;
23
using Spectre.Console;
34

@@ -6,7 +7,32 @@ namespace BooleanExpressionParser;
67
internal class Program
78
{
89

9-
static void Main(params string[] args)
10+
private static void Main(string[] args)
11+
{
12+
var rootCommand = new RootCommand();
13+
14+
var trueOption = new Option<string>(new[] { "--true", "-t" }, () => "1", description: "Character to use for true values in the truth table.");
15+
var falseOption = new Option<string>(new[] { "--false", "-f" }, () => "0", description: "Character to use for false values in the truth table.");
16+
var expressionsArgument = new Argument<string[]>("expression(s)", description: "The boolean expression(s) to evaluate.");
17+
18+
var tableCommand = new Command("table", description: "Prints the truth table of a boolean expression(s).")
19+
{
20+
trueOption,
21+
falseOption,
22+
expressionsArgument
23+
};
24+
25+
tableCommand.SetHandler(Run, trueOption, falseOption, expressionsArgument);
26+
27+
rootCommand.AddCommand(tableCommand);
28+
29+
rootCommand.Invoke(args);
30+
31+
32+
}
33+
34+
35+
private static void Run(string @true, string @false, string[] args)
1036
{
1137
Console.OutputEncoding = Encoding.UTF8;
1238

@@ -26,7 +52,11 @@ static void Main(params string[] args)
2652
}
2753

2854
var tables = new List<string>();
29-
var formatter = new Formatter();
55+
var formatter = new Formatter
56+
{
57+
True = @true[0],
58+
False = @false[0]
59+
};
3060

3161
foreach (var expression in expressions)
3262
{
@@ -57,7 +87,6 @@ static void Main(params string[] args)
5787
if (tables.Count > 1)
5888
{
5989
var output = formatter.JoinTruthTables(tables.ToArray());
60-
Console.WriteLine(output);
6190
AnsiConsole.Markup(output);
6291

6392
}

0 commit comments

Comments
 (0)