moqros-cli 0.8.0

ROS 2 <-> Media over QUIC bridge nodes for images and other messages
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Parsing the command line and ROS parameters into the flags.

use std::any::TypeId;
use std::collections::HashSet;

use anyhow::{Context, bail};
use clap::error::ErrorKind;
use clap::{ArgAction, Command, Parser};
use moqros::ros::r2r;
use moqros::ros::r2r::ParameterValue;

/// Create the ROS node and parse `T` from the command line and the node's ROS parameters.
///
/// Arguments between `--ros-args` and `--` are left to rcl, so the binaries accept
/// remapping (`-r __node:=…`, `-r __ns:=…`), `-p` and `--params-file` like any ROS
/// node. Each ROS parameter named after an argument (`bitrate`, `keyframe_interval_ms`,
/// …) fills that argument: flags win over parameters, which win over environment
/// variables and defaults.
pub fn init<T: Parser>(node_name: &str) -> anyhow::Result<(T, r2r::Node)> {
	let args = strip_ros_args(std::env::args());
	// Answer `--help` and `--version` without bringing up ROS.
	if let Some(err) = help_or_version(T::command(), &args) {
		err.exit();
	}

	let ctx = r2r::Context::create().context("failed to create ROS context")?;
	let node = r2r::Node::create(ctx, node_name, "").context("failed to create ROS node")?;

	let flags = param_flags(
		&T::command(),
		node.params
			.lock()
			.unwrap()
			.iter()
			.map(|(name, param)| (name.as_str(), &param.value)),
	)?;
	let cli = match parse_args(args, flags) {
		Ok(cli) => cli,
		Err(ParseError::CommandLine(err)) => err.exit(),
		Err(ParseError::Param(err)) => {
			// Just clap's first line; its usage hints are about the command line.
			let message = err.to_string();
			let message = message.lines().next().unwrap_or_default().trim_start_matches("error: ");
			bail!("invalid ROS parameter: {message}");
		}
	};
	Ok((cli, node))
}

/// Drop the ROS arguments (`--ros-args … [--]`, possibly repeated) from `args`.
pub fn strip_ros_args(args: impl IntoIterator<Item = String>) -> Vec<String> {
	let mut in_ros_args = false;
	args.into_iter()
		.filter(|arg| match (in_ros_args, arg.as_str()) {
			(false, "--ros-args") | (true, "--") => {
				in_ros_args = !in_ros_args;
				false
			}
			_ => !in_ros_args,
		})
		.collect()
}

/// The error to exit with if `args` ask for help or the version.
///
/// Clap answers these as soon as it meets the flag, before checking anything else.
fn help_or_version(command: Command, args: &[String]) -> Option<clap::Error> {
	let err = command.try_get_matches_from(args).err()?;
	matches!(err.kind(), ErrorKind::DisplayHelp | ErrorKind::DisplayVersion).then_some(err)
}

/// Why [`parse_args`] failed.
#[derive(Debug)]
enum ParseError {
	/// The command line itself is invalid.
	CommandLine(clap::Error),
	/// The command line is fine on its own, so a ROS parameter is invalid.
	Param(clap::Error),
}

/// Parse `T` from `args` (program name first, ROS arguments already stripped) and
/// `param_flags`.
///
/// The parameter flags go right after the program name. With `args_override_self` a
/// later occurrence of a flag wins, so the command line overrides them, while
/// environment variables and defaults only apply to flags given neither way. Repeatable
/// flags collect every occurrence instead, so parameter flags that the command line
/// gives again are dropped up front.
fn parse_args<T: Parser>(args: Vec<String>, param_flags: Vec<String>) -> Result<T, ParseError> {
	let flag = |arg: &str| arg.split_once('=').map_or(arg, |(flag, _)| flag).to_string();
	let given: HashSet<String> = args
		.iter()
		.skip(1)
		.filter(|arg| arg.starts_with("--"))
		.map(|arg| flag(arg))
		.collect();
	let param_flags: Vec<String> = param_flags
		.into_iter()
		.filter(|param| !given.contains(&flag(param)))
		.collect();
	let parse = |param_flags: Vec<String>| {
		let mut args = args.iter().cloned();
		let args = args.next().into_iter().chain(param_flags).chain(args);
		let matches = T::command().args_override_self(true).try_get_matches_from(args)?;
		T::from_arg_matches(&matches)
	};
	parse(param_flags).map_err(|err| match parse(Vec::new()) {
		Ok(_) => ParseError::Param(err),
		Err(err) => ParseError::CommandLine(err),
	})
}

