Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutually exclusive group information #21

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
8 changes: 5 additions & 3 deletions Documentation/OptionalArguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ Additional group:
```

## Mutual Exclusion
If you add options to the mutually exclusive group, **NetArgumentParser** will make sure that only one of the arguments in this group was present on the command line. Note that currently mutually exclusive groups don't have title and description (it will not be displayed in the help output). Moreover, you cannot add options to the option set using mutually exclusive group. This group is intended only to mark options that are already added.
If you add options to the mutually exclusive group, **NetArgumentParser** will make sure that only one of the arguments in this group was present on the command line. Note that title and description of mutually exclusive group will not be displayed in the help output. Moreover, you cannot add options to the option set using mutually exclusive group. This group is intended only to mark options that are already added.

You can create mutually exclusive group using `AddMutuallyExclusiveOptionGroup()` method of the `ArgumentParser` class. Options can be added to this group by passing them to method `AddMutuallyExclusiveOptionGroup()` or by calling method `AddOptions()` of the created group.

Expand All @@ -270,8 +270,10 @@ var nickOption = new ValueOption<string>("nick", afterValueParsingAction: t => n
var parser = new ArgumentParser();
parser.AddOptions(options);

MutuallyExclusiveOptionGroup<ICommonOption> group =
parser.AddMutuallyExclusiveOptionGroup([nameOption, nickOption]);
MutuallyExclusiveOptionGroup<ICommonOption> group = parser.AddMutuallyExclusiveOptionGroup(
"group",
"description",
[nameOption, nickOption]);

parser.Parse(new string[] { "--name", "John" }); // name: John
parser.Parse(new string[] { "--nick", "mr.john" }); // nick: mr.john
Expand Down
2 changes: 1 addition & 1 deletion Documentation/Subcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Subcommand subcommand = parser.AddSubcommand("name", "description");
subcommand.AddOptions(new FlagOption("verbose", afterHandlingAction: () => verbose = true));
subcommand.AddConverters(new ValueConverter<int>(Convert.ToInt32));

OptionGroup<ICommonOption> subcommandGroup = subcommand.AddOptionGroup("group", string.Empty);
OptionGroup<ICommonOption> subcommandGroup = subcommand.AddOptionGroup("group", "description");
subcommandGroup.AddOptions(new FlagOption("debug", afterHandlingAction: () => debug = true));
```

Expand Down
Loading