Skip to content

Commit 8d6e999

Browse files
committed
varlink_generator: update cli and generate new testsuite output
1 parent befa40e commit 8d6e999

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

varlink_generator/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ failure = "0.1.2"
2828
varlink_parser = { version = "2.1", path = "../varlink_parser" }
2929
quote = "0.6"
3030
proc-macro2 = "0.4"
31+
getopts = "0"
3132

3233
[badges]
3334
travis-ci = { repository = "varlink/rust" }

varlink_generator/src/bin/varlink-rust-generator.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,51 @@ extern crate varlink_generator;
1414
use std::env;
1515
use std::fs::File;
1616
use std::io;
17+
use std::io::{Error, ErrorKind};
1718
use std::io::{Read, Write};
1819
use std::path::Path;
20+
1921
use varlink_generator::{generate, Result};
2022

23+
fn print_usage(program: &str, opts: &getopts::Options) {
24+
let brief = format!("Usage: {} [VARLINK FILE]", program);
25+
print!("{}", opts.usage(&brief));
26+
}
27+
2128
fn main() -> Result<()> {
2229
let args: Vec<_> = env::args().collect();
23-
let mut reader: Box<Read> = match args.len() {
24-
0 | 1 => Box::new(io::stdin()),
30+
let program = args[0].clone();
31+
32+
let mut opts = getopts::Options::new();
33+
opts.optflag("h", "help", "print this help menu");
34+
opts.optflag("", "nosource", "don't print doc header and allow");
35+
36+
let matches = match opts.parse(&args[1..]) {
37+
Ok(m) => m,
38+
Err(f) => {
39+
eprintln!("{}", f.to_string());
40+
print_usage(&program, &opts);
41+
return Err(Error::from(ErrorKind::InvalidData).into());
42+
}
43+
};
44+
45+
if matches.opt_present("h") {
46+
print_usage(&program, &opts);
47+
return Ok(());
48+
}
49+
50+
let tosource = !matches.opt_present("nosource");
51+
52+
let mut reader: Box<Read> = match matches.free.len() {
53+
0 => Box::new(io::stdin()),
2554
_ => {
26-
if args[1] == "-" {
55+
if matches.free[0] == "-" {
2756
Box::new(io::stdin())
2857
} else {
29-
Box::new(File::open(Path::new(&args[1]))?)
58+
Box::new(File::open(Path::new(&matches.free[0]))?)
3059
}
3160
}
3261
};
3362
let writer: &mut Write = &mut io::stdout();
34-
generate(&mut reader, writer, true)
63+
generate(&mut reader, writer, tosource)
3564
}

varlink_generator/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,10 @@ fn varlink_to_rust(
357357

358358
if tosource {
359359
ts.extend(quote!(
360-
#![doc = "This file was automatically generated by the varlink rust generator" ]
361-
#![allow(non_camel_case_types)]
362-
#![allow(non_snake_case)]
363-
));
360+
#![doc = "This file was automatically generated by the varlink rust generator" ]
361+
#![allow(non_camel_case_types)]
362+
#![allow(non_snake_case)]
363+
));
364364
}
365365

366366
ts.extend(quote!(

0 commit comments

Comments
 (0)