Separate Opt into separate mod

This commit is contained in:
Ryan Geary 2020-03-15 16:43:25 -04:00
parent f87a3f1658
commit 47c9c73c4b
4 changed files with 38 additions and 30 deletions

View file

@ -80,7 +80,8 @@ impl Choice {
#[cfg(test)]
mod tests {
use crate::config::{Config, Opt};
use crate::config::Config;
use crate::opt::Opt;
use std::ffi::OsString;
use std::io::{self, BufWriter, Write};
use structopt::StructOpt;

View file

@ -1,36 +1,9 @@
use regex::Regex;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::process;
use structopt::StructOpt;
use crate::choice::Choice;
#[derive(Debug, StructOpt)]
#[structopt(name = "choose", about = "`choose` sections from each line of files")]
pub struct Opt {
/// Specify field separator other than whitespace
#[structopt(short, long)]
pub field_separator: Option<String>,
/// Use exclusive ranges, similar to array slicing in many programming languages
#[structopt(short = "x", long)]
pub exclusive: bool,
/// Activate debug mode
#[structopt(short, long)]
pub debug: bool,
/// Input file
#[structopt(short, long, parse(from_os_str))]
pub input: Option<PathBuf>,
/// Fields to print. Either x, x:, :y, or x:y, where x and y are integers, colons indicate a
/// range, and an empty field on either side of the colon continues to the beginning or end of
/// the line.
#[structopt(required = true, min_values = 1, parse(try_from_str = Config::parse_choice))]
pub choice: Vec<Choice>,
}
use crate::opt::Opt;
lazy_static! {
static ref PARSE_CHOICE_RE: Regex = Regex::new(r"^(\d*):(\d*)$").unwrap();

View file

@ -7,11 +7,13 @@ extern crate lazy_static;
mod choice;
mod config;
mod opt;
mod reader;
use config::Config;
use opt::Opt;
fn main() {
let opt = config::Opt::from_args();
let opt = Opt::from_args();
let config = Config::new(opt);
let read = match &config.opt.input {

32
src/opt.rs Normal file
View file

@ -0,0 +1,32 @@
use std::path::PathBuf;
use structopt::StructOpt;
use crate::choice::Choice;
use crate::config::Config;
#[derive(Debug, StructOpt)]
#[structopt(name = "choose", about = "`choose` sections from each line of files")]
pub struct Opt {
/// Specify field separator other than whitespace
#[structopt(short, long)]
pub field_separator: Option<String>,
/// Use exclusive ranges, similar to array slicing in many programming languages
#[structopt(short = "x", long)]
pub exclusive: bool,
/// Activate debug mode
#[structopt(short, long)]
pub debug: bool,
/// Input file
#[structopt(short, long, parse(from_os_str))]
pub input: Option<PathBuf>,
/// Fields to print. Either x, x:, :y, or x:y, where x and y are integers, colons indicate a
/// range, and an empty field on either side of the colon continues to the beginning or end of
/// the line.
#[structopt(required = true, min_values = 1, parse(try_from_str = Config::parse_choice))]
pub choice: Vec<Choice>,
}