| 1 | // This example demonstrates the `vlib/cli` features that show up automatically |
| 2 | // in `--help` when you populate the optional `group`, `examples` and |
| 3 | // `learn_more` fields of `Command`. No opt-in is required — leaving any of |
| 4 | // them empty simply skips the corresponding section. |
| 5 | // |
| 6 | // Compile from the V repo root: |
| 7 | // v -o tasky examples/cli_groups.v |
| 8 | // |
| 9 | // Try it out: |
| 10 | // ./tasky --help # root help with grouped commands and examples |
| 11 | // ./tasky issue --help # sub-command help with INHERITED FLAGS |
| 12 | // ./tasky issue list -v |
| 13 | // ./tasky version |
| 14 | module main |
| 15 | |
| 16 | import cli |
| 17 | import os |
| 18 | |
| 19 | fn main() { |
| 20 | mut app := cli.Command{ |
| 21 | name: 'tasky' |
| 22 | description: 'A tiny issue tracker CLI' |
| 23 | version: '0.1.0' |
| 24 | posix_mode: true |
| 25 | examples: [ |
| 26 | '\$ tasky issue list', |
| 27 | '\$ tasky issue create --title "Fix CI"', |
| 28 | '\$ tasky config get editor', |
| 29 | ] |
| 30 | learn_more: 'Use `tasky <command> --help` for details about a command.\nDocumentation lives at https://example.test/tasky' |
| 31 | } |
| 32 | app.add_flag(cli.Flag{ |
| 33 | flag: .string |
| 34 | name: 'config' |
| 35 | abbrev: 'c' |
| 36 | description: 'Path to a tasky config file' |
| 37 | global: true |
| 38 | }) |
| 39 | app.add_command(issue_command()) |
| 40 | app.add_command(config_command()) |
| 41 | app.setup() |
| 42 | app.parse(os.args) |
| 43 | } |
| 44 | |
| 45 | fn issue_command() cli.Command { |
| 46 | return cli.Command{ |
| 47 | name: 'issue' |
| 48 | description: 'Work with issues' |
| 49 | group: 'Core commands' |
| 50 | commands: [ |
| 51 | cli.Command{ |
| 52 | name: 'list' |
| 53 | description: 'List open issues' |
| 54 | execute: issue_list |
| 55 | flags: [ |
| 56 | cli.Flag{ |
| 57 | flag: .bool |
| 58 | name: 'verbose' |
| 59 | abbrev: 'v' |
| 60 | description: 'Show issue bodies in addition to titles' |
| 61 | }, |
| 62 | ] |
| 63 | }, |
| 64 | cli.Command{ |
| 65 | name: 'create' |
| 66 | description: 'Create a new issue' |
| 67 | execute: issue_create |
| 68 | flags: [ |
| 69 | cli.Flag{ |
| 70 | flag: .string |
| 71 | name: 'title' |
| 72 | abbrev: 't' |
| 73 | description: 'Title of the issue to create' |
| 74 | required: true |
| 75 | }, |
| 76 | ] |
| 77 | }, |
| 78 | ] |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn config_command() cli.Command { |
| 83 | return cli.Command{ |
| 84 | name: 'config' |
| 85 | description: 'Read or write tasky settings' |
| 86 | group: 'Additional commands' |
| 87 | commands: [ |
| 88 | cli.Command{ |
| 89 | name: 'get' |
| 90 | description: 'Print the value of a config key' |
| 91 | usage: '<key>' |
| 92 | required_args: 1 |
| 93 | execute: config_get |
| 94 | }, |
| 95 | cli.Command{ |
| 96 | name: 'set' |
| 97 | description: 'Update a config key' |
| 98 | usage: '<key> <value>' |
| 99 | required_args: 2 |
| 100 | execute: config_set |
| 101 | }, |
| 102 | ] |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn issue_list(cmd cli.Command) ! { |
| 107 | verbose := cmd.flags.get_bool('verbose') or { false } |
| 108 | println('Listing issues (verbose=${verbose})') |
| 109 | } |
| 110 | |
| 111 | fn issue_create(cmd cli.Command) ! { |
| 112 | title := cmd.flags.get_string('title')! |
| 113 | println('Created issue: ${title}') |
| 114 | } |
| 115 | |
| 116 | fn config_get(cmd cli.Command) ! { |
| 117 | println('config get ${cmd.args[0]}') |
| 118 | } |
| 119 | |
| 120 | fn config_set(cmd cli.Command) ! { |
| 121 | println('config set ${cmd.args[0]} = ${cmd.args[1]}') |
| 122 | } |
| 123 | |