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());
 }
예제 #2
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))
                 .AppendWhen(optionSpec.TargetType != TargetType.Switch, longName && settings.UseEqualToken ? "=" : " ")
                 .ToString());
        }
예제 #3
0
        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());
        }
예제 #4
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)
        {
            // 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());
        }
        /// <summary>
        /// Format a command line argument string from a parsed instance.
        /// </summary>
        /// <typeparam name="T">Type of <paramref name="options"/>.</typeparam>
        /// <param name="parser">Parser instance.</param>
        /// <param name="options">A parsed (or manually correctly constructed instance).</param>
        /// <param name="configuration">The <see cref="Action{UnParserSettings}"/> lambda used to configure
        /// aspects and behaviors of the unparsersing process.</param>
        /// <returns>A string with command line arguments.</returns>
        public static string FormatCommandLine <T>(this Parser parser, T options, Action <UnParserSettings> configuration)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            var settings = new UnParserSettings();

            configuration(settings);
            settings.Consumed = true;

            var type    = options.GetType();
            var builder = new StringBuilder();

            type.GetVerbSpecification()
            .MapValueOrDefault(verb => builder.Append(verb.Name).Append(' '), builder);

            var specs =
                (from info in
                 type.GetSpecifications(
                     pi => new
            {
                Specification = Specification.FromProperty(pi),
                Value = pi.GetValue(options, null).NormalizeValue(),
                PropertyValue = pi.GetValue(options, null)
            })
                 where !info.PropertyValue.IsEmpty(info.Specification, settings.SkipDefault)
                 select info)
                .Memoize();

            var allOptSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Option)
                              let o = (OptionSpecification)info.Specification
                                      where o.TargetType != TargetType.Switch || (o.TargetType == TargetType.Switch && ((bool)info.Value))
                                      where !o.Hidden || settings.ShowHidden
                                      orderby o.UniqueName()
                                      select info;

            var shortSwitches = from info in allOptSpecs
                                let o = (OptionSpecification)info.Specification
                                        where o.TargetType == TargetType.Switch
                                        where o.ShortName.Length > 0
                                        orderby o.UniqueName()
                                        select info;

            var optSpecs = settings.GroupSwitches
                ? allOptSpecs.Where(info => !shortSwitches.Contains(info))
                : allOptSpecs;

            var valSpecs = from info in specs.Where(i => i.Specification.Tag == SpecificationType.Value)
                           let v = (ValueSpecification)info.Specification
                                   orderby v.Index
                                   select info;

            builder = settings.GroupSwitches && shortSwitches.Any()
                ? builder.Append('-').Append(string.Join(string.Empty, shortSwitches.Select(
                                                             info => ((OptionSpecification)info.Specification).ShortName).ToArray())).Append(' ')
                : builder;
            optSpecs.ForEach(
                opt =>
                builder
                .Append(FormatOption((OptionSpecification)opt.Specification, opt.Value, settings))
                .Append(' ')
                );

            builder.AppendWhen(valSpecs.Any() && parser.Settings.EnableDashDash, "-- ");

            valSpecs.ForEach(
                val => builder.Append(FormatValue(val.Specification, val.Value)).Append(' '));

            return(builder
                   .ToString().TrimEnd(' '));
        }
예제 #7
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);
        }