public static void Parse(IEnumerable<string> arguments, Action<CommandLineSyntax> syntaxAction) { if (arguments == null) throw new ArgumentNullException("arguments"); if (syntaxAction == null) throw new ArgumentNullException("syntaxAction"); var syntax = new CommandLineSyntax(arguments); var showHelp = false; try { syntax.DefineQualifier("?|help", ref showHelp, "Shows this help page"); syntaxAction(syntax); } catch (CommandLineSyntaxException ex) { ShowErrorAndExit(ex, -1); } if (showHelp) ShowHelpAndExit(syntax, 0); try { syntax.Validate(); } catch (CommandLineSyntaxException ex) { ShowError(ex); ShowHelpAndExit(syntax, -1); } }
public void DetectsDuplicateQualifiers() { var commandLine = "-a -b -a"; var exception = Assert.Throws<CommandLineSyntaxException>(() => { var syntax = new CommandLineSyntax(commandLine); var arg1 = false; var arg2 = false; syntax.DefineQualifier("a|arg1", ref arg1, string.Empty); syntax.DefineQualifier("b|arg2", ref arg2, string.Empty); syntax.Validate(); }); Assert.Equal("qualifier a|arg1 is specified multiple times", exception.Message); }
public void ParsesArrayQualifiers() { var commandLine = "-a x -b -a y"; var syntax = new CommandLineSyntax(commandLine); var arg1 = new string[0]; var arg2 = false; syntax.DefineQualifier("a|arg 1", ref arg1, string.Empty); syntax.DefineQualifier("b|arg 2", ref arg2, string.Empty); syntax.Validate(); var expected1 = new[] { "x", "y" }; var actual1 = arg1; Assert.Equal(expected1, actual1); var expected2 = true; var actual2 = arg2; Assert.Equal(expected2, actual2); }
public void DetectsNonExistingQualifier() { var commandLine = "-e -d"; var exception = Assert.Throws<CommandLineSyntaxException>(() => { var syntax = new CommandLineSyntax(commandLine); var exists = false; syntax.DefineQualifier("e|exists", ref exists, "Some qualifier"); syntax.Validate(); }); Assert.Equal("invalid qualifier -d", exception.Message); }