Skip to content

Commit 3976f5c

Browse files
committed
Added a handler to the CLIHandler for already parsed arguments. Added a method on Arguments for checking flags. Updated unit tests accordingly.
1 parent 323f390 commit 3976f5c

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

PointerPlace.CLI/PointerPlace.CLI.Test/TestArguments.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void TestTrailingFlag()
6565
var goodbyeFlag = arguments["GoodbyeFlag"];
6666

6767
Assert.IsTrue(goodbyeFlag.IsFlag);
68+
Assert.IsTrue(arguments.CheckFlag("GoodbyeFlag"));
6869
}
6970

7071
[TestMethod]
@@ -83,6 +84,7 @@ public void TestConcurrentFlag()
8384
var imAFlag = arguments["ImAFlag"];
8485

8586
Assert.IsTrue(imAFlag.IsFlag);
87+
Assert.IsTrue(arguments.CheckFlag("ImAFlag"));
8688
}
8789

8890
[TestMethod]

PointerPlace.CLI/PointerPlace.CLI/Arguments.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,19 @@ public string FormatArgumentName(string argumentName)
152152
return FormatArgumentName(argumentName, EscapeChar);
153153
}
154154

155+
/// <summary>
156+
/// Checks this arguments dictionary for the flag, returning true if the flag is set
157+
/// </summary>
158+
/// <param name="flagName">The name of the flag to check for</param>
159+
/// <returns>A bool indicating whether or not the flag is set</returns>
160+
public bool CheckFlag(string flagName)
161+
{
162+
var argument = this[flagName];
163+
if (argument != null)
164+
return argument.IsFlag;
165+
166+
return false;
167+
}
168+
155169
}
156170
}

PointerPlace.CLI/PointerPlace.CLI/CLIHandler.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,28 @@ internal struct ArgumentInfo
131131
public static int HandleMain(string[] args, char escapeCharacter = DefaultEscapeChar, bool ignoreCase = true)
132132
{
133133
// Parse out the arguments
134-
var arguments = Arguments.FromArgs(args, escapeCharacter, ignoreCase);
134+
var parsedArgs = Arguments.FromArgs(args, escapeCharacter, ignoreCase);
135135

136+
return HandleMain(parsedArgs, escapeCharacter, ignoreCase);
137+
}
138+
139+
/// <summary>
140+
/// The function for handling "Main". This is the function to call for using
141+
/// the CLI to handle the program
142+
/// </summary>
143+
/// <param name="args">The arguments passed to the "Main" function of a program, parsed into Arguments</param>
144+
/// <param name="escapeCharacter">The escape character used to identify the argument names in the arguments</param>
145+
/// <param name="ignoreCase">Whether or not commands and arguments are to be treated case insensitive. Defaults to true.</param>
146+
/// <returns>An int which is the exit code</returns>
147+
public static int HandleMain(Arguments args, char escapeCharacter = DefaultEscapeChar, bool ignoreCase = true)
148+
{
136149
// Try manual commands first
137-
var commandResult = ExecuteManualCommand(arguments, ignoreCase);
150+
var commandResult = ExecuteManualCommand(args, ignoreCase);
138151

139152
// If we didn't execute a manual command
140153
// try the attribute commands next
141154
if (commandResult.HasValue == false)
142-
commandResult = ExecuteCommand(arguments, Assembly.GetCallingAssembly(), ignoreCase);
155+
commandResult = ExecuteCommand(args, Assembly.GetCallingAssembly(), ignoreCase);
143156

144157
// If a command was executed, return its exit code
145158
if (commandResult.HasValue)

0 commit comments

Comments
 (0)