-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to show command line parser help text in console when "--…
…help" argument is specified
- Loading branch information
Showing
4 changed files
with
146 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
namespace AnnoDesigner | ||
{ | ||
/// <summary> | ||
/// Console manager class for showing console when help text should be shown. | ||
/// </summary> | ||
/// <remarks> | ||
/// Source: https://stackoverflow.com/a/718505 | ||
/// </remarks> | ||
[SuppressUnmanagedCodeSecurity] | ||
public static class ConsoleManager | ||
{ | ||
private const string Kernel32_DllName = "kernel32.dll"; | ||
|
||
[DllImport(Kernel32_DllName)] | ||
private static extern bool AllocConsole(); | ||
|
||
[DllImport(Kernel32_DllName)] | ||
private static extern bool FreeConsole(); | ||
|
||
[DllImport(Kernel32_DllName)] | ||
private static extern IntPtr GetConsoleWindow(); | ||
|
||
[DllImport(Kernel32_DllName)] | ||
private static extern int GetConsoleOutputCP(); | ||
|
||
public static bool HasConsole | ||
{ | ||
get { return GetConsoleWindow() != IntPtr.Zero; } | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new console instance if the process is not attached to a console already. | ||
/// </summary> | ||
public static void Show() | ||
{ | ||
if (!HasConsole) | ||
{ | ||
AllocConsole(); | ||
InvalidateOutAndError(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown. | ||
/// </summary> | ||
public static void Hide() | ||
{ | ||
if (HasConsole) | ||
{ | ||
SetOutAndErrorNull(); | ||
FreeConsole(); | ||
} | ||
} | ||
|
||
public static void Toggle() | ||
{ | ||
if (HasConsole) | ||
{ | ||
Hide(); | ||
} | ||
else | ||
{ | ||
Show(); | ||
} | ||
} | ||
|
||
static void InvalidateOutAndError() | ||
{ | ||
var type = typeof(Console); | ||
|
||
type.GetField("_out", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) | ||
.SetValue(null, null); | ||
|
||
type.GetField("_error", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) | ||
.SetValue(null, null); | ||
|
||
type.GetMethod("InitializeStdOutError", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic) | ||
.Invoke(null, new object[] { true }); | ||
} | ||
|
||
static void SetOutAndErrorNull() | ||
{ | ||
Console.SetOut(TextWriter.Null); | ||
Console.SetError(TextWriter.Null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters