Skip to content

Commit

Permalink
Updating PicoArgs to v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lookbusy1344 committed Jul 17, 2024
1 parent cefbf2b commit 7a768f0
Showing 1 changed file with 52 additions and 28 deletions.
80 changes: 52 additions & 28 deletions PicoArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ namespace PicoArgs_dotnet;
/* PICOARGS_DOTNET - a tiny command line argument parser for .NET
https://github.com/lookbusy1344/PicoArgs-dotnet
Version 1.5.1 - 09 Jul 2024
Version 1.6.0 - 17 Jul 2024
Example usage:
var pico = new PicoArgs(args);
bool verbose = pico.Contains("-v", "--verbose"); // true if any of these switches are present
string? pattern = pico.GetParamOpt("-t", "--pattern") ?? "*.txt"; // optional parameter
string requiredpath = pico.GetParam("-p", "--path"); // mandatory parameter, throws if not present
string? patternOpt = pico.GetParamOpt("-t", "--pattern"); // optional parameter
string pattern = pico.GetParamOpt("-t", "--pattern") ?? "*.txt"; // optional parameter with default
string requirePath = pico.GetParam("-p", "--path"); // mandatory parameter, throws if not present
string[] files = pico.GetMultipleParams("-f", "--file"); // multiple parameters returned in string[]
string command = pico.GetCommand(); // first parameter, throws if not present
string? commandopt = pico.GetCommandOpt(); // first parameter, null if not present
string? commandOpt = pico.GetCommandOpt(); // first parameter, null if not present
pico.Finished(); // We are done. Throw if there are any unused parameters
Expand All @@ -39,21 +40,15 @@ public class PicoArgs(IEnumerable<string> args, bool recogniseEquals = true)
/// </summary>
public bool Contains(params string[] options)
{
ValidateParams(options);
CheckFinished();
if (options == null || options.Length == 0) {
throw new ArgumentException("Must specify at least one option", nameof(options));
}

// no args left
if (args.Count == 0) {
return false;
}

foreach (var o in options) {
if (!o.StartsWith('-')) {
throw new ArgumentException("Must start with -", nameof(options));
}

var index = args.FindIndex(a => a.Key == o);
if (index >= 0) {
// if this argument has a value, throw eg "--verbose=true" when we just expected "--verbose"
Expand All @@ -77,10 +72,12 @@ public bool Contains(params string[] options)
/// </summary>
public string[] GetMultipleParams(params string[] options)
{
ValidateParams(options);
CheckFinished();

var result = new List<string>();
while (true) {
var s = GetParamOpt(options);
var s = GetParamInternal(options); // Internal call, because we have already validated the options
if (s == null) {
break; // nothing else found, break out of loop
}
Expand All @@ -93,32 +90,31 @@ public string[] GetMultipleParams(params string[] options)

/// <summary>
/// Get a string value from the command line, throws is not present
/// eg -a "value" or --foldera "value"
/// eg -a "value" or --folder "value"
/// </summary>
public string GetParam(params string[] options) => GetParamOpt(options) ?? throw new PicoArgsException(10, $"Expected value for \"{string.Join(", ", options)}\"");

/// <summary>
/// Get a string value from the command line, or null if not present
/// eg -a "value" or --foldera "value"
/// eg -a "value" or --folder "value"
/// </summary>
public string? GetParamOpt(params string[] options)
{
ValidateParams(options);
CheckFinished();
if (options == null || options.Length == 0) {
throw new ArgumentException("Must specify at least one option", nameof(options));
}

return GetParamInternal(options);
}

/// <summary>
/// Internal version of GetParamOpt, which does not check for valid options
/// </summary>
private string? GetParamInternal(string[] options)
{
if (args.Count == 0) {
return null;
}

// check all options are switches
foreach (var o in options) {
if (!o.StartsWith('-')) {
throw new ArgumentException("Must start with -", nameof(options));
}
}

// do we have this switch on command line?
var index = args.FindIndex(a => options.Contains(a.Key));
if (index == -1) {
Expand All @@ -140,16 +136,16 @@ public string[] GetMultipleParams(params string[] options)
}

// grab and check the next parameter
var seconditem = args[index + 1];
if (seconditem.Value != null) {
throw new PicoArgsException(30, $"Cannot identify value for param \"{item.Key}\", followed by \"{seconditem.Key}\" and \"{seconditem.Value}\"");
var secondItem = args[index + 1];
if (secondItem.Value != null) {
throw new PicoArgsException(30, $"Cannot identify value for param \"{item.Key}\", followed by \"{secondItem.Key}\" and \"{secondItem.Value}\"");
}

// consume the switch and the separate value
args.RemoveRange(index, 2);

// return the value
return seconditem.Key;
return secondItem.Key;
}

/// <summary>
Expand Down Expand Up @@ -210,6 +206,34 @@ private void CheckFinished()
}
}

/// <summary>
/// Check options are valid, eg -a or --action, but not -aa (already expanded) or ---action or --a
/// </summary>
private static void ValidateParams(string[] options)
{
if (options == null || options.Length == 0) {
throw new ArgumentException("Must specify at least one option", nameof(options));
}

foreach (var o in options) {
if (o.Length == 1 || !o.StartsWith('-')) {
throw new ArgumentException($"Options must start with a dash and be longer than 1 character: {o}", nameof(options));
}
if (o.Length > 2) {
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 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));
}
if (o.Length == 3) {
throw new ArgumentException($"Long options must be 2 characters or more: {o}", nameof(options));
}
}
}
}

/// <summary>
/// Helper function to process the command line arguments. Splits multiple switches into individual switches
/// eg -abc becomes -a -b -c
Expand Down

0 comments on commit 7a768f0

Please sign in to comment.