Skip to content

Commit

Permalink
Add header and description to mutually exclusive option group
Browse files Browse the repository at this point in the history
  • Loading branch information
yakovypg committed Jan 27, 2025
1 parent 0387e2b commit 90c691a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
14 changes: 14 additions & 0 deletions Core/NetArgumentParser/src/Options/IOptionGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace NetArgumentParser.Options;

public interface IOptionGroup<T>
where T : IOption
{
string Header { get; set; }
string Description { get; set; }
IReadOnlyList<T> Options { get; }

void AddOptions(params T[] options);
bool RemoveOption(T item);
}
30 changes: 25 additions & 5 deletions Core/NetArgumentParser/src/Options/MutuallyExclusiveOptionGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,49 @@

namespace NetArgumentParser.Options;

public sealed class MutuallyExclusiveOptionGroup<T>
public sealed class MutuallyExclusiveOptionGroup<T> : IOptionGroup<T>
where T : IOption
{
private readonly List<T> _options;
private string _header;
private string _description;

internal MutuallyExclusiveOptionGroup(IEnumerable<T>? options = null)
internal MutuallyExclusiveOptionGroup(
string header,
string description,
IEnumerable<T>? options = null)
{
_header = header;
_description = description;

_options = options is not null
? [.. options]
: [];
}

public IReadOnlyList<T> Options => _options;

public string Header
{
get => _header;
set => _header = value ?? string.Empty;
}

public string Description
{
get => _description;
set => _description = value ?? string.Empty;
}

public void AddOptions(params T[] options)
{
ExtendedArgumentNullException.ThrowIfNull(options, nameof(options));
Array.ForEach(options, _options.Add);
}

public bool RemoveOption(T option)
public bool RemoveOption(T item)
{
ExtendedArgumentNullException.ThrowIfNull(option, nameof(option));
return _options.Remove(option);
ExtendedArgumentNullException.ThrowIfNull(item, nameof(item));
return _options.Remove(item);
}
}
10 changes: 5 additions & 5 deletions Core/NetArgumentParser/src/Options/OptionGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NetArgumentParser.Options;

public sealed class OptionGroup<T>
public sealed class OptionGroup<T> : IOptionGroup<T>
where T : IOption
{
private readonly List<T> _options;
Expand Down Expand Up @@ -50,11 +50,11 @@ public void AddOptions(params T[] options)
Array.ForEach(options, _options.Add);
}

public bool RemoveOption(T option)
public bool RemoveOption(T item)
{
ExtendedArgumentNullException.ThrowIfNull(option, nameof(option));
ExtendedArgumentNullException.ThrowIfNull(item, nameof(item));

return OptionSet.RemoveOption(option)
&& _options.Remove(option);
return OptionSet.RemoveOption(item)
&& _options.Remove(item);
}
}

0 comments on commit 90c691a

Please sign in to comment.