예제 #1
0
        /// <summary>
        /// Adds a new command.
        /// </summary>
        /// <param name="name">The command name.</param>
        /// <param name="description">The command description.</param>
        /// <returns>
        /// The <see cref="CommandLineCommand"/> object this method creates.
        /// </returns>
        public CommandLineCommand AddCommand(string name, string description)
        {
            CommandLineCommand command = new CommandLineCommand(name, description);

            this.commands.Add(command);
            return(command);
        }
예제 #2
0
        /// <summary>
        /// Parses the command-line arguments.
        /// </summary>
        /// <param name="args">The command-line arguments.</param>
        /// <returns>
        /// The <see cref="CommandLineCommand"/> object that represents the command that was parsed.
        /// </returns>
        public CommandLineCommand Parse(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                this.PrintUsage(false);
                return(null);
            }

            CommandLineCommand command = this.commands.Find(x => x.Name == args[0]);

            if (command == null)
            {
                this.PrintUsage(false);
                return(null);
            }

            command = command.Parse(this.applicationName, args.Skip(1).ToArray());
            if (command?.Name == "help")
            {
                this.PrintUsage(command.FindSwitch("long").Exists);
                return(null);
            }

            return(command);
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandLineParser"/> class.
        /// </summary>
        /// <param name="applicationName">The application name.</param>
        public CommandLineParser(string applicationName)
        {
            this.applicationName = applicationName;

            CommandLineCommand helpCommand = new CommandLineCommand("help", "display usage info");

            helpCommand.AddSwitch("long", "display full usage info");
            this.commands.Add(helpCommand);
        }
예제 #4
0
 /// <summary>
 /// Adds a new command.
 /// </summary>
 /// <param name="command">The command to add.</param>
 public void AddCommand(CommandLineCommand command) => this.commands.Add(command);
예제 #5
0
        /// <summary>
        /// Parses the command-line arguments.
        /// </summary>
        /// <param name="prefix">The command prefix that includes application name and parent commands.</param>
        /// <param name="args">The command-line arguments.</param>
        /// <returns>
        /// The parsed <see cref="CommandLineCommand"/>.
        /// </returns>
        internal CommandLineCommand Parse(string prefix, string[] args)
        {
            if (args.Length == 1 && CommandLineCommand.switchHelp.Parse(args[0]))
            {
                goto err;
            }

            if (this.commands.Count > 0)
            {
                if (args.Length == 0)
                {
                    goto err;
                }

                CommandLineCommand command = this.commands.Find(x => x.Name == args[0]);
                if (command == null)
                {
                    goto err;
                }

                return(command.Parse(string.Join(" ", prefix, this.Name), args.Skip(1).ToArray()));
            }
            else
            {
                // parse switches and parameters
                foreach (CommandLineSwitch @switch in this.options)
                {
                    bool found = false;
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (@switch.Parse(args[i]))
                        {
                            args.RemoveAt(i);
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        if (@switch is CommandLineOption missingParameter &&
                            missingParameter.Types.HasFlag(CommandLineOptionTypes.Required))
                        {
                            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The parameter <{0}> is required.", missingParameter.Name));
                        }
                    }
                }

                // parse arguments
                if (args.Length > this.arguments.Count)
                {
                    goto err;
                }

                for (int i = 0; i < args.Length && i < this.arguments.Count; i++)
                {
                    if (!this.arguments[i].Parse(args[i]))
                    {
                        goto err;
                    }
                }

                // validate required arguments
                CommandLineArgument missingArgument = this.arguments
                                                      .FirstOrDefault(x => string.IsNullOrEmpty(x.Value) && !x.Types.HasFlag(CommandLineArgumentTypes.Optional));
                if (missingArgument != null)
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The argument <{0}> is required.", missingArgument.Name));
                }

                return(this);
            }

err:
            this.PrintUsage(prefix);
            return(null);
        }