Skip to content

Commit

Permalink
Fix issue #409 to avoid IOException break in Debug mode in WPF app. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-hassan authored Mar 9, 2020
1 parent de94a89 commit 30539a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/CommandLine/ParserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,29 @@ public ParserSettings()
autoHelp = true;
autoVersion = true;
parsingCulture = CultureInfo.InvariantCulture;
maximumDisplayWidth = GetWindowWidth();
}

private int GetWindowWidth()
{

#if !NET40
if (Console.IsOutputRedirected) return DefaultMaximumLength;
#endif
var width = 1;
try
{
maximumDisplayWidth = Console.WindowWidth;
if (maximumDisplayWidth < 1)
width = Console.WindowWidth;
if (width < 1)
{
maximumDisplayWidth = DefaultMaximumLength;
width = DefaultMaximumLength;
}
}
catch (IOException)
}
catch (Exception e) when (e is IOException || e is PlatformNotSupportedException || e is ArgumentOutOfRangeException)
{
maximumDisplayWidth = DefaultMaximumLength;
width = DefaultMaximumLength;
}
return width;
}

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -892,5 +892,17 @@ public void Parse_default_verb_with_empty_name()
Assert.True(args.TestValue);
});
}
//Fix Issue #409 for WPF
[Fact]
public void When_HelpWriter_is_null_it_should_not_fire_exception()
{
// Arrange

//Act
var sut = new Parser(config => config.HelpWriter = null);
sut.ParseArguments<Simple_Options>(new[] {"--dummy"});
//Assert
sut.Settings.MaximumDisplayWidth.Should().Be(80);
}
}
}

0 comments on commit 30539a5

Please sign in to comment.