|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.shell.docs; |
| 17 | + |
| 18 | +import org.springframework.shell.command.CommandRegistration; |
| 19 | +import org.springframework.shell.standard.ShellOption; |
| 20 | + |
| 21 | +class OptionTypesSnippets { |
| 22 | + |
| 23 | + class Dump1 { |
| 24 | + // tag::option-type-boolean-anno[] |
| 25 | + String example( |
| 26 | + @ShellOption() boolean arg1, |
| 27 | + @ShellOption(defaultValue = "true") boolean arg2, |
| 28 | + @ShellOption(defaultValue = "false") boolean arg3, |
| 29 | + @ShellOption() Boolean arg4, |
| 30 | + @ShellOption(defaultValue = "true") Boolean arg5, |
| 31 | + @ShellOption(defaultValue = "false") Boolean arg6 |
| 32 | + ) { |
| 33 | + return String.format("arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", |
| 34 | + arg1, arg2, arg3, arg4, arg5, arg6); |
| 35 | + } |
| 36 | + // end::option-type-boolean-anno[] |
| 37 | + void dump() { |
| 38 | + // tag::option-type-boolean-reg[] |
| 39 | + CommandRegistration.builder() |
| 40 | + .command("example") |
| 41 | + .withOption() |
| 42 | + .longNames("arg1").type(boolean.class).and() |
| 43 | + .withOption() |
| 44 | + .longNames("arg2").type(boolean.class).defaultValue("true").and() |
| 45 | + .withOption() |
| 46 | + .longNames("arg3").type(boolean.class).defaultValue("false").and() |
| 47 | + .withOption() |
| 48 | + .longNames("arg4").type(Boolean.class).and() |
| 49 | + .withOption() |
| 50 | + .longNames("arg5").type(Boolean.class).defaultValue("true").and() |
| 51 | + .withOption() |
| 52 | + .longNames("arg6").type(Boolean.class).defaultValue("false").and() |
| 53 | + .withTarget() |
| 54 | + .function(ctx -> { |
| 55 | + boolean arg1 = ctx.hasMappedOption("arg1") |
| 56 | + ? ctx.getOptionValue("arg1") |
| 57 | + : false; |
| 58 | + boolean arg2 = ctx.getOptionValue("arg2"); |
| 59 | + boolean arg3 = ctx.getOptionValue("arg3"); |
| 60 | + Boolean arg4 = ctx.getOptionValue("arg4"); |
| 61 | + Boolean arg5 = ctx.getOptionValue("arg5"); |
| 62 | + Boolean arg6 = ctx.getOptionValue("arg6"); |
| 63 | + return String.format("Hello arg1=%s arg2=%s arg3=%s arg4=%s arg5=%s arg6=%s", |
| 64 | + arg1, arg2, arg3, arg4, arg5, arg6); |
| 65 | + }) |
| 66 | + .and() |
| 67 | + .build(); |
| 68 | + // end::option-type-boolean-reg[] |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + class Dump2 { |
| 73 | + // tag::option-type-integer-anno[] |
| 74 | + String example(@ShellOption(value = "arg1") int arg1) { |
| 75 | + return "Hello " + arg1; |
| 76 | + } |
| 77 | + // end::option-type-integer-anno[] |
| 78 | + void dump() { |
| 79 | + // tag::option-type-integer-reg[] |
| 80 | + CommandRegistration.builder() |
| 81 | + .command("example") |
| 82 | + .withOption() |
| 83 | + .longNames("arg1") |
| 84 | + .type(int.class) |
| 85 | + .required() |
| 86 | + .and() |
| 87 | + .withTarget() |
| 88 | + .function(ctx -> { |
| 89 | + boolean arg1 = ctx.getOptionValue("arg1"); |
| 90 | + return "Hello " + arg1; |
| 91 | + }) |
| 92 | + .and() |
| 93 | + .build(); |
| 94 | + // end::option-type-integer-reg[] |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + class Dump3 { |
| 99 | + // tag::option-type-string-anno[] |
| 100 | + String example(@ShellOption(value = "arg1") String arg1) { |
| 101 | + return "Hello " + arg1; |
| 102 | + } |
| 103 | + // end::option-type-string-anno[] |
| 104 | + void dump() { |
| 105 | + // tag::option-type-string-reg[] |
| 106 | + CommandRegistration.builder() |
| 107 | + .command("example") |
| 108 | + .withOption() |
| 109 | + .longNames("arg1") |
| 110 | + .type(String.class) |
| 111 | + .required() |
| 112 | + .and() |
| 113 | + .withTarget() |
| 114 | + .function(ctx -> { |
| 115 | + String arg1 = ctx.getOptionValue("arg1"); |
| 116 | + return "Hello " + arg1; |
| 117 | + }) |
| 118 | + .and() |
| 119 | + .build(); |
| 120 | + // end::option-type-string-reg[] |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + class Dump4 { |
| 125 | + |
| 126 | + // tag::option-type-enum-class[] |
| 127 | + enum OptionTypeEnum { |
| 128 | + ONE,TWO,THREE |
| 129 | + } |
| 130 | + // end::option-type-enum-class[] |
| 131 | + |
| 132 | + // tag::option-type-enum-anno[] |
| 133 | + String example(@ShellOption(value = "arg1") OptionTypeEnum arg1) { |
| 134 | + return "Hello " + arg1; |
| 135 | + } |
| 136 | + // end::option-type-enum-anno[] |
| 137 | + void dump() { |
| 138 | + // tag::option-type-enum-reg[] |
| 139 | + CommandRegistration.builder() |
| 140 | + .command("example") |
| 141 | + .withOption() |
| 142 | + .longNames("arg1") |
| 143 | + .type(OptionTypeEnum.class) |
| 144 | + .required() |
| 145 | + .and() |
| 146 | + .withTarget() |
| 147 | + .function(ctx -> { |
| 148 | + OptionTypeEnum arg1 = ctx.getOptionValue("arg1"); |
| 149 | + return "Hello " + arg1; |
| 150 | + }) |
| 151 | + .and() |
| 152 | + .build(); |
| 153 | + // end::option-type-enum-reg[] |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments