|
1 | 1 | # Menu |
2 | 2 |
|
| 3 | +## Introduction |
| 4 | + |
3 | 5 | A simple command-line menu system in Rust. Works on embedded systems, but also |
4 | 6 | on your command-line. |
5 | 7 |
|
6 | | -``` |
7 | | -$ cargo run --example simple |
8 | | - Compiling menu v0.1.0 (file:///home/jonathan/Documents/programming/menu) |
| 8 | +**NOTE:** This crates works only in `&str` - there's no heap allocation, but |
| 9 | +there's also no automatic conversion to integers, boolean, etc. |
| 10 | + |
| 11 | +```console |
| 12 | +user@host: ~/menu $ cargo run --example simple |
| 13 | + Compiling menu v0.2.0 (file:///home/user/menu) |
9 | 14 | Finished dev [unoptimized + debuginfo] target(s) in 0.84 secs |
10 | 15 | Running `target/debug/examples/simple` |
11 | 16 | In enter_root() |
12 | 17 | > help |
13 | | -foo - makes a foo appear |
14 | | -bar - fandoggles a bar |
15 | | -sub - enter sub-menu |
16 | | -help - print this help text. |
| 18 | +AVAILABLE ITEMS: |
| 19 | + foo <a> [ <b> ] [ --verbose ] [ --level=INT ] - Makes a foo appear. |
| 20 | + bar - fandoggles a bar |
| 21 | + sub - enter sub-menu |
| 22 | + help [ <command> ] - Show this help, or get help on a specific command. |
| 23 | + |
| 24 | + |
| 25 | +> help foo |
| 26 | +SUMMARY: |
| 27 | + foo <a> [ <b> ] [ --verbose ] [ --level=INT ] |
| 28 | + |
| 29 | +PARAMETERS: |
| 30 | + <a> |
| 31 | + - This is the help text for 'a' |
| 32 | + <b> |
| 33 | + - No help text found |
| 34 | + --verbose |
| 35 | + - No help text found |
| 36 | + --level=INT |
| 37 | + - Set the level of the dangle |
| 38 | + |
| 39 | + |
| 40 | +DESCRIPTION: |
| 41 | +Makes a foo appear. |
| 42 | + |
| 43 | +This is some extensive help text. |
| 44 | + |
| 45 | +It contains multiple paragraphs and should be preceeded by the parameter list. |
| 46 | + |
| 47 | +> foo --level=3 --verbose 1 2 |
| 48 | +In select_foo. Args = ["--level=3", "--verbose", "1", "2"] |
| 49 | +a = Ok(Some("1")) |
| 50 | +b = Ok(Some("2")) |
| 51 | +verbose = Ok(Some("")) |
| 52 | +level = Ok(Some("3")) |
| 53 | +no_such_arg = Err(()) |
| 54 | + |
17 | 55 |
|
18 | 56 | > foo |
19 | | -In select_foo(): foo |
| 57 | +Error: Insufficient arguments given! |
| 58 | + |
| 59 | +> foo 1 2 3 3 |
| 60 | +Error: Too many arguments given |
20 | 61 |
|
21 | 62 | > sub |
22 | 63 |
|
23 | | -sub> help |
24 | | -baz - thingamobob a baz |
25 | | -quux - maximum quux |
26 | | -exit - leave this menu. |
27 | | -help - print this help text. |
| 64 | +/sub> help |
| 65 | +AVAILABLE ITEMS: |
| 66 | + baz - thingamobob a baz |
| 67 | + quux - maximum quux |
| 68 | + exit - Leave this menu. |
| 69 | + help [ <command> ] - Show this help, or get help on a specific command. |
| 70 | + |
28 | 71 |
|
29 | 72 | > exit |
30 | 73 |
|
31 | 74 | > help |
32 | | -foo - makes a foo appear |
33 | | -bar - fandoggles a bar |
34 | | -sub - enter sub-menu |
35 | | -help - print this help text. |
| 75 | +AVAILABLE ITEMS: |
| 76 | + foo <a> [ <b> ] [ --verbose ] [ --level=INT ] - Makes a foo appear. |
| 77 | + bar - fandoggles a bar |
| 78 | + sub - enter sub-menu |
| 79 | + help [ <command> ] - Show this help, or get help on a specific command. |
| 80 | + |
36 | 81 |
|
37 | 82 | > ^C |
38 | | -$ |
| 83 | +user@host: ~/menu $ |
39 | 84 | ``` |
| 85 | + |
| 86 | +## Using |
| 87 | + |
| 88 | +See `examples/simple.rs` for a working example that runs on Linux or Windows. Here's the menu definition from that example: |
| 89 | + |
| 90 | +```rust |
| 91 | +const ROOT_MENU: Menu<Output> = Menu { |
| 92 | + label: "root", |
| 93 | + items: &[ |
| 94 | + &Item { |
| 95 | + item_type: ItemType::Callback { |
| 96 | + function: select_foo, |
| 97 | + parameters: &[ |
| 98 | + Parameter::Mandatory { |
| 99 | + parameter_name: "a", |
| 100 | + help: Some("This is the help text for 'a'"), |
| 101 | + }, |
| 102 | + Parameter::Optional { |
| 103 | + parameter_name: "b", |
| 104 | + help: None, |
| 105 | + }, |
| 106 | + Parameter::Named { |
| 107 | + parameter_name: "verbose", |
| 108 | + help: None, |
| 109 | + }, |
| 110 | + Parameter::NamedValue { |
| 111 | + parameter_name: "level", |
| 112 | + argument_name: "INT", |
| 113 | + help: Some("Set the level of the dangle"), |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + command: "foo", |
| 118 | + help: Some( |
| 119 | + "Makes a foo appear. |
| 120 | +
|
| 121 | +This is some extensive help text. |
| 122 | +
|
| 123 | +It contains multiple paragraphs and should be preceeded by the parameter list. |
| 124 | +", |
| 125 | + ), |
| 126 | + }, |
| 127 | + &Item { |
| 128 | + item_type: ItemType::Callback { |
| 129 | + function: select_bar, |
| 130 | + parameters: &[], |
| 131 | + }, |
| 132 | + command: "bar", |
| 133 | + help: Some("fandoggles a bar"), |
| 134 | + }, |
| 135 | + &Item { |
| 136 | + item_type: ItemType::Menu(&Menu { |
| 137 | + label: "sub", |
| 138 | + items: &[ |
| 139 | + &Item { |
| 140 | + item_type: ItemType::Callback { |
| 141 | + function: select_baz, |
| 142 | + parameters: &[], |
| 143 | + }, |
| 144 | + command: "baz", |
| 145 | + help: Some("thingamobob a baz"), |
| 146 | + }, |
| 147 | + &Item { |
| 148 | + item_type: ItemType::Callback { |
| 149 | + function: select_quux, |
| 150 | + parameters: &[], |
| 151 | + }, |
| 152 | + command: "quux", |
| 153 | + help: Some("maximum quux"), |
| 154 | + }, |
| 155 | + ], |
| 156 | + entry: Some(enter_sub), |
| 157 | + exit: Some(exit_sub), |
| 158 | + }), |
| 159 | + command: "sub", |
| 160 | + help: Some("enter sub-menu"), |
| 161 | + }, |
| 162 | + ], |
| 163 | + entry: Some(enter_root), |
| 164 | + exit: Some(exit_root), |
| 165 | +}; |
| 166 | + |
| 167 | +``` |
| 168 | + |
| 169 | +## Changelog |
| 170 | + |
| 171 | +### Unreleased changes |
| 172 | + |
| 173 | +* Parameter / Argument support |
| 174 | +* Re-worked help text system |
| 175 | +* Example uses `pancurses` |
| 176 | + |
| 177 | +### v0.1.0 |
| 178 | + |
| 179 | +* First release |
0 commit comments