1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use super::stdin::BossCommand;
use structopt::StructOpt;
use anyhow::anyhow;
#[derive(Debug)]
#[derive(StructOpt)]
struct Send {
id: Option<String>,
message: Option<String>,
cycletime: Option<String>,
}
#[derive(Debug)]
#[derive(StructOpt)]
struct Receive {
id: Option<String>,
nrofmessages: Option<String>,
}
#[derive(StructOpt)]
#[structopt(
name = "rusty-can",
version = "0.1.0",
setting(clap::AppSettings::NoBinaryName),
global_setting(clap::AppSettings::ColoredHelp),
setting(clap::AppSettings::ArgRequiredElseHelp),
subcommand(
clap::SubCommand::with_name("send")
.about("Used to send can messages")
.arg(
clap::Arg::with_name("id")
.takes_value(true)
.multiple(false)
.required(false)
.short("i")
.long("canid")
.help("Id of can message")
.default_value("0x40A"),
)
.arg(
clap::Arg::with_name("message")
.takes_value(true)
.multiple(false)
.required(false)
.short("m")
.long("message")
.help("message to be send")
.default_value("deadbeef"),
)
.arg(
clap::Arg::with_name("cycletime")
.takes_value(true)
.multiple(false)
.required(false)
.short("c")
.long("cycletime")
.help("cycle time in ms. 0 if not cyclic.")
.default_value("0"),
)
),
subcommand(
clap::SubCommand::with_name("receive")
.about("Used to receive can messages")
.arg(
clap::Arg::with_name("id")
.takes_value(true)
.multiple(false)
.required(false)
.short("i")
.long("canid")
.help("listen to this can ID")
.default_value("0x40A"),
)
.arg(
clap::Arg::with_name("nrofmessages")
.takes_value(true)
.multiple(false)
.required(false)
.short("n")
.long("nrofmessages")
.help("number of messages to receive")
.default_value("1"),
)
),
subcommand(
clap::SubCommand::with_name("exit")
.about("exit the cli")
.help("exit the cli")
),
)]
#[derive(Debug)]
enum CanCommand {
Send(Send),
Receive(Receive),
Exit,
}
pub enum ParsedCommand {
Boss(BossCommand),
Exit,
}
pub fn parse(command: &str) -> anyhow::Result<ParsedCommand> {
let words = shell_words::split(command)?;
let cmd = match CanCommand::from_iter_safe(words) {
Ok(cmd) => cmd,
Err(error) => return Err(anyhow!(error.message)),
};
macro_rules! c {
($cmd:ident$($args:tt)*) => {
ParsedCommand::Boss(BossCommand::$cmd$($args)*)
};
}
let cmd = match cmd {
CanCommand::Exit => ParsedCommand::Exit,
CanCommand::Send(sendcmd) => match sendcmd {
Send { id, message, cycletime} => c!(SendCan{ id, message, cycletime}),
},
CanCommand::Receive(receivecmd) => match receivecmd {
Receive { id, nrofmessages} => c!(ReceiveCan{ id, nrofmessages}),
},
};
Ok(cmd)
}