Ejemplo n.º 1
0
        /**
         * Appends the usage clause for an OptionGroup to a StringBuffer.
         * The clause is wrapped in square brackets if the group is required.
         * The display of the options is handled by appendOption
         * @param buff the StringBuffer to append to
         * @param group the group to append
         * @see #appendOption(StringBuffer,Option,boolean)
         */
        private void AppendOptionGroup(StringBuilder buff, OptionGroup group)
        {
            if (!group.IsRequired())
            {
                buff.Append("[");
            }

            List <Option> optList = new List <Option>(group.GetOptions());

            if (GetOptionComparator() != null)
            {
                optList.Sort(GetOptionComparator());
            }
            foreach (Option option in optList)
            {
                // whether the option is required or not is handled at group level
                AppendOption(buff, option, true);
                if (optList.IndexOf(option) < optList.Count - 1)
                {
                    buff.Append(" | ");
                }
            }
            if (!group.IsRequired())
            {
                buff.Append("]");
            }
        }
Ejemplo n.º 2
0
 /**
  * Add the specified option group.
  *
  * @param group the OptionGroup that is to be added
  * @return the resulting Options instance
  */
 public Options AddOptionGroup(OptionGroup group)
 {
     if (group.IsRequired())
     {
         requiredOpts.Add(group);
     }
     foreach (Option option in group.GetOptions())
     {
         // an Option cannot be required if it is in an
         // OptionGroup, either the group is required or
         // nothing is required
         option.SetRequired(false);
         AddOption(option);
         optionGroups.Add(option.GetKey(), group);
     }
     return(this);
 }
Ejemplo n.º 3
0
        /**
         * Prints the usage statement for the specified application.
         *
         * @param pw The StringWriter to print the usage statement
         * @param width The number of characters to display per line
         * @param app The application name
         * @param options The command line Options
         */
        public void PrintUsage(TextWriter pw, int width, string app, Options options)
        {
            // initialise the string buffer
            StringBuilder buff = new StringBuilder(getSyntaxPrefix()).Append(app).Append(" ");

            // create a list for processed option groups
            List <OptionGroup> processedGroups = new List <OptionGroup>();

            var optList = new List <Option>(options.GetOptions());

            if (GetOptionComparator() != null)
            {
                optList.Sort(GetOptionComparator());
            }
            foreach (Option option in optList)
            {
                // check if the option is part of an OptionGroup
                OptionGroup group = options.GetOptionGroup(option);
                // if the option is part of a group
                if (group != null)
                {
                    // and if the group has not already been processed
                    if (!processedGroups.Contains(group))
                    {
                        // add the group to the processed list
                        processedGroups.Add(group);
                        // add the usage clause
                        AppendOptionGroup(buff, group);
                    }
                    // otherwise the option was displayed in the group previously so ignore it.
                }
                else // if the Option is not part of an OptionGroup
                {
                    AppendOption(buff, option, option.IsRequired());
                }
                if (optList.IndexOf(option) < optList.Count - 1)
                {
                    buff.Append(" ");
                }
            }
            // iterate over the options
            // call printWrapped
            PrintWrapped(pw, width, buff.ToString().IndexOf(' ') + 1, buff.ToString());
        }
Ejemplo n.º 4
0
        /**
         * Removes the option or its group from the list of expected elements.
         *
         * @param option
         */
        private void UpdateRequiredOptions(Option option)
        {
            if (option.IsRequired())
            {
                expectedOpts.Remove(option.GetKey());
            }

            // if the option is in an OptionGroup make that option the selected option of the group
            if (options.GetOptionGroup(option) != null)
            {
                OptionGroup group = options.GetOptionGroup(option);

                if (group.IsRequired())
                {
                    expectedOpts.Remove(group);
                }
                group.SetSelected(option);
            }
        }
Ejemplo n.º 5
0
        /**
         * Sets the values of Options using the values in <code>properties</code>.
         *
         * @param properties The value properties to be processed.
         */
        private void HandleProperties(Dictionary <string, string> properties)
        {
            if (properties == null)
            {
                return;
            }

            foreach (var prop in properties)
            {
                string option = prop.Key;
                Option opt    = options.GetOption(option);
                if (opt == null)
                {
                    throw new UnrecognizedOptionException("Default option wasn't defined", option);
                }
                // if the option is part of a group, check if another option of the group has been selected
                OptionGroup group    = options.GetOptionGroup(opt);
                bool        selected = group != null && group.GetSelected() != null;
                if (!cmd.HasOption(option) && !selected)
                {
                    // get the value from the properties
                    string value = properties[option];

                    if (opt.HasArg())
                    {
                        if (opt.GetValues() == null || opt.GetValues().Length == 0)
                        {
                            opt.AddValueForProcessing(value);
                        }
                    }
                    else if (!("yes".Equals(value.ToLower()) ||
                               "true".Equals(value.ToLower()) ||
                               "1".Equals(value.ToLower())))
                    {
                        // if the value is not yes, true or 1 then don't add the option to the CommandLine
                        continue;
                    }
                    HandleOption(opt);
                    currentOption = null;
                }
            }
        }
Ejemplo n.º 6
0
 /**
  * Construct a new <code>AlreadySelectedException</code>
  * for the specified option group.
  *
  * @param group  the option group already selected
  * @param option the option that triggered the exception
  * @since 1.2
  */
 public AlreadySelectedException(OptionGroup group, Option option) : base("The option '" + option.GetKey() + "' was specified but an option from this group "
                                                                          + "has already been selected: '" + group.GetSelected() + "'")
 {
     this.group  = group;
     this.option = option;
 }