Skip to content

Commit

Permalink
feat: parsing the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
moz-sec committed Apr 30, 2024
1 parent b1a8239 commit 50a76df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 52 deletions.
42 changes: 42 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::path::PathBuf;

use clap::{Parser, ValueEnum};

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Compress or Decompress files name
#[arg(required = true)]
pub files: Vec<PathBuf>,

/// Whether to decompress the input
#[arg(short, long)]
decompress: bool,

/// Put passwords on compressed files
#[arg(short, long)]
encrypt: bool,

#[arg(short, long)]
#[clap(value_enum, default_value_t=Format::Zip)]
pub format: Format,

/// Whether to recursively greet
#[arg(short, long)]
recursive: bool,

/// Verbose mode
#[arg(short, long)]
verbose: bool,

/// Number of compressions to perform
#[arg(short, long, default_value = "6")]
pub count: u32,
}

#[derive(ValueEnum, Clone, Debug)]
pub enum Format {
Gzip,
Zip,
}
66 changes: 14 additions & 52 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,27 @@
use clap::{Parser, ValueEnum};
use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Compress or Decompress file name
#[arg(value_name = "FILE")]
file: String,

/// Multiple File Designation
#[arg(last=true)]
multiple_files: Vec<String>,

/// Whether to decompress the input
#[arg(short, long)]
decompress: bool,

/// Put passwords on compressed files
#[arg(short, long)]
encrypt: bool,

#[arg(short, long)]
#[clap(value_enum, default_value_t=Format::Zip)]
format: Format,

/// Whether to recursively greet
#[arg(short, long)]
recursive: bool,

/// Verbose mode
#[arg(short, long)]
verbose: bool,

/// Number of compressions to perform
#[arg(short, long, default_value = "6")]
count: u32,
}

#[derive(ValueEnum, Clone, Debug)]
enum Format {
Gzip,
Zip,
}

fn hello() -> String {
"Hello, world!".to_string()
}
mod cli;

fn main() {
let _args = Args::parse();
let args = cli::Args::parse();

println!("Hello, world!");
println!("{}", hello());
print!("{:?}", args);
}

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use super::*;

#[test]
fn test_hello() {
assert_eq!(hello(), "Hello, world!");
fn test_run() {
let opts =
cli::Args::parse_from(&["unicom", "-f", "zip", "-r", "-c", "8", "file1", "file2"]);
assert_eq!(
opts.files,
vec![PathBuf::from("file1"), PathBuf::from("file2")]
);
assert_eq!(opts.count, 8);
}
}

0 comments on commit 50a76df

Please sign in to comment.