Skip to content

Commit 10fcce2

Browse files
authored
Merge pull request #4 from thejpster/named_args
Add argument support
2 parents 2a81488 + 6de62b5 commit 10fcce2

File tree

4 files changed

+1017
-161
lines changed

4 files changed

+1017
-161
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ version = "0.1.1"
44
authors = ["Jonathan 'theJPster' Pallant <[email protected]>"]
55
description = "A simple #[no_std] command line interface."
66
license = "MIT OR Apache-2.0"
7+
edition = "2018"
78

89
[dependencies]
10+
11+
12+
[dev-dependencies]
13+
pancurses = "0.16"

README.md

Lines changed: 158 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,179 @@
11
# Menu
22

3+
## Introduction
4+
35
A simple command-line menu system in Rust. Works on embedded systems, but also
46
on your command-line.
57

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)
914
Finished dev [unoptimized + debuginfo] target(s) in 0.84 secs
1015
Running `target/debug/examples/simple`
1116
In enter_root()
1217
> 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+
1755

1856
> foo
19-
In select_foo(): foo
57+
Error: Insufficient arguments given!
58+
59+
> foo 1 2 3 3
60+
Error: Too many arguments given
2061

2162
> sub
2263

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+
2871

2972
> exit
3073

3174
> 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+
3681

3782
> ^C
38-
$
83+
user@host: ~/menu $
3984
```
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

Comments
 (0)