fcli icon indicating copy to clipboard operation
fcli copied to clipboard

Check whether picocli can be customized to automatically convert `_` to `-` in enum values

Open rsenden opened this issue 2 years ago • 1 comments

For consistency, enum values passed on the command line should use - as word separator, however enum values cannot contain a - character. We have various Converter and Iterable implementations to convert - to _ and vice versa, for example:

  • OutputFormatConfigConverter (also covers some additional functionality)
  • ProgressWriterTypeConverter

We should check whether there's any way to have picocli automatically handle the _ to/from - conversion when processing option values and generating completion candidates.

rsenden avatar May 14 '23 11:05 rsenden

See https://github.com/remkop/picocli/issues/2025

rsenden avatar Jun 09 '23 12:06 rsenden

We've now identified a much easier way to handle this; enum's can simply provide a toString() method like the following:

        @Override
        public String toString() {
            // Show and accept dashes instead of underscores when this
            // enum is used in picocli options.
            return name().replace('_', '-');
        }

Picocli will display the toString() value as completion candidates, and also properly resolve enum values when specified with dashes on the command line.

rsenden avatar Jul 26 '24 13:07 rsenden