Types
本部分讨论如何使用特定数据类型作为选项值。
String
String
是最为简单的类型,因为它不涉及任何转换,因为用户输入的内容始终为字符串。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
尽管要求将类型定义为 String
并非严格规定,但始终建议这样做。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Boolean
使用布尔类型涉及更多内容,因为有 boolean
和 Boolean
,而后者可以为 null。布尔类型通常用作标志,这意味着可能不需要参数值。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
shell:>example
arg1=false arg2=true arg3=false arg4=false arg5=true arg6=false
shell:>example --arg4
arg1=false arg2=true arg3=false arg4=true arg5=true arg6=false
shell:>example --arg4 false
arg1=false arg2=true arg3=false arg4=false arg5=true arg6=false
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
shell:>example
arg1=false arg2=true arg3=false arg4=null arg5=true arg6=false
shell:>example --arg4
arg1=false arg2=true arg3=false arg4=true arg5=true arg6=false
shell:>example --arg4 false
arg1=false arg2=true arg3=false arg4=false arg5=true arg6=false
Number
数字按原样转换。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Enum
如果给定值与枚举本身完全匹配,则可以转换为枚举。目前,您可以假设不区分大小写进行转换。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Array
数组可以按原样用于字符串和基元类型。
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]
Unresolved include directive in modules/ROOT/pages/options/types.adoc - include::../../../../src/test/java/org/springframework/shell/docs/OptionTypesSnippets.java[]