Skip to content

Commit

Permalink
New RunOnce methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
orakist committed Feb 1, 2024
1 parent cb1f53d commit 64466fe
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 51 deletions.
8 changes: 2 additions & 6 deletions src/Oaksoft.ArgumentParser/Base/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NETSTANDARD2_1
#if NETSTANDARD2_1

using System.ComponentModel;

Expand All @@ -19,4 +15,4 @@ internal static class IsExternalInit
}
}

#endif
#endif
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Base/TextColoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ private static string CloseNestedPastelStrings(string input, Color color, ColorP

return closedString;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,3 @@ private void BuildVerbosityOption()
return string.IsNullOrWhiteSpace(description) ? null : description;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public override Exception ToException(params object[] values)
With(values);
return new OptionBuilderException(this);
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/Builder/BuilderErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ internal static class BuilderErrors
public static readonly ErrorInfo UnableToSuggestAlias = new(
$"{Name}.{nameof(UnableToSuggestAlias)}",
$"Unable to suggest alias for the option '{{0}}'! Use '{nameof(OptionExtensions.AddAliases)}(...)' to set aliases of the option.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public OptionBuilderException(IErrorMessage error)
{
Error = error;
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/ErrorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ public static Exception ToException(this ErrorInfo error, params object[] values
{
return error.With(values).ToException();
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/ErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ private string InitializeMessage()

return _message;
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/IErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public interface IErrorMessage
/// OptionName of the error message
/// </summary>
string? OptionName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public OptionParserException(ErrorMessage error)
{
Error = error;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public override Exception ToException(params object[] values)
With(values);
return new OptionParserException(this);
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/Parser/ParserErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ internal static class ParserErrors
public static readonly ErrorInfo ListPredicateFailure = new(
$"{Name}.{nameof(ListPredicateFailure)}",
"Option value list validation failed. Value(s): {0}");
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Errors/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ public TValue GetOrThrow(string optionName)
public static implicit operator Result<TValue>(TValue value) => new(value);

public static implicit operator Result<TValue>(ErrorMessage error) => new(error);
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Options/BaseValueOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,4 @@ public override void Clear()
_valueTokens.Clear();
_inputValues.Clear();
}
}
}
2 changes: 1 addition & 1 deletion src/Oaksoft.ArgumentParser/Options/IBaseOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ public interface IBaseOption
/// Indicates hidden option
/// </summary>
bool IsHidden { get; }
}
}
108 changes: 86 additions & 22 deletions src/Oaksoft.ArgumentParser/Parser/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void Run(Action<TOptions> callback, params string[] args)
});
}

public void Run(Action<IArgumentParser<TOptions>, TOptions> callback, params string[] args)
public void Run(Action<IArgumentParser, TOptions> callback, params string[] args)
{
RunInner("Type the options or type 'q' to quit; press enter.", args, () =>
{
Expand All @@ -84,15 +84,76 @@ public void Run(string? comment, Action<TOptions> callback, params string[] args
});
}

public void Run(string? comment, Action<IArgumentParser<TOptions>, TOptions> callback, params string[] args)
public void Run(string? comment, Action<IArgumentParser, TOptions> callback, params string[] args)
{
RunInner(comment, args, () =>
{
callback.Invoke(this, _appOptions);
});
}

public void RunOnce(Action<TOptions> callback, params string[] args)
{
RunOnceInner("Type the options; press enter.", args, () =>
{
if (IsValid && !IsHelpOption && !IsVersionOption)
{
callback.Invoke(_appOptions);
}
});
}

public void RunOnce(Action<IArgumentParser, TOptions> callback, params string[] args)
{
RunOnceInner("Type the options; press enter.", args, () =>
{
callback.Invoke(this, _appOptions);
});
}

public void RunOnce(string? comment, Action<TOptions> callback, params string[] args)
{
RunOnceInner(comment, args, () =>
{
if (IsValid && !IsHelpOption && !IsVersionOption)
{
callback.Invoke(_appOptions);
}
});
}

public void RunOnce(string? comment, Action<IArgumentParser, TOptions> callback, params string[] args)
{
RunOnceInner(comment, args, () =>
{
callback.Invoke(this, _appOptions);
});
}

private void RunInner(string? comment, string[]? args, Action callback)
{
WriteComment(comment);

args = InitializeArguments(args);

while (!IsQuitArgument(args))
{
EvaluateArguments(args, callback);

args = GetInputArguments();
}
}

private void RunOnceInner(string? comment, string[]? args, Action callback)
{
WriteComment(comment);

args = InitializeArguments(args);

EvaluateArguments(args, callback);
}

private static void WriteComment(string? comment)
{
if (!string.IsNullOrWhiteSpace(comment))
{
Expand All @@ -101,7 +162,10 @@ private void RunInner(string? comment, string[]? args, Action callback)
Console.WriteLine(comment);
}
}
}

