コード例 #1
0
        /// <summary>
        ///     Returns a dictionary containing the values specified in the command line arguments with which the application was
        ///     started, keyed by argument name.
        /// </summary>
        /// <param name="commandLineString">The command line arguments with which the application was started.</param>
        /// <param name="configure">An action to configure the provided <see cref="ArgumentParseOptions"/> instance.</param>
        /// <returns>
        ///     The dictionary containing the arguments and values specified in the command line arguments with which the
        ///     application was started.
        /// </returns>
        public static Arguments Parse(string commandLineString, Action <ArgumentParseOptions> configure = null)
        {
            configure ??= new Action <ArgumentParseOptions>((_) => { });
            var options = new ArgumentParseOptions();

            configure(options);

            return(Parse(commandLineString, options));
        }
コード例 #2
0
        /// <summary>
        ///     Returns a dictionary containing the values specified in the command line arguments with which the application was
        ///     started, keyed by argument name.
        /// </summary>
        /// <param name="commandLineString">The command line arguments with which the application was started.</param>
        /// <param name="options">Parser options.</param>
        /// <returns>
        ///     The dictionary containing the arguments and values specified in the command line arguments with which the
        ///     application was started.
        /// </returns>
        public static Arguments Parse(string commandLineString, ArgumentParseOptions options)
        {
            options ??= new ArgumentParseOptions();

            commandLineString = commandLineString == default || string.IsNullOrEmpty(commandLineString) ? Environment.CommandLine : commandLineString;

            List <KeyValuePair <string, string> > argumentList;
            List <string> operandList;

            // use the strict operand regular expression to test for/extract the two halves of the string, if the operator is used.
            MatchCollection matches = Regex.Matches(commandLineString, StrictOperandSplitRegEx);

            // if there is a match, the string contains the strict operand delimiter. parse the first and second matches accordingly.
            if (matches.Count > 0)
            {
                // the first group of the first match will contain everything in the string prior to the strict operand delimiter,
                // so extract the argument key/value pairs and list of operands from that string.
                argumentList = GetArgumentList(matches[0].Groups[1].Value);
                operandList  = GetOperandList(matches[0].Groups[1].Value);

                // the first group of the second match will contain everything in the string after the strict operand delimiter, so
                // extract the operands from that string using the strict method.
                if (!string.IsNullOrEmpty(matches[0].Groups[3].Value))
                {
                    List <string> operandListStrict = GetOperandListStrict(matches[0].Groups[3].Value);
                    operandList.AddRange(operandListStrict);
                }
            }
            else
            {
                argumentList = GetArgumentList(commandLineString);
                operandList  = GetOperandList(commandLineString);
            }

            var argumentDictionary = GetArgumentDictionary(argumentList, options);

            return(new Arguments(commandLineString, argumentList, argumentDictionary, operandList, options.TargetType));
        }
コード例 #3
0
        private static Dictionary <string, object> GetArgumentDictionary(List <KeyValuePair <string, string> > argumentList, ArgumentParseOptions options)
        {
            var dict         = new ConcurrentDictionary <string, object>();
            var argumentInfo = options.TargetType == null ? new List <ArgumentInfo>() : GetArgumentInfo(options.TargetType);

            foreach (var arg in argumentList)
            {
                var info = argumentInfo.SingleOrDefault(i => i.ShortName.ToString(CultureInfo.InvariantCulture) == arg.Key || i.LongName == arg.Key);

                if (info != default(ArgumentInfo))
                {
                    bool added = false;

                    foreach (var k in new[] { info.ShortName.ToString(CultureInfo.InvariantCulture), info.LongName })
                    {
                        if (dict.ContainsKey(k))
                        {
                            dict.AddOrUpdate(k, arg.Value, (key, existingValue) => info.IsCollection ? ((List <object>)existingValue).Concat(new[] { arg.Value }).ToList() : (object)arg.Value);
                            added = true;
                            break;
                        }
                    }

                    if (!added)
                    {
                        dict.TryAdd(arg.Key, info.IsCollection ? new List <object>(new[] { arg.Value }) : (object)arg.Value);
                    }
                }
                else
                {
                    if (dict.ContainsKey(arg.Key) && (options.CombineAllMultiples || options.CombinableArguments.Contains(arg.Key)))
                    {
                        dict.AddOrUpdate(arg.Key, arg.Value, (key, existingValue) =>
                        {
                            if (existingValue.GetType() == typeof(List <object>))
                            {
                                return(((List <object>)existingValue).Concat(new[] { arg.Value }).ToList());
                            }

                            return(new List <object>()
                            {
                                existingValue, arg.Value
                            });
                        });
                    }
                    else
                    {
                        dict.AddOrUpdate(arg.Key, arg.Value, (key, existingValue) => arg.Value);
                    }
                }
            }

            return(dict.ToDictionary(a => a.Key, a => a.Value));
        }
コード例 #4
0
 /// <summary>
 /// 解析命令行参数
 /// </summary>
 /// <param name="commandLineString"></param>
 /// <param name="configure"></param>
 /// <returns></returns>
 public static ArgumentModel Parse(string commandLineString, ArgumentParseOptions options)
 {
     return(MapperTo(Arguments.Parse(commandLineString, options)));
 }