Skip to content

Commit

Permalink
feat: added subcommand for clap
Browse files Browse the repository at this point in the history
  • Loading branch information
Utilitycoder committed Sep 8, 2023
1 parent c1d3862 commit 2b2d9f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 36 deletions.
55 changes: 34 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
use clap::{App, Arg};
use std::num::ParseIntError;
use clap::{App, Arg, SubCommand};

use numberconverter::utils::convert;

fn main() -> Result<(), ParseIntError> {
/// This is the main function for the number converter program.
/// It uses the clap library to handle command line arguments and subcommands.
/// The program supports a "convert" subcommand that takes three arguments:
/// - base_convert_to: The base to convert the number to (e.g., 'binary', 'hex', 'octal')
/// - base_to_convert_from: The base of the number to be converted
/// - number_to_convert: The number to be converted
///
/// The convert function from the utils module is used to perform the conversion.
/// If the conversion is successful, the result is printed to the console.
fn main() -> Result<(), Box<dyn std::error::Error>> {
let matches = App::new("Number Converter")
.version("1.0")
.author("Utilitycoder")
.about("Converts numbers between different bases")
.arg(
Arg::with_name("base_convert_to")
.help("The type of base to convert to (e.g., 'binary', 'hex', 'octal')")
.required(true),
)
.arg(
Arg::with_name("base_to_convert_from")
.help("Specifies the base of the number you want to convert")
.required(true),
)
.arg(
Arg::with_name("number_to_convert")
.help("The number you want to convert")
.required(true),
.subcommand(
SubCommand::with_name("convert")
.arg(
Arg::with_name("base_convert_to")
.help("The type of base to convert to (e.g., 'binary', 'hex', 'octal')")
.required(true),
)
.arg(
Arg::with_name("base_to_convert_from")
.help("Specifies the base of the number you want to convert")
.required(true),
)
.arg(
Arg::with_name("number_to_convert")
.help("The number you want to convert")
.required(true),
),
)
.get_matches();

let base_convert_from = matches.value_of("base_to_convert_from").unwrap();
let base_convert_to = matches.value_of("base_convert_to").unwrap();
let number_str = matches.value_of("number_to_convert").unwrap();
if let Some(matches) = matches.subcommand_matches("convert") {
let base_convert_from = matches.value_of("base_to_convert_from").unwrap();
let base_convert_to = matches.value_of("base_convert_to").unwrap();
let number_str = matches.value_of("number_to_convert").unwrap();

let _ = convert(base_convert_to, number_str, base_convert_from);
convert(base_convert_to, number_str, base_convert_from)?;
}

Ok(())
}
42 changes: 27 additions & 15 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
use num_base::Based;

fn get_base_value(base: &str) -> usize {
// This function returns the base value for a given string representation of the base.
// If the base is not supported, it returns an error.
fn get_base_value(base: &str) -> Result<usize, &'static str> {
match base {
"binary" => 2,
"hex" => 16,
"octal" => 8,
"decimal" => 10,
_ => {
panic!("Unsupported base: {}", base);
}
"binary" => Ok(2),
"hex" => Ok(16),
"octal" => Ok(8),
"decimal" => Ok(10),
_ => Err("Unsupported base"),
}
}

// This function converts a number from one base to another.
// It takes three arguments:
// - base_convert_to: the base to convert the number to
// - number_str: the number to convert, as a string
// - base_convert_from: the base of the number to convert
// It returns a Result. If the conversion is successful, it returns Ok. If an error occurs, it returns Err.
pub fn convert(
base_convert_to: &str,
number_str: &str,
base_convert_from: &str,
) -> Result<(), std::num::ParseIntError> {
let convert_from = get_base_value(base_convert_from);
let convert_to = get_base_value(base_convert_to);
) -> Result<(), Box<dyn std::error::Error>> {
// Get the base values for the input and output bases
let convert_from = get_base_value(base_convert_from)?;
let convert_to = get_base_value(base_convert_to)?;

let number = Based::new(number_str, convert_from).to(convert_to).unwrap();
// Convert the number from the input base to the output base
let number = Based::new(number_str, convert_from)
.to(convert_to)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", e)))?;

// Print the converted number in the appropriate format
match base_convert_to {
"binary" => {
println!("Binary: {}", number.val);
Expand All @@ -44,8 +55,9 @@ pub fn convert(
}

mod tests {
use super::*;

#[allow(unused_imports)]
use super::convert;

#[test]
fn test_convert_binary_to_decimal() {
convert("decimal", "10", "binary").unwrap();
Expand Down Expand Up @@ -100,4 +112,4 @@ mod tests {
fn test_convert_decimal_to_hex() {
convert("hex", "10", "decimal").unwrap();
}
}
}

0 comments on commit 2b2d9f8

Please sign in to comment.