/// Command-line flags (`--name=value`) for the ROS parameters named after an argument
/// of `command`.
fn param_flags<'a>(
	command: &Command,
	params: impl IntoIterator<Item = (&'a str, &'a ParameterValue)>,
) -> anyhow::Result<Vec<String>> {
	let mut flags = Vec::new();
	for (name, value) in params {
		let Some((long, arg)) = command
			.get_arguments()
			.find(|arg| arg.get_id() == name)
			.and_then(|arg| Some((arg.get_long()?, arg)))
		else {
			// r2r reads `use_sim_time` itself.
			if name != "use_sim_time" {
				tracing::warn!(name, "ignoring unknown ROS parameter");
			}
			continue;
		};
		let value = match value {
			// An empty YAML value: leave the flag to its environment variable or default.
			ParameterValue::NotSet => continue,
			ParameterValue::Bool(value) => value.to_string(),
			ParameterValue::Integer(value) => value.to_string(),
			// Keep the fraction of whole numbers for strings (`1.0` stays `1.0`), but drop
			// it for numbers so `2000000.0` still fits an integer flag.
			ParameterValue::Double(value) if arg.get_value_parser().type_id() == TypeId::of::<String>() => {
				format!("{value:?}")
			}
			ParameterValue::Double(value) => value.to_string(),
			ParameterValue::String(value) => value.clone(),
			// A list fills a repeatable flag, one occurrence per element.
			ParameterValue::StringArray(values) if matches!(arg.get_action(), ArgAction::Append) => {
				flags.extend(values.iter().map(|value| format!("--{long}={value}")));
				continue;
			}
			value => bail!("ROS parameter `{name}` has unsupported type: {value:?}"),
		};
		// One token, so values starting with `-` aren't taken for flags.
		flags.push(format!("--{long}={value}"));
	}
	Ok(flags)
}

#[cfg(test)]
mod tests {
	use clap::CommandFactory;

	use super::*;
	use crate::{EncoderArgs, Format, QosArgs, RelayArgs, RepublishArgs, TopicSpec};

	fn strip(args: &[&str]) -> Vec<String> {
		strip_ros_args(args.iter().map(|arg| arg.to_string()))
	}

	#[derive(Debug, Parser)]
	#[command(version = "1.2.3")]
	struct TestCli {
		#[arg(long, default_value = "/image_raw")]
		topic: String,

		#[arg(long)]
		broadcast: Option<String>,

		#[arg(long, default_value_t = 30.0)]
		fps: f32,

		#[arg(long, default_value_t = 2000)]
		keyframe_interval_ms: u64,

		#[arg(long, value_enum, default_value_t = Format::Video)]
		format: Format,

		#[arg(id = "type", long = "type")]
		ros_type: Option<String>,

		#[arg(long, default_value_t = moqros::DataMode::default())]
		mode: moqros::DataMode,

		#[command(flatten)]
		relay: RelayArgs,

		#[command(flatten)]
		qos: QosArgs,
	}

	#[derive(Debug, Parser)]
	struct MultiCli {
		#[arg(long, default_value = "/image_raw")]
		topic: Vec<TopicSpec>,
	}

	/// Only parsed by `params_override_env`, in a child process of its own.
	#[derive(Debug, Parser)]
	struct EnvCli {
		#[arg(long, env = "MOQROS_TEST_BITRATE", default_value_t = 1)]
		bitrate: u32,
	}

	fn flags<T: Parser>(params: &[(&str, ParameterValue)]) -> anyhow::Result<Vec<String>> {
		param_flags(&T::command(), params.iter().map(|(name, value)| (*name, value)))
	}

