| 1 | module cli |
| 2 | |
| 3 | fn test_help_message() { |
| 4 | mut cmd := Command{ |
| 5 | name: 'command' |
| 6 | description: 'description' |
| 7 | commands: [ |
| 8 | Command{ |
| 9 | name: 'sub' |
| 10 | description: 'subcommand' |
| 11 | }, |
| 12 | Command{ |
| 13 | name: 'sub2' |
| 14 | description: 'another subcommand' |
| 15 | }, |
| 16 | ] |
| 17 | flags: [ |
| 18 | Flag{ |
| 19 | flag: .string |
| 20 | name: 'str' |
| 21 | description: 'str flag' |
| 22 | }, |
| 23 | Flag{ |
| 24 | flag: .bool |
| 25 | name: 'bool' |
| 26 | description: 'bool flag' |
| 27 | abbrev: 'b' |
| 28 | }, |
| 29 | Flag{ |
| 30 | flag: .string |
| 31 | name: 'required' |
| 32 | abbrev: 'r' |
| 33 | required: true |
| 34 | }, |
| 35 | ] |
| 36 | } |
| 37 | assert cmd.help_message() == r'Usage: command [flags] [commands] |
| 38 | |
| 39 | description |
| 40 | |
| 41 | Flags: |
| 42 | -str str flag |
| 43 | -b -bool bool flag |
| 44 | -r -required (required) |
| 45 | |
| 46 | Commands: |
| 47 | sub subcommand |
| 48 | sub2 another subcommand |
| 49 | ' |
| 50 | |
| 51 | cmd.posix_mode = true |
| 52 | assert cmd.help_message() == r'Usage: command [flags] [commands] |
| 53 | |
| 54 | description |
| 55 | |
| 56 | Flags: |
| 57 | --str str flag |
| 58 | -b --bool bool flag |
| 59 | -r --required (required) |
| 60 | |
| 61 | Commands: |
| 62 | sub subcommand |
| 63 | sub2 another subcommand |
| 64 | ' |
| 65 | } |
| 66 | |
| 67 | fn test_help_message_groups_commands_by_group_field() { |
| 68 | cmd := Command{ |
| 69 | name: 'app' |
| 70 | description: 'app' |
| 71 | commands: [ |
| 72 | Command{ |
| 73 | name: 'auth' |
| 74 | description: 'Authenticate' |
| 75 | group: 'Core commands' |
| 76 | }, |
| 77 | Command{ |
| 78 | name: 'repo' |
| 79 | description: 'Work with repositories' |
| 80 | group: 'Core commands' |
| 81 | }, |
| 82 | Command{ |
| 83 | name: 'alias' |
| 84 | description: 'Define shortcuts' |
| 85 | group: 'General commands' |
| 86 | }, |
| 87 | Command{ |
| 88 | name: 'misc' |
| 89 | description: 'Other things' |
| 90 | }, |
| 91 | ] |
| 92 | } |
| 93 | assert cmd.help_message() == r'Usage: app [commands] |
| 94 | |
| 95 | app |
| 96 | |
| 97 | Core commands: |
| 98 | auth Authenticate |
| 99 | repo Work with repositories |
| 100 | |
| 101 | General commands: |
| 102 | alias Define shortcuts |
| 103 | |
| 104 | Commands: |
| 105 | misc Other things |
| 106 | ' |
| 107 | } |
| 108 | |
| 109 | fn test_help_message_renders_group_title_verbatim() { |
| 110 | // Group string is rendered as-is; capitalisation is the caller's |
| 111 | // choice. Non-ASCII first characters must work without mojibake. |
| 112 | cmd := Command{ |
| 113 | name: 'app' |
| 114 | commands: [ |
| 115 | Command{ |
| 116 | name: 'edit' |
| 117 | description: 'Edit something' |
| 118 | group: 'éditeurs' |
| 119 | }, |
| 120 | ] |
| 121 | } |
| 122 | assert cmd.help_message() == r'Usage: app [commands] |
| 123 | |
| 124 | éditeurs: |
| 125 | edit Edit something |
| 126 | ' |
| 127 | } |
| 128 | |
| 129 | fn test_help_message_separates_inherited_flags_from_locals() { |
| 130 | mut root := Command{ |
| 131 | name: 'app' |
| 132 | flags: [ |
| 133 | Flag{ |
| 134 | flag: .string |
| 135 | name: 'config' |
| 136 | abbrev: 'c' |
| 137 | description: 'Path to config' |
| 138 | global: true |
| 139 | }, |
| 140 | ] |
| 141 | commands: [ |
| 142 | Command{ |
| 143 | name: 'run' |
| 144 | description: 'Run something' |
| 145 | flags: [ |
| 146 | Flag{ |
| 147 | flag: .bool |
| 148 | name: 'verbose' |
| 149 | abbrev: 'v' |
| 150 | description: 'Verbose output' |
| 151 | }, |
| 152 | ] |
| 153 | }, |
| 154 | ] |
| 155 | } |
| 156 | root.setup() |
| 157 | mut sub := root.commands[0] |
| 158 | sub.parent = unsafe { &root } |
| 159 | // Mirror what `parse_commands` does when a child is dispatched. |
| 160 | sub.flags << root.flags[0] |
| 161 | assert sub.help_message() == r'Usage: app run [flags] |
| 162 | |
| 163 | Run something |
| 164 | |
| 165 | Flags: |
| 166 | -v -verbose Verbose output |
| 167 | |
| 168 | Inherited flags: |
| 169 | -c -config Path to config |
| 170 | ' |
| 171 | } |
| 172 | |
| 173 | fn test_help_message_renders_examples_and_learn_more() { |
| 174 | cmd := Command{ |
| 175 | name: 'app' |
| 176 | description: 'app' |
| 177 | examples: ['\$ app run', '\$ app help run'] |
| 178 | learn_more: 'Use `app help <command>` for details about a command.\nDocumentation lives at https://example.test' |
| 179 | } |
| 180 | assert cmd.help_message() == r'Usage: app |
| 181 | |
| 182 | app |
| 183 | |
| 184 | Examples: |
| 185 | $ app run |
| 186 | $ app help run |
| 187 | |
| 188 | Learn more: |
| 189 | Use `app help <command>` for details about a command. |
| 190 | Documentation lives at https://example.test |
| 191 | ' |
| 192 | } |
| 193 | |
| 194 | fn test_help_message_omits_empty_optional_sections() { |
| 195 | cmd := Command{ |
| 196 | name: 'app' |
| 197 | } |
| 198 | // No description, no flags, no commands, no examples, no learn_more. |
| 199 | assert cmd.help_message() == 'Usage: app\n' |
| 200 | } |
| 201 | |