Skip to content

Commit

Permalink
switch command line parsing libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
hlorenzi committed Oct 11, 2016
1 parent 354daba commit cb55973
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "customasm"
version = "0.1.0"
version = "0.2.0"
authors = ["Henrique Lorenzi <[email protected]>"]

[lib]
Expand All @@ -12,4 +12,4 @@ name = "customasm"
path = "src/main.rs"

[dependencies]
docopt = "0.6"
getopts = "0.2.14"
2 changes: 1 addition & 1 deletion src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Definition
else
{
Err(Error::new_with_span(
"invalid variable; use `_` as a the argument stand-in",
"invalid variable; use `_` as the argument stand-in",
span.clone()))
},

Expand Down
100 changes: 65 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
extern crate customasm;
extern crate docopt;
extern crate getopts;


use customasm::FileHandler;
use std::path::Path;
use std::process::exit;


const USAGE: &'static str = "
Usage:
customasm [options] <def_file> <asm_file> [<out_file>]
customasm -v | -h
Options:
-h, --help Display this information.
-v, --version Display version information.
-q, --quiet Do not print progress to stdout.
-f <format>, --format=<format> The format of the output file. Can be one of:
binary, binstr, hexstr, bindump, hexdump.
[default: hexdump]
";


enum OutputFormat
{
Binary,
Expand All @@ -34,32 +19,64 @@ enum OutputFormat

fn main()
{
let args = docopt::Docopt::new(USAGE)
.map(|d| d.help(true))
.map(|d| d.version(Some(format!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")))))
.and_then(|d| d.parse())
.unwrap_or_else(|e| e.exit());
let args: Vec<String> = std::env::args().collect();

let mut opts = getopts::Options::new();
opts.optflag("h", "help", "Display this information.");
opts.optflag("v", "version", "Display version information.");
opts.optflag("q", "quiet", "Suppress diagnostics and progress reports.");
opts.optopt("f", "format", "The format of the output file. Possible formats: binary, binstr, hexstr, bindump, hexdump", "FORMAT");

let matches = match opts.parse(&args[1..])
{
Ok(m) => m,
Err(f) => print_usage(true, &opts, Some(&format!("{}", f)))
};

if matches.opt_present("h")
{
print_usage(false, &opts, None);
}

if matches.opt_present("v")
{
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
return;
}

let quiet = args.get_bool("--quiet");
let quiet = matches.opt_present("q");

let out_format = match args.get_str("--format")
let out_format = match matches.opt_str("f").as_ref().map(|s| s.as_ref())
{
"binary" => OutputFormat::Binary,
"binstr" => OutputFormat::BinStr,
"hexstr" => OutputFormat::HexStr,
"bindump" => OutputFormat::BinDump,
"hexdump" => OutputFormat::HexDump,
_ => error_exit(customasm::Error::new("invalid output format"))
Some("binary") => OutputFormat::Binary,
Some("binstr") => OutputFormat::BinStr,
Some("hexstr") => OutputFormat::HexStr,
Some("bindump") => OutputFormat::BinDump,
Some("hexdump") |
None => OutputFormat::HexDump,
Some(_) => print_usage(true, &opts, Some("Invalid output format."))
};

let def_file = Path::new(args.get_str("<def_file>"));
let asm_file = Path::new(args.get_str("<asm_file>"));
let out_file = match args.get_str("<out_file>")
let (def_file, asm_file, out_file) =
{
"" => None,
f => Some(Path::new(f))
if matches.free.len() == 3
{
(Path::new(&matches.free[0]),
Path::new(&matches.free[1]),
Some(Path::new(&matches.free[2])))
}

else if matches.free.len() == 2
{
(Path::new(&matches.free[0]),
Path::new(&matches.free[1]),
None)
}

else
{ print_usage(true, &opts, None) }
};


let mut filehandler = customasm::RealFileHandler::new();

Expand Down Expand Up @@ -121,6 +138,19 @@ fn main()
}


fn print_usage(error: bool, opts: &getopts::Options, msg: Option<&str>) -> !
{
if let Some(msg) = msg
{
println!("{}", msg);
println!("");
}

println!("{}", opts.usage(&format!("Usage: customasm [options] <def-file> <asm-file> [<out-file>]")));
exit(if error { 1 } else { 0 });
}


fn error_exit(err: customasm::Error) -> !
{
err.print();
Expand Down

0 comments on commit cb55973

Please sign in to comment.