Skip to content

Commit

Permalink
Merge pull request #712 from sys27/feature/cli
Browse files Browse the repository at this point in the history
#710 - Add ability to execute expression from files.
  • Loading branch information
sys27 committed Sep 6, 2023
2 parents f841774 + 315f521 commit f1ad7a1
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 26 deletions.
25 changes: 21 additions & 4 deletions docs/articles/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The `Simplifier` allows you to simplify the expression, in the code shown above,
xFunc has a CLI tool, to install it use the following command:

```bash
dotnet tool install -g xFunc.DotnetTool
dotnet tool install -g xFunc.Cli
```

after that, you will be able to use the `xfunc` command in your terminal. It supports the following commands:
Expand All @@ -89,9 +89,26 @@ This command launches the CLI tool in the interactive mode where you can enter e

```
> sin(90 'deg')
> 1
1
> cos(90 'deg')
> 0
0
```

To exit use Cmd+C or Cmd+D on Mac OS.
To exit use Cmd+C or Cmd+D on Mac OS.

Also, you are able to save the current progress to a file (by using `#save <path>` command) and run it later with `xfunc run`.

### `xfunc run`

Let's assume you saved your progress to the `test.xf` file with the following content:

```
sin(90 'deg')
cos(90 'deg')
```

So, you can execute it by:

```
xfunc run test.xf
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.DotnetTool.Options;
namespace xFunc.Cli.Options;

public abstract class BaseOptions : DebugInfoOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.DotnetTool.Options;
namespace xFunc.Cli.Options;

public abstract class DebugInfoOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.DotnetTool.Options;
namespace xFunc.Cli.Options;

[Verb("interactive", HelpText = "Run interactive mode.")]
public class InteractiveOptions : DebugInfoOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.DotnetTool.Options;
namespace xFunc.Cli.Options;

[Verb("parse", HelpText = "Parse string expression.")]
public class ParseOptions : BaseOptions
Expand Down
18 changes: 18 additions & 0 deletions xFunc.Cli/Options/RunFileOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.Cli.Options;

[Verb("run", HelpText = "Run all expressions from a file.")]
public class RunFileOptions : DebugInfoOptions
{
[Value(0, Required = true, MetaName = "File", HelpText = "Path to a file.")]
public string File { get; set; }

[Usage(ApplicationAlias = "xfunc")]
public static IEnumerable<Example> Examples
=> new List<Example>
{
new Example("Run all expressions from a file", new RunFileOptions { File = "./file.xf" })
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace xFunc.DotnetTool.Options;
namespace xFunc.Cli.Options;

[Verb("solve", HelpText = "Calculate result of expression.")]
public class SolveOptions : BaseOptions
Expand Down
63 changes: 48 additions & 15 deletions xFunc.DotnetTool/Program.cs → xFunc.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) Dmytro Kyshchenko. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using xFunc.DotnetTool.Options;
using xFunc.Cli.Options;

namespace xFunc.DotnetTool;
namespace xFunc.Cli;

public class Program
{
private static readonly Processor processor = new Processor();
private static readonly List<string> expressions = new List<string>();

private static void Parse(ParseOptions options)
{
Expand All @@ -34,17 +35,53 @@ private static void Interactive(InteractiveOptions options)
Console.Write("> ");

var stringExpression = Console.ReadLine()?.ToLower();
if (stringExpression is null ||
stringExpression == "#quit" ||
stringExpression == "#exit")
{
if (stringExpression is null or "#quit" or "#exit")
break;

if (stringExpression == "#clear")
{
Console.Clear();
expressions.Clear();
continue;
}

if (stringExpression.StartsWith("#save "))
{
var path = stringExpression[6..];
using var file = File.CreateText(path);

foreach (var expression in expressions)
file.WriteLine(expression);

continue;
}

Run(options, () =>
{
var result = processor.Solve(stringExpression);
Console.WriteLine($"> {result}");
Console.WriteLine(result);
expressions.Add(stringExpression);
});
}
}

private static void RunFile(RunFileOptions options)
{
using var file = File.OpenText(options.File);
while (!file.EndOfStream)
{
var line = file.ReadLine()?.Trim();
if (string.IsNullOrWhiteSpace(line))
continue;

if (line.StartsWith("#"))
continue;

Run(options, () =>
{
var result = processor.Solve(line);
Console.WriteLine(result);
});
}
}
Expand Down Expand Up @@ -130,17 +167,13 @@ private static void Run(DebugInfoOptions options, Action action)
}

private static void PrintError(DebugInfoOptions options, Exception e)
{
if (options.Debug)
Console.WriteLine($"> {e}");
else
Console.WriteLine($"> {e.Message}");
}
=> Console.WriteLine(options.Debug ? e : e.Message);

public static void Main(string[] args)
=> CommandLine.Parser.Default
.ParseArguments<ParseOptions, SolveOptions, InteractiveOptions>(args)
.ParseArguments<ParseOptions, SolveOptions, InteractiveOptions, RunFileOptions>(args)
.WithParsed<ParseOptions>(Parse)
.WithParsed<SolveOptions>(Solve)
.WithParsed<InteractiveOptions>(Interactive);
.WithParsed<InteractiveOptions>(Interactive)
.WithParsed<RunFileOptions>(RunFile);
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>latest</LangVersion>
<PackAsTool>true</PackAsTool>
<ToolCommandName>xfunc</ToolCommandName>
<PackageId>xFunc.DotnetTool</PackageId>
<PackageId>xFunc.Cli</PackageId>
<Version>4.3.0</Version>
<Product>xFunc.Maths</Product>
<Authors>Dmytro Kyshchenko</Authors>
Expand Down
2 changes: 1 addition & 1 deletion xFunc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.Maths", "xFunc.Maths\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.Tests", "xFunc.Tests\xFunc.Tests.csproj", "{A4C3E90E-647D-4DA8-BA97-C9034C301D37}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.DotnetTool", "xFunc.DotnetTool\xFunc.DotnetTool.csproj", "{907CDF76-60C1-4894-9CB3-496FFFF92E13}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.Cli", "xFunc.Cli\xFunc.Cli.csproj", "{907CDF76-60C1-4894-9CB3-496FFFF92E13}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "xFunc.Benchmark", "xFunc.Benchmark\xFunc.Benchmark.csproj", "{EC5FB562-9BFE-4BD9-A3E4-666D3021AF1F}"
EndProject
Expand Down

0 comments on commit f1ad7a1

Please sign in to comment.