Skip to content

Commit

Permalink
rust: use cookbook pattern to parse cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Jun 23, 2024
1 parent d1148cc commit 700240a
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions rust/semantic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::io::{BufReader, BufWriter, Read, stdin, stdout};
use std::path::Path;

use anyhow::{anyhow, Context, Result};
use clap::{arg, ArgAction, command};
use clap::{arg, command, ArgAction, Command, ArgMatches};
use json_compilation_db::Entry;
use log::LevelFilter;
use serde_json::Error;
Expand All @@ -43,7 +43,8 @@ mod filter;
mod fixtures;

fn main() -> Result<()> {
let arguments = Arguments::parse()?;
let matches = cli().get_matches();
let arguments = Arguments::from_matches(matches)?;
prepare_logging(arguments.verbose)?;

let application = Application::configure(arguments)?;
Expand All @@ -52,6 +53,23 @@ fn main() -> Result<()> {
Ok(())
}

fn cli() -> Command {
command!()
.args(&[
arg!(-i --input <FILE> "Path of the event file")
.default_value("commands.json")
.hide_default_value(false),
arg!(-o --output <FILE> "Path of the result file")
.default_value("compile_commands.json")
.hide_default_value(false),
arg!(-c --config <FILE> "Path of the config file"),
arg!(-a --append "Append result to an existing output file")
.action(ArgAction::SetTrue),
arg!(-v --verbose ... "Sets the level of verbosity")
.action(ArgAction::Count),
])
}

#[derive(Debug, PartialEq)]
struct Arguments {
input: String,
Expand All @@ -62,23 +80,7 @@ struct Arguments {
}

impl Arguments {
fn parse() -> Result<Self> {
let matches = command!()
.args(&[
arg!(-i --input <FILE> "Path of the event file")
.default_value("commands.json")
.hide_default_value(false),
arg!(-o --output <FILE> "Path of the result file")
.default_value("compile_commands.json")
.hide_default_value(false),
arg!(-c --config <FILE> "Path of the config file"),
arg!(-a --append "Append result to an existing output file")
.action(ArgAction::SetTrue),
arg!(-v --verbose ... "Sets the level of verbosity")
.action(ArgAction::Count),
])
.get_matches();

fn from_matches(matches: ArgMatches) -> Result<Self> {
Arguments {
input: matches.get_one::<String>("input")
.expect("input is defaulted")
Expand Down

0 comments on commit 700240a

Please sign in to comment.