Picocli,如何识别具有可选值和自定义类型转换器的选项的存在
Picocli, how to recognise presence of option with optional value and custom type converter
Picocli v2.3.0.
@CommandLine.Option(names = {"--number-headings"}, arity = "0..1", paramLabel = "levels", description = {"Adds numbers to headings. Optional parameter to set the heading levels to be numbered.", "eg. 2-4"})
public HeadingNumberingRange numberHeadings;
自定义类型转换器已注册并在提供值时正常工作 (mycommand --number-headings 2-5
)。但是 numberHeadings 仍然是 null
如果这样调用:mycommand --number-headings
.
http://picocli.info/man/2.x/#_optional_values 处的示例表明,String
键入的选项在未提供任何值时将收到一个空字符串。
因此当存在选项但未提供任何值时,空字符串是默认值。
这让我们可以区分 3 种情况:
- 选项不存在(我们得到
null
)
- 没有值的选项(我们得到空字符串)
- 带有值的选项(我们得到值)
对于自定义 ITypeConverter
,ITypeConverter
的 convert()
方法在未提供任何值时不会被调用。那么当存在选项但没有为自定义类型提供值时,等效的默认值是多少?
感谢 raising this picocli 问题跟踪器。
截至 picocli 3.0.0-alpha-5, custom type converters can map the empty String (when the option is present without a value) to a custom default value. This functionality is included in the picocli 3.0.0 刚刚发布的 GA 版本。
Picocli v2.3.0.
@CommandLine.Option(names = {"--number-headings"}, arity = "0..1", paramLabel = "levels", description = {"Adds numbers to headings. Optional parameter to set the heading levels to be numbered.", "eg. 2-4"})
public HeadingNumberingRange numberHeadings;
自定义类型转换器已注册并在提供值时正常工作 (mycommand --number-headings 2-5
)。但是 numberHeadings 仍然是 null
如果这样调用:mycommand --number-headings
.
http://picocli.info/man/2.x/#_optional_values 处的示例表明,String
键入的选项在未提供任何值时将收到一个空字符串。
因此当存在选项但未提供任何值时,空字符串是默认值。
这让我们可以区分 3 种情况:
- 选项不存在(我们得到
null
) - 没有值的选项(我们得到空字符串)
- 带有值的选项(我们得到值)
对于自定义 ITypeConverter
,ITypeConverter
的 convert()
方法在未提供任何值时不会被调用。那么当存在选项但没有为自定义类型提供值时,等效的默认值是多少?
感谢 raising this picocli 问题跟踪器。
截至 picocli 3.0.0-alpha-5, custom type converters can map the empty String (when the option is present without a value) to a custom default value. This functionality is included in the picocli 3.0.0 刚刚发布的 GA 版本。