Uploaded image for project: 'Commons CLI'
  1. Commons CLI
  2. CLI-212

Print command-line options in same order as added

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2
    • Fix Version/s: 1.3
    • Component/s: Help formatter
    • Labels:
      None

      Description

      Using the current HelpFormater and Options classes it is impossible to print command-line options in the same order as they were added.

      In some applications this is critical, since option grouping is important for understanding how to use a program. Also, the programmer might have good reasons for adding options in a particular order. The default could remain printing sorted options, however.

      Two small changes need to be made to allow this:

      1. Change to use LinkedHashMap instead of HashMap in Options class:

      private Map shortOpts = new LinkedHashMap(); // keeps original order by default
      private Map optionGroups = new LinkedHashMap();

      2. Change the "null" formatter option in HelpFormatter to actually skip sorting:

      public void setOptionComparator(Comparator comparator)
      this.optionComparator = comparator; // allow null values, default ordering
      }

      Fix #2 above isn't strictly necessary, but would avoid forcing users to create boiler-plate code for this.

      1. sort-fix.patch
        2 kB
        Per Cederberg

        Activity

        Hide
        percederberg Per Cederberg added a comment -

        Clarification + patch for required code changes.

        Show
        percederberg Per Cederberg added a comment - Clarification + patch for required code changes.
        Hide
        ebourg Emmanuel Bourg added a comment -

        Thank you for the suggestion, that makes sense. I'll add this to the 1.3 release.

        Show
        ebourg Emmanuel Bourg added a comment - Thank you for the suggestion, that makes sense. I'll add this to the 1.3 release.
        Hide
        ebourg Emmanuel Bourg added a comment -

        This is now fixed on the trunk

        Show
        ebourg Emmanuel Bourg added a comment - This is now fixed on the trunk

          People

          • Assignee:
            ebourg Emmanuel Bourg
            Reporter:
            percederberg Per Cederberg
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development