From b36efadefdafbeec7589b46da429fc2fd09cbea8 Mon Sep 17 00:00:00 2001 From: wagesj45 Date: Tue, 30 Jun 2026 02:53:33 -0500 Subject: [PATCH] Fixed for AOT publishing on linux. --- src/cs-mic-cli/Options.cs | 14 +--- src/cs-mic-cli/Program.cs | 137 ++++++++++++++++++++----------- src/cs-mic-cli/cs-mic-cli.csproj | 2 +- 3 files changed, 92 insertions(+), 61 deletions(-) diff --git a/src/cs-mic-cli/Options.cs b/src/cs-mic-cli/Options.cs index 7859b45..edbd808 100644 --- a/src/cs-mic-cli/Options.cs +++ b/src/cs-mic-cli/Options.cs @@ -1,21 +1,11 @@ -using CommandLine; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace cs_mic_cli +namespace cs_mic_cli { internal class Options { - [Option('v', "verbose", HelpText = "Displays error messages from the parser if a soft error occurs.")] public bool Verbose { get; set; } - [Option('i', "interactive", HelpText = "Starts an interactive session.")] public bool Interactive { get; set; } - [Value(0)] - public required IEnumerable Expression { get; set; } + public IEnumerable Expression { get; set; } = Array.Empty(); } } diff --git a/src/cs-mic-cli/Program.cs b/src/cs-mic-cli/Program.cs index abed934..ca6ebaf 100644 --- a/src/cs-mic-cli/Program.cs +++ b/src/cs-mic-cli/Program.cs @@ -1,66 +1,107 @@ -using CommandLine; -using cs_mic_cli.Functions; +using cs_mic_cli.Functions; using CSMic; using CSMic.StandardLibrary; +using System.CommandLine; namespace cs_mic_cli { internal class Program { - static void Main(string[] args) + static int Main(string[] args) { - CommandLine.Parser.Default.ParseArguments(args) - .WithParsed(options => + var verboseOption = new Option("--verbose", "-v") + { + Description = "Displays error messages from the parser if a soft error occurs." + }; + var interactiveOption = new Option("--interactive", "-i") + { + Description = "Starts an interactive session." + }; + var expressionArgument = new Argument("expression") + { + Arity = ArgumentArity.ZeroOrMore, + Description = "Expression to evaluate." + }; + expressionArgument.Validators.Add(result => + { + foreach (var token in result.Tokens) { - var fullExpression = string.Join(' ', options.Expression); - var inputInterpreter = new InputInterpreter(); - - Initializer.InitializeAll(inputInterpreter); - inputInterpreter.RegisterFunction(new PrintVariables(inputInterpreter)); - - if(!options.Verbose) + if (token.Value.StartsWith("--", StringComparison.Ordinal)) { - 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) - { - // 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); - } - }) - .WithNotParsed(options => + rootCommand.SetAction(parseResult => + { + var options = new Options { + Verbose = parseResult.GetValue(verboseOption), + Interactive = parseResult.GetValue(interactiveOption), + Expression = parseResult.GetValue(expressionArgument) ?? Array.Empty() + }; - }); + 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) diff --git a/src/cs-mic-cli/cs-mic-cli.csproj b/src/cs-mic-cli/cs-mic-cli.csproj index c888b75..bb71e68 100644 --- a/src/cs-mic-cli/cs-mic-cli.csproj +++ b/src/cs-mic-cli/cs-mic-cli.csproj @@ -11,7 +11,7 @@ - +