// 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 { sealed class OptionSpecification : Specification { private readonly string shortName; private readonly string longName; private readonly char separator; private readonly string setName; private readonly string group; private readonly bool flagCounter; public OptionSpecification(string shortName, string longName, bool required, string setName, Maybe min, Maybe max, char separator, Maybe defaultValue, string helpText, string metaValue, IEnumerable enumValues, Type conversionType, TargetType targetType, string group, bool flagCounter = false, bool hidden = false) : base(SpecificationType.Option, required, min, max, defaultValue, helpText, metaValue, enumValues, conversionType, conversionType == typeof(int) && flagCounter ? TargetType.Switch : targetType, hidden) { this.shortName = shortName; this.longName = longName; this.separator = separator; this.setName = setName; this.group = group; this.flagCounter = flagCounter; } public static OptionSpecification FromAttribute(OptionAttribute attribute, Type conversionType, IEnumerable enumValues) { return new OptionSpecification( attribute.ShortName, attribute.LongName, attribute.Required, attribute.SetName, attribute.Min == -1 ? Maybe.Nothing() : Maybe.Just(attribute.Min), attribute.Max == -1 ? Maybe.Nothing() : Maybe.Just(attribute.Max), attribute.Separator, attribute.Default.ToMaybe(), attribute.HelpText, attribute.MetaValue, enumValues, conversionType, conversionType.ToTargetType(), attribute.Group, attribute.FlagCounter, attribute.Hidden); } public static OptionSpecification NewSwitch(string shortName, string longName, bool required, string helpText, string metaValue, bool hidden = false) { return new OptionSpecification(shortName, longName, required, string.Empty, Maybe.Nothing(), Maybe.Nothing(), '\0', Maybe.Nothing(), helpText, metaValue, Enumerable.Empty(), typeof(bool), TargetType.Switch, string.Empty, false, hidden); } public string ShortName { get { return shortName; } } public string LongName { get { return longName; } } public char Separator { get { return separator; } } public string SetName { get { return setName; } } public string Group { get { return group; } } /// /// Whether this is an int option that counts how many times a flag was set rather than taking a value on the command line /// public bool FlagCounter { get { return flagCounter; } } } }