public bool TryExtractObjectPropertyIntoCommandLineArgument(object o, CommandLineArgument argument, string[] staticMappings, out string commandLineKey, out string commandLineValue)
        {
            if (o is JObject == false)
            {
                throw new ArgumentException("Type not supported, must be JObject: " + o.GetType().FullName);
            }

            var dynamicObject = (IDictionary<string,JProperty>)o;

            var mapCandidates = argument.Aliases.Union(staticMappings).Select(a => a.Replace("-", ""));

            var mapSuccessCandidate = (from member in dynamicObject.Keys
                                       where mapCandidates.Contains(member, StringComparer.Create(System.Globalization.CultureInfo.InvariantCulture, true))
                                       select member).FirstOrDefault();

            if (mapSuccessCandidate != null)
            {
                commandLineKey = "-" + argument.DefaultAlias;
                commandLineValue = dynamicObject[mapSuccessCandidate] + "";
                return true;
            }
            else
            {
                commandLineKey = null;
                commandLineValue = null;
                return false;
            }
        }
예제 #2
0
 internal bool TryExtractObjectPropertyIntoCommandLineArgument(object o, CommandLineArgument argument, out string commandLineKey, out string commandLineValue)
 {
     if (ArgPipelineObjectMapper.CurrentMapper == null || ArgPipelineObjectMapper.CurrentMapper.TryExtractObjectPropertyIntoCommandLineArgument(o, argument, StaticMappings, out commandLineKey, out commandLineValue) == false)
     {
         return TryDefaultShredStaticObjectPropertyIntoCommandLineArgument(o, argument, out commandLineKey, out commandLineValue);
     }
     else
     {
         return true;
     }
 }
        public void exception_is_thrown_when_argments_are_not_valid()
        {
            parser.IsValid = false;

            CommandLineArgument<int> arg = new CommandLineArgument<int>(parser, "shortName", "longName", "description", true);

            int value;
            Error.Expect<InvalidOperationException>(() =>
            {
                value = arg.Value;
            });
        }
        public void value_is_take_from_the_parser()
        {
            CommandLineArgument<int> arg = new CommandLineArgument<int>(parser, "shortName", "longName", "description", true);

            parser.GetValue = (a) =>
            {
                if (a == arg)
                    return 7;
                return -1;
            };

            Assert.That(arg.Value, Is.EqualTo(7));
        }
 private static bool NeedsSingleLetterExpansion(CommandLineArgument argument)
 {
     return argument.IsQualifier &&
            argument.Modifier == "-" &&
            argument.Name.Length > 1;
 }
        private static void ExpandSingleLetterArguments(List<CommandLineArgument> receiver, int index)
        {
            var qualifiers = receiver[index].Name;
            receiver.RemoveAt(index);

            foreach (var c in qualifiers)
            {
                var singleLetterName = char.ToString(c);
                var expandedArgument = new CommandLineArgument("-", singleLetterName, null);
                receiver.Insert(index, expandedArgument);
                index++;
            }
        }
        public static IReadOnlyList<CommandLineArgument> Parse(IEnumerable<string> commandLineArguments)
        {
            var result = new List<CommandLineArgument>();

            // We'll split the arguments into command line argument objects.
            //
            // CommandLineArgument objects combine modifier (/, -, --), the qualifier
            // name, and the qualifier value.
            //
            // Please note that this code doesn't combine arguments. It only provides
            // some pre-processing over the arguments to split out the modifier,
            // qualifier, and value:
            //
            // { "--out", "out.exe" } ==> { new CommandLineArgument("--", "out", null),
            //                              new CommandLineArgument(null, null, "out.exe") }
            //
            // {"--out:out.exe" }     ==> { new CommandLineArgument("--", "out", "out.exe") }
            //
            // The code also handles the special -- argument which indicates that the following
            // arguments shouldn't be considered qualifiers.

            var hasSeenDashDash = false;

            foreach (var argument in ExpandReponseFiles(commandLineArguments))
            {
                if (!hasSeenDashDash && argument == "--")
                {
                    hasSeenDashDash = true;
                    continue;
                }

                string modifier;
                string remainder;
                bool isModifier;

                if (!hasSeenDashDash)
                {
                    isModifier = TryExtractModifer(argument, out modifier, out remainder);
                }
                else
                {
                    modifier = null;
                    remainder = argument;
                    isModifier = false;
                }

                if (!isModifier)
                    remainder = argument;

                string key;
                string value;
                if (!isModifier || !TrySplitKeyValue(remainder, out key, out value))
                {
                    key = remainder;
                    value = null;
                }

                var parsedArgument = new CommandLineArgument(modifier, key, value);
                result.Add(parsedArgument);
            }

            // Single letter options can be combined, for example the following two
            // forms are considered equivalent:
            //
            //    (1)  -xdf
            //    (2)  -x -d -f
            //
            // In order to free later phases from handling this case, we simply expand
            // single letter options to the second form.

            for (var i = result.Count - 1; i >= 0; i--)
            {
                if (NeedsSingleLetterExpansion(result[i]))
                    ExpandSingleLetterArguments(result, i);
            }

            return result.ToArray();
        }
예제 #8
0
        private static bool IsCompatible(object o, CommandLineArgument directMappingTarget)
        {
            var oType = o.GetType();

            if (oType == directMappingTarget.ArgumentType) return true;
            if (oType.GetInterfaces().Contains(directMappingTarget.ArgumentType)) return true;
            if (oType.IsSubclassOf(directMappingTarget.ArgumentType)) return true;

            return false;
        }
예제 #9
0
        private bool TryDefaultShredStaticObjectPropertyIntoCommandLineArgument(object o, CommandLineArgument argument, out string commandLineKey, out string commandLineValue)
        {
            var mapCandidates = argument.Aliases.Union(StaticMappings).Select(a => a.Replace("-", ""));

            var mapSuccessCandidate = (from p in o.GetType().GetProperties()
                                       where mapCandidates.Contains(p.Name, StringComparer.Create(System.Globalization.CultureInfo.InvariantCulture, true))
                                       select p).FirstOrDefault();

            if (mapSuccessCandidate != null)
            {
                commandLineKey = "-" + argument.DefaultAlias;
                commandLineValue = mapSuccessCandidate.GetValue(o, null) + "";
                return true;
            }
            else
            {
                commandLineKey = null;
                commandLineValue = null;
                return false;
            }
        }