	fn args(args: &[&str]) -> Vec<String> {
		std::iter::once("bin")
			.chain(args.iter().copied())
			.map(String::from)
			.collect()
	}

	fn parse<T: Parser>(args_: &[&str], params: &[(&str, ParameterValue)]) -> Result<T, ParseError> {
		parse_args(args(args_), flags::<T>(params).unwrap())
	}

	fn string(value: &str) -> ParameterValue {
		ParameterValue::String(value.to_string())
	}

	#[test]
	fn strips_ros_args() {
		assert_eq!(strip(&["bin", "--topic", "a"]), ["bin", "--topic", "a"]);
		assert_eq!(
			strip(&[
				"bin",
				"--topic",
				"a",
				"--ros-args",
				"-r",
				"__node:=n",
				"-p",
				"bitrate:=1"
			]),
			["bin", "--topic", "a"]
		);
		assert_eq!(
			strip(&[
				"bin",
				"--ros-args",
				"-r",
				"__ns:=/x",
				"--",
				"--reliable",
				"--ros-args",
				"-p",
				"a:=1"
			]),
			["bin", "--reliable"]
		);
	}

	#[test]
	fn params_fill_flags() {
		let cli: TestCli = parse(
			&[],
			&[
				("topic", string("/front/image_raw")),
				("keyframe_interval_ms", ParameterValue::Integer(500)),
				("reliable", ParameterValue::Bool(true)),
				("url", string("http://relay:4443/anon")),
			],
		)
		.unwrap();
		assert_eq!(cli.topic, "/front/image_raw");
		assert_eq!(cli.keyframe_interval_ms, 500);
		assert!(cli.qos.reliable);
		assert_eq!(cli.relay.url.as_str(), "http://relay:4443/anon");
		assert!(!cli.relay.insecure);
		assert_eq!(cli.broadcast, None);
	}

	#[test]
	fn client_cert_flags_come_in_pairs() {
		let cli: TestCli = parse(
			&["--tls-cert", "/certs/robot.pem"],
			&[("tls_key", string("/certs/robot.key"))],
		)
		.unwrap();
		let config = cli.relay.config();
		assert_eq!(
			config.tls_cert.as_deref(),
			Some(std::path::Path::new("/certs/robot.pem"))
		);
		assert_eq!(
			config.tls_key.as_deref(),
			Some(std::path::Path::new("/certs/robot.key"))
		);
		assert!(parse::<TestCli>(&["--tls-cert", "/certs/robot.pem"], &[]).is_err());
		assert!(parse::<TestCli>(&["--tls-key", "/certs/robot.key"], &[]).is_err());
	}

	#[test]
	fn data_params_fill_flags() {
		let cli: TestCli = parse(&[], &[]).unwrap();
		assert_eq!(
			(cli.format, cli.ros_type, cli.mode),
			(Format::Video, None, moqros::DataMode::Snapshot)
		);

		let cli: TestCli = parse(
			&[],
			&[
				("format", string("json")),
				("type", string("sensor_msgs/msg/Imu")),
				("mode", string("stream")),
			],
		)
		.unwrap();
		assert_eq!(cli.format, Format::Json);
		assert_eq!(cli.format.data(), Some(moqros::DataFormat::Json));
		assert_eq!(cli.ros_type.as_deref(), Some("sensor_msgs/msg/Imu"));
		assert_eq!(cli.mode, moqros::DataMode::Stream);

		let cli: TestCli = parse(&["--format", "cdr", "--type", "std_msgs/msg/String"], &[]).unwrap();
		assert_eq!(cli.format.data(), Some(moqros::DataFormat::Cdr));
		assert_eq!(cli.ros_type.as_deref(), Some("std_msgs/msg/String"));
		assert!(matches!(
			parse::<TestCli>(&[], &[("format", string("xml"))]),
			Err(ParseError::Param(_))
		));
	}

	#[derive(Debug, Parser)]
	struct EncoderCli {
		#[command(flatten)]
		encoder: EncoderArgs,
	}

