Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument support #4

Merged
merged 9 commits into from
Aug 11, 2019
Merged
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ version = "0.1.1"
authors = ["Jonathan 'theJPster' Pallant <[email protected]>"]
description = "A simple #[no_std] command line interface."
license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]


[dev-dependencies]
pancurses = "0.16"
176 changes: 158 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,179 @@
# Menu

## Introduction

A simple command-line menu system in Rust. Works on embedded systems, but also
on your command-line.

```
$ cargo run --example simple
Compiling menu v0.1.0 (file:///home/jonathan/Documents/programming/menu)
**NOTE:** This crates works only in `&str` - there's no heap allocation, but
there's also no automatic conversion to integers, boolean, etc.

```console
user@host: ~/menu $ cargo run --example simple
Compiling menu v0.2.0 (file:///home/user/menu)
Finished dev [unoptimized + debuginfo] target(s) in 0.84 secs
Running `target/debug/examples/simple`
In enter_root()
> help
foo - makes a foo appear
bar - fandoggles a bar
sub - enter sub-menu
help - print this help text.
AVAILABLE ITEMS:
foo <a> [ <b> ] [ --verbose ] [ --level=INT ] - Makes a foo appear.
bar - fandoggles a bar
sub - enter sub-menu
help [ <command> ] - Show this help, or get help on a specific command.


> help foo
SUMMARY:
foo <a> [ <b> ] [ --verbose ] [ --level=INT ]

PARAMETERS:
<a>
- This is the help text for 'a'
<b>
- No help text found
--verbose
- No help text found
--level=INT
- Set the level of the dangle


DESCRIPTION:
Makes a foo appear.

This is some extensive help text.

It contains multiple paragraphs and should be preceeded by the parameter list.

> foo --level=3 --verbose 1 2
In select_foo. Args = ["--level=3", "--verbose", "1", "2"]
a = Ok(Some("1"))
b = Ok(Some("2"))
verbose = Ok(Some(""))
level = Ok(Some("3"))
no_such_arg = Err(())


> foo
In select_foo(): foo
Error: Insufficient arguments given!

> foo 1 2 3 3
Error: Too many arguments given

> sub

sub> help
baz - thingamobob a baz
quux - maximum quux
exit - leave this menu.
help - print this help text.
/sub> help
AVAILABLE ITEMS:
baz - thingamobob a baz
quux - maximum quux
exit - Leave this menu.
help [ <command> ] - Show this help, or get help on a specific command.


> exit

> help
foo - makes a foo appear
bar - fandoggles a bar
sub - enter sub-menu
help - print this help text.
AVAILABLE ITEMS:
foo <a> [ <b> ] [ --verbose ] [ --level=INT ] - Makes a foo appear.
bar - fandoggles a bar
sub - enter sub-menu
help [ <command> ] - Show this help, or get help on a specific command.


> ^C
$
user@host: ~/menu $
```

## Using

See `examples/simple.rs` for a working example that runs on Linux or Windows. Here's the menu definition from that example:

```rust
const ROOT_MENU: Menu<Output> = Menu {
label: "root",
items: &[
&Item {
item_type: ItemType::Callback {
function: select_foo,
parameters: &[
Parameter::Mandatory {
parameter_name: "a",
help: Some("This is the help text for 'a'"),
},
Parameter::Optional {
parameter_name: "b",
help: None,
},
Parameter::Named {
parameter_name: "verbose",
help: None,
},
Parameter::NamedValue {
parameter_name: "level",
argument_name: "INT",
help: Some("Set the level of the dangle"),
},
],
},
command: "foo",
help: Some(
"Makes a foo appear.

This is some extensive help text.

It contains multiple paragraphs and should be preceeded by the parameter list.
",
),
},
&Item {
item_type: ItemType::Callback {
function: select_bar,
parameters: &[],
},
command: "bar",
help: Some("fandoggles a bar"),
},
&Item {
item_type: ItemType::Menu(&Menu {
label: "sub",
items: &[
&Item {
item_type: ItemType::Callback {
function: select_baz,
parameters: &[],
},
command: "baz",
help: Some("thingamobob a baz"),
},
&Item {
item_type: ItemType::Callback {
function: select_quux,
parameters: &[],
},
command: "quux",
help: Some("maximum quux"),
},
],
entry: Some(enter_sub),
exit: Some(exit_sub),
}),
command: "sub",
help: Some("enter sub-menu"),
},
],
entry: Some(enter_root),
exit: Some(exit_root),
};

```

## Changelog

### Unreleased changes

* Parameter / Argument support
* Re-worked help text system
* Example uses `pancurses`

### v0.1.0

* First release
Loading