private string[] InitializeArguments(string[]? args)
{
if (args?.Length > 0)
{
if (!CommandLine.DisableConsoleOutput)
Expand All @@ -114,32 +178,32 @@ private void RunInner(string? comment, string[]? args, Action callback)
args = GetInputArguments();
}

while (!IsQuitArgument(args))
{
ParseTokens(args);
return args;
}

private void EvaluateArguments(string[] args, Action callback)
{
ParseTokens(args);

try
try
{
if (!IsEmpty)
{
if (!IsEmpty)
{
callback.Invoke();
}
callback.Invoke();
}
catch (Exception ex)
{
var error = new ErrorInfo($"{ParserErrors.Name}.RunCallbackError", ex.Message);
_errors.Add(error.WithException(ex));
}
catch (Exception ex)
{
var error = new ErrorInfo($"{ParserErrors.Name}.RunCallbackError", ex.Message);
_errors.Add(error.WithException(ex));

if (!Settings.AutoPrintErrors)
{
throw;
}
if (!Settings.AutoPrintErrors)
{
throw;
}

AutoPrintErrorText();

args = GetInputArguments();
}

AutoPrintErrorText();
}

private static bool IsQuitArgument(string[] args)
Expand Down
48 changes: 41 additions & 7 deletions src/Oaksoft.ArgumentParser/Parser/IArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,23 @@ public interface IArgumentParser<out TOptions> : IArgumentParser
TOptions Parse(params string[] args);

/// <summary>
/// Runs a parser loop to parse given arguments. At each step of the loop,
/// Runs a parser loop to parse given 'args' or console inputs. At each step of the loop,
/// after arguments are parsed successfully, invokes the given callback function by passing parsed option values.
/// </summary>
/// <param name="callback">Parser invokes this callback after each successful parsing.</param>
/// <param name="args">Arguments to parse</param>
void Run(Action<TOptions> callback, params string[] args);

/// <summary>
/// Runs a parser loop to parse given arguments. At each step of the loop,
/// Runs a parser loop to parse given 'args' or console inputs. At each step of the loop,
/// after arguments are parsed, it invokes the given callback delegate by passing parsed option values.
/// </summary>
/// <param name="callback">Parser invokes this callback after each parsing.</param>
/// <param name="args">Arguments to parse</param>
void Run(Action<IArgumentParser<TOptions>, TOptions> callback, params string[] args);
void Run(Action<IArgumentParser, TOptions> callback, params string[] args);

/// <summary>
/// Runs a parser loop to parse given arguments. At each step of the loop,
/// Runs a parser loop to parse given 'args' or console inputs. At each step of the loop,
/// after arguments are parsed successfully, invokes the given callback function by passing parsed option values.
/// </summary>
/// <param name="comment">Prompted text</param>
Expand All @@ -173,11 +173,45 @@ public interface IArgumentParser<out TOptions> : IArgumentParser
void Run(string? comment, Action<TOptions> callback, params string[] args);

/// <summary>
/// Runs a parser loop to parse given arguments. At each step of the loop,
/// Runs a parser loop to parse given 'args' or console inputs. At each step of the loop,
/// after arguments are parsed, it invokes the given callback delegate by passing parsed option values.
/// </summary>
/// <param name="comment">Prompted text</param>
/// <param name="callback">Parser invokes this callback after each parsing.</param>
/// <param name="args">Arguments to parse</param>
void Run(string? comment, Action<IArgumentParser<TOptions>, TOptions> callback, params string[] args);
}
void Run(string? comment, Action<IArgumentParser, TOptions> callback, params string[] args);

/// <summary>
/// Runs parser only once to parse given 'args' or console inputs. After arguments are parsed successfully,
/// invokes the given callback function by passing parsed option values.
/// </summary>
/// <param name="callback">Parser invokes this callback after each successful parsing.</param>
/// <param name="args">Arguments to parse</param>
void RunOnce(Action<TOptions> callback, params string[] args);

/// <summary>
/// Runs parser only once to parse given 'args' or console inputs. After arguments are parsed,
/// it invokes the given callback delegate by passing parsed option values.
/// </summary>
/// <param name="callback">Parser invokes this callback after each parsing.</param>
/// <param name="args">Arguments to parse</param>
void RunOnce(Action<IArgumentParser, TOptions> callback, params string[] args);

/// <summary>
/// Runs parser only once to parse given 'args' or console inputs. After arguments are parsed successfully,
/// invokes the given callback function by passing parsed option values.
/// </summary>
/// <param name="comment">Prompted text</param>
/// <param name="callback">Parser invokes this callback after each successful parsing.</param>
/// <param name="args">Arguments to parse</param>
void RunOnce(string? comment, Action<TOptions> callback, params string[] args);

/// <summary>
/// Runs parser only once to parse given 'args' or console inputs. After arguments are parsed,
/// it invokes the given callback delegate by passing parsed option values.
/// </summary>
/// <param name="comment">Prompted text</param>
/// <param name="callback">Parser invokes this callback after each parsing.</param>
/// <param name="args">Arguments to parse</param>
void RunOnce(string? comment, Action<IArgumentParser, TOptions> callback, params string[] args);
}
2 changes: 1 addition & 1 deletion test/Oaksoft.ArgumentParser.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static void Main(string[] args)
}
}

private static void EvaluateOptions(IArgumentParser<CalculatorOptions> parser, CalculatorOptions options)
private static void EvaluateOptions(IArgumentParser parser, CalculatorOptions options)
{
if (!parser.IsParsed || options.Operator == null)
return;
Expand Down
2 changes: 1 addition & 1 deletion test/Oaksoft.ArgumentParser.Tutorial/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,4 @@ internal static class Arguments
public static string[] UnknownTypeArgs { get; } = { "-l", "5", "-r", "1", "-c", "abc" };
public static string[] NegativeNumberArgs { get; } = { "-l", "-5", "-r", "-1", "-c", "add" };
public static string[] ParenthesesNumberArgs { get; } = { "-l", "(5.1)", "-r", "(2.4)", "-c", "add" };
}
}

0 comments on commit 64466fe

Please sign in to comment.