	#[test]
	fn encoder_params_fill_flags() {
		let cli: EncoderCli = parse(&[], &[]).unwrap();
		assert_eq!(cli.encoder.settings().hardware, moqros::Hardware::Auto);

		let params = [("codec", string("av1")), ("hw", string("off"))];
		let settings = parse::<EncoderCli>(&[], &params).unwrap().encoder.settings();
		assert_eq!(
			(settings.codec, settings.hardware),
			(moqros::Codec::Av1, moqros::Hardware::Off)
		);

		let cli: EncoderCli = parse(&["--hw", "require"], &params).unwrap();
		assert_eq!(cli.encoder.hw, moqros::Hardware::Require);
		assert!(parse::<EncoderCli>(&["--hw", "gpu"], &[]).is_err());
	}

	#[derive(Debug, Parser)]
	struct RepublishCli {
		#[command(flatten)]
		republish: RepublishArgs,
	}

	#[test]
	fn republish_params_fill_flags() {
		let params = [
			("frame_id", string("camera")),
			("stamp", string("receive")),
			("max_height", ParameterValue::Integer(480)),
			("encoding", string("bgr8")),
		];
		let options = parse::<RepublishCli>(&[], &params).unwrap().republish.options();
		assert_eq!(
			options,
			crate::RepublishOptions {
				frame_id: Some("camera".into()),
				stamp: moqros::ros::StampSource::Receive,
				max_height: Some(480),
				encoding: moqros::PixelFormat::Bgr8,
			}
		);
		let cli: RepublishCli = parse(&["--max-height", "360"], &params).unwrap();
		assert_eq!(cli.republish.max_height, Some(360));
		assert!(parse::<RepublishCli>(&[], &[("encoding", string("yuyv"))]).is_err());
	}

	#[test]
	fn command_line_overrides_params() {
		let params = [
			("topic", string("/param")),
			("reliable", ParameterValue::Bool(true)),
			("insecure", ParameterValue::Bool(false)),
		];
		let cli: TestCli = parse(&["--topic", "/flag", "--reliable=false", "--insecure"], &params).unwrap();
		assert_eq!(cli.topic, "/flag");
		assert!(!cli.qos.reliable);
		assert!(cli.relay.insecure);
	}

	/// Setting a variable while other tests read the environment is undefined behavior,
	/// so this test reruns itself in a child process for each value of the variable.
	#[test]
	fn params_override_env() {
		const VAR: &str = "MOQROS_TEST_BITRATE";
		let bitrate = ("bitrate", ParameterValue::Integer(2));
		match std::env::var(VAR).ok().as_deref() {
			None => {
				for value in ["bogus", "4"] {
					let output = std::process::Command::new(std::env::current_exe().unwrap())
						.args(["--exact", "params::tests::params_override_env"])
						.env(VAR, value)
						.output()
						.unwrap();
					let stdout = String::from_utf8_lossy(&output.stdout);
					assert!(
						output.status.success() && stdout.contains("1 passed"),
						"{VAR}={value}:\n{stdout}"
					);
				}
			}
			// An invalid variable is never looked at when a parameter or flag is set.
			Some("bogus") => {
				assert_eq!(parse::<EnvCli>(&[], std::slice::from_ref(&bitrate)).unwrap().bitrate, 2);
				assert_eq!(parse::<EnvCli>(&["--bitrate", "3"], &[]).unwrap().bitrate, 3);
				assert!(matches!(parse::<EnvCli>(&[], &[]), Err(ParseError::CommandLine(_))));
				let help = help_or_version(EnvCli::command(), &args(&["--help"]));
				assert_eq!(help.map(|err| err.kind()), Some(ErrorKind::DisplayHelp));
			}
			Some(value) => {
				assert_eq!(value, "4");
				assert_eq!(parse::<EnvCli>(&[], &[]).unwrap().bitrate, 4);
				assert_eq!(parse::<EnvCli>(&[], std::slice::from_ref(&bitrate)).unwrap().bitrate, 2);
				assert_eq!(parse::<EnvCli>(&["--bitrate=3"], &[bitrate]).unwrap().bitrate, 3);
			}
		}
	}

