Skip to content

Commit

Permalink
Updating PicoArgs to v1.6.1, checking input params
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed Jul 17, 2024
1 parent 7a768f0 commit 05b7643
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions PicoArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace PicoArgs_dotnet;
/* PICOARGS_DOTNET - a tiny command line argument parser for .NET
https://github.com/lookbusy1344/PicoArgs-dotnet
Version 1.6.0 - 17 Jul 2024
Version 1.6.1 - 17 Jul 2024
Example usage:
Expand Down Expand Up @@ -40,7 +40,7 @@ public class PicoArgs(IEnumerable<string> args, bool recogniseEquals = true)
/// </summary>
public bool Contains(params string[] options)
{
ValidateParams(options);
ValidatePossibleParams(options);
CheckFinished();

// no args left
Expand Down Expand Up @@ -72,7 +72,7 @@ public bool Contains(params string[] options)
/// </summary>
public string[] GetMultipleParams(params string[] options)
{
ValidateParams(options);
ValidatePossibleParams(options);
CheckFinished();

var result = new List<string>();
Expand Down Expand Up @@ -100,7 +100,7 @@ public string[] GetMultipleParams(params string[] options)
/// </summary>
public string? GetParamOpt(params string[] options)
{
ValidateParams(options);
ValidatePossibleParams(options);
CheckFinished();

return GetParamInternal(options);
Expand Down Expand Up @@ -207,9 +207,9 @@ private void CheckFinished()
}

/// <summary>
/// Check options are valid, eg -a or --action, but not -aa (already expanded) or ---action or --a
/// Check options are valid for Contains() or GetParam(), eg -a or --action, but not -aa (already expanded) or ---action or --a
/// </summary>
private static void ValidateParams(string[] options)
private static void ValidatePossibleParams(string[] options)
{
if (options == null || options.Length == 0) {
throw new ArgumentException("Must specify at least one option", nameof(options));
Expand All @@ -223,7 +223,8 @@ private static void ValidateParams(string[] options)
if (o[1] != '-') {
// if it is longer than 2 characters, the second character must be a dash. eg -ab is invalid here (its already been expanded to -a -b)
throw new ArgumentException($"Long options must start with 2 dashes: {o}", nameof(options));
} else if (o[2] == '-') {
}
if (o[2] == '-') {
// if it is longer than 2 characters, the third character must not be a dash. eg ---a is not valid
throw new ArgumentException($"Options should not start with 3 dashes: {o}", nameof(options));
}
Expand All @@ -241,6 +242,8 @@ private static void ValidateParams(string[] options)
private static IEnumerable<KeyValue> ProcessItems(IEnumerable<string> args, bool recogniseEquals)
{
foreach (var arg in args) {
ValidateInputParam(arg);

var switches = CountCombinedSwitches(arg);
if (switches > 1) {
// split combined switches into individual switches eg "-abc" -> "-a" "-b" "-c"
Expand Down Expand Up @@ -286,6 +289,25 @@ private static int CountCombinedSwitches(string arg)
return 1;
}
}

/// <summary>
/// Validate this input param from command line. Invalid is ---something or --x. Valid options are -a or -ab or --action
/// </summary>
private static void ValidateInputParam(string arg)
{
if (arg == "-") {
// a single dash is not valid
throw new PicoArgsException(90, "Parameter should not be a single dash");
}
if (arg.StartsWith("---")) {
// eg ---something is not valid
throw new PicoArgsException(90, $"Parameter should not start with 3 dashes: {arg}");
}
if (arg.Length == 3 && arg.StartsWith("--")) {
// eg --a is not valid
throw new PicoArgsException(90, $"Long options must be 2 characters or more: {arg}");
}
}
}

/// <summary>
Expand Down

0 comments on commit 05b7643

Please sign in to comment.