Skip to content

Commit

Permalink
Add multi-instance option support (#678)
Browse files Browse the repository at this point in the history
* Add multi-instance option support

Fixes #357

* Fix Appveyor build

It seems Appveyor's C# compiler is old enough that it doesn't know how
to do tuple deconstruction, so we'll avoid doing that. It makes
TokenPartitioner uglier, but no getting around that if we're dealing
with an old C# compiler.
  • Loading branch information
rmunn authored Aug 17, 2020
1 parent e6c670c commit 690136e
Show file tree
Hide file tree
Showing 22 changed files with 598 additions and 132 deletions.
26 changes: 25 additions & 1 deletion src/CommandLine/Core/InstanceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ public static ParserResult<T> Build<T>(
bool autoVersion,
IEnumerable<ErrorType> nonFatalErrors)
{
return Build(
factory,
tokenizer,
arguments,
nameComparer,
ignoreValueCase,
parsingCulture,
autoHelp,
autoVersion,
false,
nonFatalErrors);
}

public static ParserResult<T> Build<T>(
Maybe<Func<T>> factory,
Func<IEnumerable<string>, IEnumerable<OptionSpecification>, Result<IEnumerable<Token>, Error>> tokenizer,
IEnumerable<string> arguments,
StringComparer nameComparer,
bool ignoreValueCase,
CultureInfo parsingCulture,
bool autoHelp,
bool autoVersion,
bool allowMultiInstance,
IEnumerable<ErrorType> nonFatalErrors) {
var typeInfo = factory.MapValueOrDefault(f => f().GetType(), typeof(T));

var specProps = typeInfo.GetSpecifications(pi => SpecificationProperty.Create(
Expand Down Expand Up @@ -95,7 +119,7 @@ public static ParserResult<T> Build<T>(
instance = BuildImmutable(typeInfo, factory, specProps, specPropsWithValue, setPropertyErrors);
}

var validationErrors = specPropsWithValue.Validate(SpecificationPropertyRules.Lookup(tokens));
var validationErrors = specPropsWithValue.Validate(SpecificationPropertyRules.Lookup(tokens, allowMultiInstance));

var allErrors =
tokenizerResult.SuccessMessages()
Expand Down
31 changes: 29 additions & 2 deletions src/CommandLine/Core/InstanceChooser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ public static ParserResult<object> Choose(
bool autoHelp,
bool autoVersion,
IEnumerable<ErrorType> nonFatalErrors)
{
return Choose(
tokenizer,
types,
arguments,
nameComparer,
ignoreValueCase,
parsingCulture,
autoHelp,
autoVersion,
false,
nonFatalErrors);
}

public static ParserResult<object> Choose(
Func<IEnumerable<string>, IEnumerable<OptionSpecification>, Result<IEnumerable<Token>, Error>> tokenizer,
IEnumerable<Type> types,
IEnumerable<string> arguments,
StringComparer nameComparer,
bool ignoreValueCase,
CultureInfo parsingCulture,
bool autoHelp,
bool autoVersion,
bool allowMultiInstance,
IEnumerable<ErrorType> nonFatalErrors)
{
var verbs = Verb.SelectFromTypes(types);
var defaultVerbs = verbs.Where(t => t.Item1.IsDefault);
Expand All @@ -46,7 +71,7 @@ bool preprocCompare(string command) =>
arguments.Skip(1).FirstOrDefault() ?? string.Empty, nameComparer))
: (autoVersion && preprocCompare("version"))
? MakeNotParsed(types, new VersionRequestedError())
: MatchVerb(tokenizer, verbs, defaultVerb, arguments, nameComparer, ignoreValueCase, parsingCulture, autoHelp, autoVersion, nonFatalErrors);
: MatchVerb(tokenizer, verbs, defaultVerb, arguments, nameComparer, ignoreValueCase, parsingCulture, autoHelp, autoVersion, allowMultiInstance, nonFatalErrors);
}

return arguments.Any()
Expand Down Expand Up @@ -92,6 +117,7 @@ private static ParserResult<object> MatchVerb(
CultureInfo parsingCulture,
bool autoHelp,
bool autoVersion,
bool allowMultiInstance,
IEnumerable<ErrorType> nonFatalErrors)
{
string firstArg = arguments.First();
Expand All @@ -114,7 +140,8 @@ private static ParserResult<object> MatchVerb(
ignoreValueCase,
parsingCulture,
autoHelp,
autoVersion,
autoVersion,
allowMultiInstance,
nonFatalErrors);
}

Expand Down
33 changes: 19 additions & 14 deletions src/CommandLine/Core/OptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@ public static Result<
.Select(
pt =>
{
var matched = options.FirstOrDefault(s =>
var matched = options.Where(s =>
s.Key.MatchName(((OptionSpecification)pt.Specification).ShortName, ((OptionSpecification)pt.Specification).LongName, comparer)).ToMaybe();
return matched.IsJust()
? (
from sequence in matched
from converted in
converter(
sequence.Value,
pt.Property.PropertyType,
pt.Specification.TargetType != TargetType.Sequence)
select Tuple.Create(
pt.WithValue(Maybe.Just(converted)), Maybe.Nothing<Error>())
)
if (matched.IsJust())
{
var matches = matched.GetValueOrDefault(Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>());
var values = new HashSet<string>();
foreach (var kvp in matches)
{
foreach (var value in kvp.Value)
{
values.Add(value);
}
}

return converter(values, pt.Property.PropertyType, pt.Specification.TargetType != TargetType.Sequence)
.Select(value => Tuple.Create(pt.WithValue(Maybe.Just(value)), Maybe.Nothing<Error>()))
.GetValueOrDefault(
Tuple.Create<SpecificationProperty, Maybe<Error>>(
pt,
Maybe.Just<Error>(
new BadFormatConversionError(
((OptionSpecification)pt.Specification).FromOptionSpecification()))))
: Tuple.Create(pt, Maybe.Nothing<Error>());
((OptionSpecification)pt.Specification).FromOptionSpecification()))));
}

return Tuple.Create(pt, Maybe.Nothing<Error>());
}
).Memoize();
return Result.Succeed(
Expand Down
25 changes: 25 additions & 0 deletions src/CommandLine/Core/PartitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using CSharpx;

namespace CommandLine.Core
{
static class PartitionExtensions
{
public static Tuple<IEnumerable<T>,IEnumerable<T>> PartitionByPredicate<T>(
this IEnumerable<T> items,
Func<T, bool> pred)
{
List<T> yes = new List<T>();
List<T> no = new List<T>();
foreach (T item in items) {
List<T> list = pred(item) ? yes : no;
list.Add(item);
}
return Tuple.Create<IEnumerable<T>,IEnumerable<T>>(yes, no);
}
}
}
27 changes: 0 additions & 27 deletions src/CommandLine/Core/Scalar.cs

This file was deleted.

43 changes: 0 additions & 43 deletions src/CommandLine/Core/Sequence.cs

This file was deleted.

17 changes: 15 additions & 2 deletions src/CommandLine/Core/SpecificationPropertyRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ static class SpecificationPropertyRules
public static IEnumerable<Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>>>
Lookup(
IEnumerable<Token> tokens)
{
return Lookup(tokens, false);
}

public static IEnumerable<Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>>>
Lookup(
IEnumerable<Token> tokens,
bool allowMultiInstance)
{
return new List<Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>>>
{
Expand All @@ -21,7 +29,7 @@ public static IEnumerable<Func<IEnumerable<SpecificationProperty>, IEnumerable<E
EnforceMutuallyExclusiveSetAndGroupAreNotUsedTogether(),
EnforceRequired(),
EnforceRange(),
EnforceSingle(tokens)
EnforceSingle(tokens, allowMultiInstance)
};
}

Expand Down Expand Up @@ -173,10 +181,15 @@ from s in options
};
}

private static Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>> EnforceSingle(IEnumerable<Token> tokens)
private static Func<IEnumerable<SpecificationProperty>, IEnumerable<Error>> EnforceSingle(IEnumerable<Token> tokens, bool allowMultiInstance)
{
return specProps =>
{
if (allowMultiInstance)
{
return Enumerable.Empty<Error>();
}

var specs = from sp in specProps
where sp.Specification.IsOption()
where sp.Value.IsJust()
Expand Down
21 changes: 0 additions & 21 deletions src/CommandLine/Core/Switch.cs

This file was deleted.

Loading

0 comments on commit 690136e

Please sign in to comment.