예제 #1
0
        /// <summary>
        /// Add subparsers for the parent parser. Subparsers is an
        /// object that resembles a branch split in a tree. That means you can
        /// branch into different parsers from a parent parser. This can be used
        /// for a tool that does more than one job and requires keyword commands
        /// to do them. .NET CLI can be an example since it contains a lot of
        /// different commands (subparsers) for different actions.
        /// </summary>
        /// <param name="help">Help string to be printed in the help message for the subparsers object. It is empty by default.</param>
        /// <param name="title">Shows the subparsers help message in a different section with the given name.</param>
        /// <param name="dest">Adds a key to the namespace for the parser with the supplied string as value.</param>
        /// <returns>The added subparser object.</returns>
        public Subparsers AddSubparsers(string help = "", string title = "", string dest = "")
        {
            var subparsers = new Subparsers(Prog, help, title, dest);

            arguments.Add(subparsers);

            if (!string.IsNullOrEmpty(subparsers.Title))
            {
                if (Categories.ContainsKey(subparsers.Title))
                {
                    Categories[subparsers.Title].Add(subparsers);
                }
                else
                {
                    Categories[subparsers.Title] = new List <Argument>()
                    {
                        subparsers
                    }
                };
            }
            else
            {
                Categories[positionalArgsTitle].Add(subparsers);
            }

            PositionalArguments.Add(subparsers);

            return(subparsers);
        }
예제 #2
0
 public ArgParser(string[] arguments)
 {
     foreach (var arg in arguments)
     {
         if (arg.StartsWith("-"))
         {
             Flags.Add(arg);
         }
         else
         {
             PositionalArguments.Add(arg);
         }
     }
 }
예제 #3
0
        /// <summary>
        /// Add an argument with the specified properties to the argument parser.
        /// </summary>
        /// <param name="name">Name of the argument. If this name starts with the "-" prefix then it will be an optional argument, else it will be a positional argument.</param>
        /// <param name="longName">Longer (not necessarily) name of an optional argument, an alias. It can be used for positional arguments, it would only change the key value in the namespace.</param>
        /// <param name="action">Action of an optional argument.</param>
        /// <param name="defaultValue">Default value of an optional argument if it is not supplied. Default value is `null` by default.</param>
        /// <param name="choices">A list of strings indicating the only selectable options for an argument. An error will be thrown if the provided values is not in the list of choices.</param>
        /// <param name="required">Determines is an optional argument is required to be supplied. Can be used for positional arguments too but it won't effect anything.</param>
        /// <param name="help">Help string to be printed in the help message for the argument. It is empty by default.</param>
        /// <param name="constant">Constant value for the argument. This parameter only works for arguments with `StoreConst` and `AppendConst` actions.</param>
        public void AddArgument(string name,
                                string longName       = null,
                                ArgumentAction action = ArgumentAction.Store,
                                object defaultValue   = null,
                                List <string> choices = null,
                                bool required         = false,
                                string help           = "",
                                object constant       = null)
        {
            if (name.Contains(" "))
            {
                throw new InvalidArgumentNameException("An argument name can't contain spaces.");
            }

            Argument arg = new Argument(name,
                                        longName: longName,
                                        action: action,
                                        defaultValue: defaultValue,
                                        choices: choices,
                                        required: required,
                                        help: help,
                                        constant: constant);

            arguments.Add(arg);

            if (arg.Name.IsOptionalArgument())
            {
                Categories[optionalArgsTitle].Add(arg);
                OptionalArguments.Add(arg);
            }
            else
            {
                Categories[positionalArgsTitle].Add(arg);
                PositionalArguments.Add(arg);
            }
        }
예제 #4
0
 public void Add <TPositional, TArg>(PositionalBase <TConfig, TPositional, TArg> positional)
     where TPositional : PositionalBase <TConfig, TPositional, TArg>
 {
     PositionalArguments.Add(positional.Arg);
 }