コード例 #1
0
        /// <summary>
        /// Parse the command line using the options defined in this argument parser.
        /// </summary>
        /// <param name="args">The command line.</param>
        /// <returns>The parsed arguments.</returns>
        public List <CommandLineArgument> ParseArguments(string[] args)
        {
            List <CommandLineArgument> result = new List <CommandLineArgument>();
            int position = 0; // For positional arguments.
            CommandLineArgument current = null;

            for (int idx = 0; idx < args.Length; idx++)
            {
                string arg = args[idx];

                if (arg.StartsWith("-"))
                {
                    if (arg.StartsWith("--"))
                    {
                        var name = arg.Substring(2);
                        current = null;
                        this.Arguments.TryGetValue(name, out current);
                    }
                    else if (arg.StartsWith("-"))
                    {
                        current = null;
                        var name = arg.Substring(1);
                        // Note that "/" is not supported as an argument delimiter because it conflicts with unix file paths.
                        foreach (var s in this.Arguments.Values)
                        {
                            if (s.ShortName == name)
                            {
                                current = s;
                                break;
                            }
                        }

                        if (current == null)
                        {
                            // See if there's a matching long name with no short name defined.
                            foreach (var s in this.Arguments.Values)
                            {
                                if (s.LongName == name)
                                {
                                    current = s;
                                    break;
                                }
                            }
                        }
                    }

                    if (current == null)
                    {
                        throw new CommandLineException(string.Format("Unexpected argument: '{0}'", arg), result);
                    }

                    current = current.Clone();
                    result.Add(current);

                    if (current.PrintHelp)
                    {
                        this.PrintHelp(Console.Out);
                        Environment.Exit(1);
                    }
                }
                else if (current != null)
                {
                    // The value for the current switch argument.
                    current.AddParsedValue(arg);
                }
                else
                {
                    // Positional arguments.
                    do
                    {
                        if (position < this.PositionalNames.Count)
                        {
                            var name = this.PositionalNames[position++];
                            current = this.Arguments[name];
                        }
                        else
                        {
                            throw new CommandLineException(string.Format("Unexpected positional argument: '{0}'", arg), result);
                        }
                    }while (!IsRequired(current, result));

                    // Positional arguments have no name so the arg is the value.
                    var temp = current.Clone();
                    temp.Value = current.ParseValue(arg);
                    result.Add(temp);
                    current = null; // This argument is done, cannot have any more values.
                }
            }

            foreach (var arg in this.Arguments.Values)
            {
                if (IsRequired(arg, result) && !(from r in result where r.LongName == arg.LongName select r).Any())
                {
                    if (arg.IsPositional)
                    {
                        throw new CommandLineException(string.Format("Missing required argument: '{0}'", arg.LongName), result);
                    }
                    else
                    {
                        throw new CommandLineException(string.Format("Missing required argument: '--{0}'", arg.LongName), result);
                    }
                }
            }

            foreach (var arg in result)
            {
                if (!arg.IsPositional && arg.Value == null && arg.DataType != typeof(bool) && !arg.AllowedValues.Contains(string.Empty))
                {
                    throw new CommandLineException(string.Format("Missing value for argument: '--{0}'", arg.LongName), result);
                }
            }

            return(result);
        }