	#[test]
	fn doubles_keep_their_fraction() {
		let cli: TestCli = parse(
			&[],
			&[
				("broadcast", ParameterValue::Double(1.0)),
				("fps", ParameterValue::Double(12.5)),
			],
		)
		.unwrap();
		assert_eq!(cli.broadcast.as_deref(), Some("1.0"));
		assert_eq!(cli.fps, 12.5);

		// Whole numbers still fit integer flags.
		let cli: TestCli = parse(&[], &[("keyframe_interval_ms", ParameterValue::Double(500.0))]).unwrap();
		assert_eq!(cli.keyframe_interval_ms, 500);

		// YAML `fps: 15` is an integer parameter.
		let cli: TestCli = parse(&[], &[("fps", ParameterValue::Integer(15))]).unwrap();
		assert_eq!(cli.fps, 15.0);
	}

	#[test]
	fn values_starting_with_a_dash_stay_values() {
		let cli: TestCli = parse(&[], &[("broadcast", string("-x"))]).unwrap();
		assert_eq!(cli.broadcast.as_deref(), Some("-x"));
	}

	#[test]
	fn unset_and_unknown_params_are_ignored() {
		let params = [
			("broadcast", ParameterValue::NotSet),
			("use_sim_time", ParameterValue::Bool(false)),
			("bitrat", ParameterValue::Integer(1)),
			("keyframe-interval-ms", ParameterValue::Integer(1)),
			// Clap's own arguments aren't parameters.
			("help", ParameterValue::Bool(true)),
			("version", string("2")),
		];
		assert!(flags::<TestCli>(&params).unwrap().is_empty());
	}

	#[test]
	fn arrays_fill_repeatable_flags() {
		let topics = |cli: MultiCli| cli.topic.into_iter().map(|spec| spec.topic).collect::<Vec<_>>();
		assert_eq!(topics(parse(&[], &[]).unwrap()), ["/image_raw"]);
		let params = [(
			"topic",
			ParameterValue::StringArray(vec!["/a".into(), "/b,format=json".into()]),
		)];
		let cli: MultiCli = parse(&[], &params).unwrap();
		assert_eq!(cli.topic[1].format, Some(Format::Json));
		assert_eq!(topics(cli), ["/a", "/b"]);
		// A single string still works, and the command line replaces the whole list.
		assert_eq!(topics(parse(&[], &[("topic", string("/c"))]).unwrap()), ["/c"]);
		assert_eq!(topics(parse(&["--topic", "/d"], &params).unwrap()), ["/d"]);
	}

	#[test]
	fn arrays_are_rejected() {
		let params = [("topic", ParameterValue::StringArray(vec!["/a".into()]))];
		let err = flags::<TestCli>(&params).unwrap_err();
		assert!(err.to_string().contains("`topic`"), "{err}");
	}

	#[test]
	fn invalid_param_values_are_blamed_on_the_param() {
		for (name, value) in [("keyframe_interval_ms", "soon"), ("url", "not a url")] {
			match parse::<TestCli>(&[], &[(name, string(value))]) {
				Err(ParseError::Param(err)) => assert_eq!(err.kind(), ErrorKind::ValueValidation, "{name}"),
				other => panic!("{name}: {other:?}"),
			}
		}
	}

	#[test]
	fn invalid_command_lines_are_blamed_on_the_command_line() {
		let params = [("keyframe_interval_ms", string("soon"))];
		for args in [&["--fps=fast"][..], &["--unknown"]] {
			assert!(
				matches!(parse::<TestCli>(args, &params), Err(ParseError::CommandLine(_))),
				"{args:?}"
			);
		}
	}

	#[test]
	fn detects_help_and_version() {
		let kind = |args_: &[&str]| help_or_version(TestCli::command(), &args(args_)).map(|err| err.kind());
		assert_eq!(kind(&["--help"]), Some(ErrorKind::DisplayHelp));
		assert_eq!(kind(&["-V"]), Some(ErrorKind::DisplayVersion));
		// Arguments after the help flag aren't looked at.
		assert_eq!(kind(&["-h", "--fps=fast"]), Some(ErrorKind::DisplayHelp));
		assert_eq!(kind(&["--topic", "/a"]), None);
		assert_eq!(kind(&["--fps=fast"]), None);
	}
}