Fixed for AOT publishing on linux.
This commit is contained in:
parent
f467d6eeae
commit
b36efadefd
3 changed files with 91 additions and 60 deletions
|
|
@ -1,21 +1,11 @@
|
||||||
using CommandLine;
|
namespace cs_mic_cli
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace cs_mic_cli
|
|
||||||
{
|
{
|
||||||
internal class Options
|
internal class Options
|
||||||
{
|
{
|
||||||
[Option('v', "verbose", HelpText = "Displays error messages from the parser if a soft error occurs.")]
|
|
||||||
public bool Verbose { get; set; }
|
public bool Verbose { get; set; }
|
||||||
|
|
||||||
[Option('i', "interactive", HelpText = "Starts an interactive session.")]
|
|
||||||
public bool Interactive { get; set; }
|
public bool Interactive { get; set; }
|
||||||
|
|
||||||
[Value(0)]
|
public IEnumerable<string> Expression { get; set; } = Array.Empty<string>();
|
||||||
public required IEnumerable<string> Expression { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,66 +1,107 @@
|
||||||
using CommandLine;
|
using cs_mic_cli.Functions;
|
||||||
using cs_mic_cli.Functions;
|
|
||||||
using CSMic;
|
using CSMic;
|
||||||
using CSMic.StandardLibrary;
|
using CSMic.StandardLibrary;
|
||||||
|
using System.CommandLine;
|
||||||
|
|
||||||
namespace cs_mic_cli
|
namespace cs_mic_cli
|
||||||
{
|
{
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
static void Main(string[] args)
|
static int Main(string[] args)
|
||||||
{
|
{
|
||||||
CommandLine.Parser.Default.ParseArguments<Options>(args)
|
var verboseOption = new Option<bool>("--verbose", "-v")
|
||||||
.WithParsed(options =>
|
{
|
||||||
|
Description = "Displays error messages from the parser if a soft error occurs."
|
||||||
|
};
|
||||||
|
var interactiveOption = new Option<bool>("--interactive", "-i")
|
||||||
|
{
|
||||||
|
Description = "Starts an interactive session."
|
||||||
|
};
|
||||||
|
var expressionArgument = new Argument<string[]>("expression")
|
||||||
|
{
|
||||||
|
Arity = ArgumentArity.ZeroOrMore,
|
||||||
|
Description = "Expression to evaluate."
|
||||||
|
};
|
||||||
|
expressionArgument.Validators.Add(result =>
|
||||||
|
{
|
||||||
|
foreach (var token in result.Tokens)
|
||||||
{
|
{
|
||||||
var fullExpression = string.Join(' ', options.Expression);
|
if (token.Value.StartsWith("--", StringComparison.Ordinal))
|
||||||
var inputInterpreter = new InputInterpreter();
|
|
||||||
|
|
||||||
Initializer.InitializeAll(inputInterpreter);
|
|
||||||
inputInterpreter.RegisterFunction(new PrintVariables(inputInterpreter));
|
|
||||||
|
|
||||||
if(!options.Verbose)
|
|
||||||
{
|
{
|
||||||
CSMic.Interpreter.Errors.errorStream = TextWriter.Null;
|
result.AddError($"Unrecognized command or argument '{token.Value}'.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var rootCommand = new RootCommand("CSMic command line client")
|
||||||
|
{
|
||||||
|
verboseOption,
|
||||||
|
interactiveOption,
|
||||||
|
expressionArgument
|
||||||
|
};
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
|
rootCommand.SetAction(parseResult =>
|
||||||
{
|
{
|
||||||
// console mode
|
var options = new Options
|
||||||
bool exitRequested = false;
|
|
||||||
|
|
||||||
Console.CancelKeyPress += (_, e) =>
|
|
||||||
{
|
|
||||||
e.Cancel = true;
|
|
||||||
exitRequested = true;
|
|
||||||
Console.WriteLine();
|
|
||||||
};
|
|
||||||
|
|
||||||
while (!exitRequested)
|
|
||||||
{
|
|
||||||
Console.Write("csmic> ");
|
|
||||||
|
|
||||||
fullExpression = Console.ReadLine();
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(fullExpression))
|
|
||||||
{
|
|
||||||
Console.WriteLine();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Calculate(inputInterpreter, options, fullExpression);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// one-shot mode
|
|
||||||
Calculate(inputInterpreter, options, fullExpression);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.WithNotParsed<Options>(options =>
|
|
||||||
{
|
{
|
||||||
|
Verbose = parseResult.GetValue(verboseOption),
|
||||||
|
Interactive = parseResult.GetValue(interactiveOption),
|
||||||
|
Expression = parseResult.GetValue(expressionArgument) ?? Array.Empty<string>()
|
||||||
|
};
|
||||||
|
|
||||||
});
|
return Run(options);
|
||||||
|
});
|
||||||
|
|
||||||
|
return rootCommand.Parse(args).Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int Run(Options options)
|
||||||
|
{
|
||||||
|
var fullExpression = string.Join(' ', options.Expression);
|
||||||
|
var inputInterpreter = new InputInterpreter();
|
||||||
|
|
||||||
|
Initializer.InitializeAll(inputInterpreter);
|
||||||
|
inputInterpreter.RegisterFunction(new PrintVariables(inputInterpreter));
|
||||||
|
|
||||||
|
if(!options.Verbose)
|
||||||
|
{
|
||||||
|
CSMic.Interpreter.Errors.errorStream = TextWriter.Null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(fullExpression) || options.Interactive)
|
||||||
|
{
|
||||||
|
// console mode
|
||||||
|
bool exitRequested = false;
|
||||||
|
|
||||||
|
Console.CancelKeyPress += (_, e) =>
|
||||||
|
{
|
||||||
|
e.Cancel = true;
|
||||||
|
exitRequested = true;
|
||||||
|
Console.WriteLine();
|
||||||
|
};
|
||||||
|
|
||||||
|
while (!exitRequested)
|
||||||
|
{
|
||||||
|
Console.Write("csmic> ");
|
||||||
|
|
||||||
|
fullExpression = Console.ReadLine();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(fullExpression))
|
||||||
|
{
|
||||||
|
Console.WriteLine();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Calculate(inputInterpreter, options, fullExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// one-shot mode
|
||||||
|
Calculate(inputInterpreter, options, fullExpression);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression)
|
private static void Calculate(InputInterpreter inputInterpreter, Options options, string expression)
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
<PackageReference Include="System.CommandLine" Version="2.0.9" />
|
||||||
<PackageReference Include="CSMic" Version="2.1.0" />
|
<PackageReference Include="CSMic" Version="2.1.0" />
|
||||||
<PackageReference Include="CSMic.StandardLibrary" Version="2.1.1" />
|
<PackageReference Include="CSMic.StandardLibrary" Version="2.1.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue