Exemple #1
0
        private int GetMaxOptionLength(OptionSpecification spec)
        {
            var specLength = 0;

            var hasShort = spec.ShortName.Length > 0;
            var hasLong = spec.LongName.Length > 0;

            var metaLength = 0;
            if (spec.MetaValue.Length > 0)
                metaLength = spec.MetaValue.Length + 1;

            if (hasShort)
            {
                ++specLength;
                if (AddDashesToOption)
                    ++specLength;

                specLength += metaLength;
            }

            if (hasLong)
            {
                specLength += spec.LongName.Length;
                if (AddDashesToOption)
                    specLength += 2;

                specLength += metaLength;
            }

            if (hasShort && hasLong)
                specLength += 2; // ", "

            return specLength;
        }
 private static string FormatOption(OptionSpecification spec, object value, UnParserSettings settings)
 {
     return(new StringBuilder()
            .Append(spec.FormatName(settings))
            .AppendWhen(spec.TargetType != TargetType.Switch, FormatValue(spec, value))
            .ToString());
 }
Exemple #3
0
        public void IsSatisfiedBy_ShouldBeTrueForOptionalArgumentThatIsNotThere()
        {
            var specification = new OptionSpecification(OptionValueSpecification.ForCommand("required"),
                                                        OptionValueSpecification.ForOptionalCommand("optional"));

            specification.IsSatisfiedBy(Argument.CreateCommand("required"))
            .Should()
            .BeTrue("because the command is optional");
        }
Exemple #4
0
 private OptionSpecification MakeHelpEntry()
 {
     return(OptionSpecification.NewSwitch(
                string.Empty,
                "help",
                false,
                sentenceBuilder.HelpCommandText(AddDashesToOption),
                string.Empty));
 }
Exemple #5
0
 private OptionSpecification MakeVersionEntry()
 {
     return(OptionSpecification.NewSwitch(
                string.Empty,
                "version",
                false,
                sentenceBuilder.VersionCommandText(AddDashesToOption),
                string.Empty));
 }
Exemple #6
0
 private IEnumerable <Specification> AdaptVerbsToSpecifications(IEnumerable <Type> types)
 {
     return((from verbTuple in Verb.SelectFromTypes(types)
             select
             OptionSpecification.NewSwitch(
                 string.Empty,
                 verbTuple.Item1.Name,
                 false,
                 verbTuple.Item1.HelpText,
                 string.Empty)).Concat(new[] { MakeHelpEntry(), MakeVersionEntry() }));
 }
        private static string FormatName(this OptionSpecification optionSpec, UnParserSettings settings)
        {
            var longName =
                optionSpec.LongName.Length > 0 &&
                settings.NameStyleFormat == NameStyleFormat.PreferLongName;

            return
                (new StringBuilder(longName
                    ? "--".JoinTo(optionSpec.LongName)
                    : "-".JoinTo(optionSpec.ShortName))
                 .AppendIf(longName && settings.UseEqualToken, "=", " ")
                 .ToString());
        }
Exemple #8
0
        private static string FormatName(this OptionSpecification optionSpec, UnParserSettings settings)
        {
            var longName =
                optionSpec.LongName.Length > 0 &&
                !settings.PreferShortName;

            return
                (new StringBuilder(longName
                    ? "--".JoinTo(optionSpec.LongName)
                    : "-".JoinTo(optionSpec.ShortName))
                 .AppendIf(longName && settings.UseEqualToken && optionSpec.ConversionType != typeof(bool), "=", " ")
                 .ToString());
        }
        private static string FormatName(this OptionSpecification optionSpec, UnParserSettings settings)
        {
            var longName =
                optionSpec.LongName.Length > 0 &&
                !settings.PreferShortName;

            return
                (new StringBuilder(longName
                    ? "--".JoinTo(optionSpec.LongName)
                    : "-".JoinTo(optionSpec.ShortName))
                 .AppendWhen(optionSpec.TargetType != TargetType.Switch, longName && settings.UseEqualToken ? "=" : " ")
                 .ToString());
        }
        private static string FormatName(this OptionSpecification optionSpec, UnParserSettings settings)
        {
            // Have a long name and short name not preferred? Go with long!
            // No short name? Has to be long!
            var longName = (optionSpec.LongName.Length > 0 && !settings.PreferShortName) ||
                           optionSpec.ShortName.Length == 0;

            return
                (new StringBuilder(longName
                    ? "--".JoinTo(optionSpec.LongName)
                    : "-".JoinTo(optionSpec.ShortName))
                 .AppendWhen(optionSpec.TargetType != TargetType.Switch, longName && settings.UseEqualToken ? "=" : " ")
                 .ToString());
        }
Exemple #11
0
        private static string FormatName(this OptionSpecification optionSpec, object value, UnParserSettings settings)
        {
            // Have a long name and short name not preferred? Go with long!
            // No short name? Has to be long!
            var longName = (optionSpec.LongName.Length > 0 && !settings.PreferShortName) ||
                           optionSpec.ShortName.Length == 0;

            var formattedName =
                new StringBuilder(longName
                    ? "--".JoinTo(optionSpec.LongName)
                    : "-".JoinTo(optionSpec.ShortName))
                .AppendWhen(optionSpec.TargetType != TargetType.Switch, longName && settings.UseEqualToken ? "=" : " ")
                .ToString();

            return(optionSpec.FlagCounter ? String.Join(" ", Enumerable.Repeat(formattedName, (int)value)) : formattedName);
        }
Exemple #12
0
        ComparableOption ToComparableOption(Specification spec, int index)
        {
            OptionSpecification option = spec as OptionSpecification;
            ValueSpecification  value  = spec as ValueSpecification;
            bool required = option?.Required ?? false;

            return(new ComparableOption()
            {
                Required = required,
                IsOption = option != null,
                IsValue = value != null,
                LongName = option?.LongName ?? value?.MetaName,
                ShortName = option?.ShortName,
                Index = index
            });
        }
Exemple #13
0
        public void Get_name_from_option_specification()
        {
            const string ShortName = "s";
            const string LongName  = "long";

            // Fixture setup
            var expected = new NameInfo(ShortName, LongName);
            var spec     = new OptionSpecification(ShortName, LongName, false, string.Empty, Maybe.Nothing <int>(), Maybe.Nothing <int>(), '.', null, string.Empty, string.Empty, new List <string>(), typeof(IEnumerable <string>), TargetType.Sequence, string.Empty);

            // Exercize system
            var result = spec.FromOptionSpecification();

            // Verify outcome
            expected.Should().BeEquivalentTo(result);

            // Teardown
        }
Exemple #14
0
        public void ParseArguments_ShouldParseCommandAndTwoLabels()
        {
            const string groupLabel    = "group:";
            const string policyLabel   = "policy:";
            var          specification = new OptionSpecification(OptionValueSpecification.ForCommand("chef"),
                                                                 OptionValueSpecification.ForValue(policyLabel, "the policy"),
                                                                 OptionValueSpecification.ForValue(groupLabel, "the group"));

            const string policyValue = "policy-value";
            const string groupValue  = "group-value";
            var          arguments   = specification.ParseArguments("chef", policyLabel, policyValue, groupLabel, groupValue);

            arguments.Should().NotBeNull("because the arguments fit the specification");
            arguments.Length.Should().Be(3);
            OptionValueSpecificationTest.AssertArgumentIsCommandArgument("chef", arguments[0]);
            OptionValueSpecificationTest.AssertArgumentIsValueArgument(policyLabel, policyValue, arguments[1]);
            OptionValueSpecificationTest.AssertArgumentIsValueArgument(groupLabel, groupValue, arguments[2]);
        }
Exemple #15
0
 private string AddOptionName(int maxLength, OptionSpecification specification)
 {
     return
         (new StringBuilder(maxLength)
          .MapIf(
              specification.ShortName.Length > 0,
              it => it
              .AppendWhen(addDashesToOption, '-')
              .AppendFormat("{0}", specification.ShortName)
              .AppendFormatWhen(specification.MetaValue.Length > 0, " {0}", specification.MetaValue)
              .AppendWhen(specification.LongName.Length > 0, ", "))
          .MapIf(
              specification.LongName.Length > 0,
              it => it
              .AppendWhen(addDashesToOption, "--")
              .AppendFormat("{0}", specification.LongName)
              .AppendFormatWhen(specification.MetaValue.Length > 0, "={0}", specification.MetaValue))
          .ToString());
 }
Exemple #16
0
        private HelpText AddOption(string requiredWord, int maxLength, OptionSpecification option, int widthOfHelpText)
        {
            this.optionsHelp.Append("  ");
            var optionName = new StringBuilder(maxLength);

            if (option.ShortName.Length > 0)
            {
                if (this.addDashesToOption)
                {
                    optionName.Append('-');
                }

                optionName.AppendFormat("{0}", option.ShortName);

                if (option.MetaValue.Length > 0)
                {
                    optionName.AppendFormat(" {0}", option.MetaValue);
                }

                if (option.LongName.Length > 0)
                {
                    optionName.Append(", ");
                }
            }

            if (option.LongName.Length > 0)
            {
                if (this.addDashesToOption)
                {
                    optionName.Append("--");
                }

                optionName.AppendFormat("{0}", option.LongName);

                if (option.MetaValue.Length > 0)
                {
                    optionName.AppendFormat("={0}", option.MetaValue);
                }
            }

            this.optionsHelp.Append(optionName.Length < maxLength ?
                                    optionName.ToString().PadRight(maxLength) :
                                    optionName.ToString());

            this.optionsHelp.Append("    ");
            var optionHelpText = option.HelpText;

            if (option.DefaultValue.IsJust())
            {
                optionHelpText = "(Default: {0}) ".FormatLocal(option.DefaultValue.FromJust()) + optionHelpText;
            }

            if (option.Required)
            {
                optionHelpText = "{0} ".FormatInvariant(requiredWord) + optionHelpText;
            }

            if (!string.IsNullOrEmpty(optionHelpText))
            {
                do
                {
                    var wordBuffer = 0;
                    var words      = optionHelpText.Split(new[] { ' ' });
                    for (var i = 0; i < words.Length; i++)
                    {
                        if (words[i].Length < (widthOfHelpText - wordBuffer))
                        {
                            this.optionsHelp.Append(words[i]);
                            wordBuffer += words[i].Length;
                            if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1)
                            {
                                this.optionsHelp.Append(" ");
                                wordBuffer++;
                            }
                        }
                        else if (words[i].Length >= widthOfHelpText && wordBuffer == 0)
                        {
                            this.optionsHelp.Append(words[i].Substring(0, widthOfHelpText));
                            wordBuffer = widthOfHelpText;
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }

                    optionHelpText = optionHelpText.Substring(
                        Math.Min(wordBuffer, optionHelpText.Length)).Trim();
                    if (optionHelpText.Length > 0)
                    {
                        this.optionsHelp.Append(Environment.NewLine);
                        this.optionsHelp.Append(new string(' ', maxLength + 6));
                    }
                }while (optionHelpText.Length > widthOfHelpText);
            }

            this.optionsHelp.Append(optionHelpText);
            this.optionsHelp.Append(Environment.NewLine);
            if (this.additionalNewLineAfterOption)
            {
                this.optionsHelp.Append(Environment.NewLine);
            }

            return(this);
        }
 internal static NameInfo FromOptionSpecification(OptionSpecification specification)
 {
     return(new NameInfo(
                specification.LongName,
                specification.ShortName));
 }