diff --git a/Examples/CaretDiagnostics/Program.cs b/Examples/CaretDiagnostics/Program.cs index 69b74d5..9c5848d 100644 --- a/Examples/CaretDiagnostics/Program.cs +++ b/Examples/CaretDiagnostics/Program.cs @@ -47,15 +47,10 @@ public static void Main(string[] args) // Write an entry to the log that contains the things // we would like to print. - log.Log( - new LogEntry( - Severity.Error, - "hello world", - new MarkupNode[] - { - new Text("look at this beautiful error message!"), - new HighlightedSource(highlightRegion, focusRegion) - })); + log.Error( + "hello world", + new Text("look at this beautiful error message!"), + new HighlightedSource(highlightRegion, focusRegion)); } private const string SourceCode = @"public static class Program { diff --git a/Examples/FormattedList/Program.cs b/Examples/FormattedList/Program.cs index 1e0dd82..61e8a15 100644 --- a/Examples/FormattedList/Program.cs +++ b/Examples/FormattedList/Program.cs @@ -54,27 +54,22 @@ public static void Main(string[] args) // Write an entry to the log that contains the things // we would like to print. - log.Log( - new LogEntry( - Severity.Info, - new MarkupNode[] - { - // Create a title, underline it and make it green because why not. - new Title( - DecorationSpan.MakeUnderlined( - new ColorSpan("Hello world", Colors.Green))), + log.Info( + // Create a title, underline it and make it green because why not. + new Title( + DecorationSpan.MakeUnderlined( + new ColorSpan("Hello world", Colors.Green))), - // Create a word-wrapped bulleted list with a uniform margin of four. - new WrapBox( - new BulletedList( - LoremIpsum - .Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries) - .Select(QuoteString) - .ToArray(), - true), - WrappingStrategy.Word, - 4) - })); + // Create a word-wrapped bulleted list with a uniform margin of four. + new WrapBox( + new BulletedList( + LoremIpsum + .Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries) + .Select(QuoteString) + .ToArray(), + true), + WrappingStrategy.Word, + 4)); } private static MarkupNode QuoteString(string text) diff --git a/Examples/LoycInterop/Program.cs b/Examples/LoycInterop/Program.cs index ba3bea0..3d7ff92 100644 --- a/Examples/LoycInterop/Program.cs +++ b/Examples/LoycInterop/Program.cs @@ -20,12 +20,9 @@ public static void Main(string[] args) // First, acquire a terminal log. We'll configure it to // turn everything it sees into a diagnostic. - var log = new TransformLog( - TerminalLog.Acquire(), - new Func[] - { - MakeDiagnostic - }); + var log = TerminalLog + .Acquire() + .WithTransform(MakeDiagnostic); // Create a message sink that redirects messages to the log. var messageSink = new PixieMessageSink(log); diff --git a/Examples/ParseOptions/Program.cs b/Examples/ParseOptions/Program.cs index 3eb6f0e..dbf5fd1 100644 --- a/Examples/ParseOptions/Program.cs +++ b/Examples/ParseOptions/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -16,7 +17,7 @@ public static class Program // This option is a simple flag. It takes no arguments. private static readonly FlagOption optimizeFastFlag = - new FlagOption(OptionForm.Short("Ofast")) + Option.Flag("-Ofast") .WithCategory("Optimization options") .WithDescription("Enable aggressive optimizations."); @@ -24,54 +25,40 @@ public static class Program // different forms: // -h and --help. private static readonly FlagOption helpFlag = - FlagOption.CreateFlagOption( - new OptionForm[] - { - OptionForm.Short("h"), - OptionForm.Long("help") - }) + Option.Flag("-h", "--help") .WithDescription( "Print a description of the options understood."); // This option has both a positive and a negative form: // -fsyntax-only and -fno-syntax-only. private static readonly FlagOption syntaxOnlyFlag = - new FlagOption( - OptionForm.Short("fsyntax-only"), - OptionForm.Short("fno-syntax-only"), - false) + Option.Toggle( + "-fsyntax-only", + "-fno-syntax-only") .WithDescription("Check the code for syntax errors only."); // This option takes zero or more strings as arguments. private static readonly SequenceOption filesOption = - SequenceOption.CreateStringOption( - OptionForm.Long("files")) + Option.StringSequence("--files") .WithDescription("Consume files as input.") - .WithParameters( - new OptionParameter[] - { - new SymbolicOptionParameter("file", true) - }); + .WithParameter("file"); // This option takes a 32-bit signed integer as argument. private static readonly ValueOption optimizeOption = - ValueOption.CreateInt32Option( - OptionForm.Short("O"), - 0) + Option.Int32WithDefault( + 0, + "-O") .WithCategory("Optimization options") .WithDescription("Pick an optimization level.") - .WithParameter(new SymbolicOptionParameter("n")); + .WithParameter("n"); // This option takes a 32-bit signed integer as argument. // It also happens to have two forms. private static readonly ValueOption maxErrorsOption = - ValueOption.CreateInt32Option( - new OptionForm[] - { - OptionForm.Short("fmax-errors"), - OptionForm.Short("ferror-limit") - }, - 0) + Option.Int32WithDefault( + 0, + "-fmax-errors", + "-ferror-limit") .WithDescription( new Sequence( new MarkupNode[] @@ -80,22 +67,15 @@ public static class Program new SymbolicOptionParameter("n").Representation, "." })) - .WithParameter(new SymbolicOptionParameter("n")); - - private static OptionSet parsedOptions; - + .WithParameter("n"); public static void Main(string[] args) { // First, acquire a terminal log. You should acquire // a log once and then re-use it in your application. - ILog log = TerminalLog.Acquire(); - - log = new TransformLog( - log, - new Func[] - { - MakeDiagnostic - }); + ILog log = TerminalLog + .Acquire() + .WithDiagnostics("program") + .WithWordWrap(); var allOptions = new Option[] { @@ -107,64 +87,87 @@ public static void Main(string[] args) maxErrorsOption }; - var parser = new GnuOptionSetParser( + // `filesOption` plays two roles in this example: + // it can be spelled explicitly as `--files`, + // and it is also the option that should consume bare + // positional arguments such as `a.txt`. + // + // The third constructor argument picks the form Pixie should + // mention when it needs to describe that positional behavior + // in help or diagnostics. + var commandLine = new CommandLine( allOptions, filesOption, filesOption.Forms[0]); - parsedOptions = parser.Parse(args, log); + var parsedOptions = commandLine.Parse(args, log); + + if (!parsedOptions.IsSuccess || parsedOptions.WasHandled) + { + return; + } if (!parsedOptions.GetValue(syntaxOnlyFlag)) { - log.Log( - new LogEntry( - Severity.Info, - new BulletedList( - allOptions - .Select(TypesetParsedOption) - .ToArray(), - true))); + log.Info(RenderParsedOptions(allOptions, parsedOptions)); } } - private static MarkupNode TypesetParsedOption(Option opt) + private static MarkupNode RenderParsedOptions( + IReadOnlyList