ascii_cli/
cli.rs

1use clap::{Parser, Subcommand};
2use std::io::BufRead;
3
4#[derive(Debug, Parser)]
5#[command(version, about)]
6pub struct Args {
7    #[command(subcommand)]
8    pub command: Option<SubCommands>,
9    /// The numbers base [max: 36] [default: 16]
10    #[arg(short, long)]
11    pub base: Option<u8>,
12}
13
14#[derive(Subcommand, Debug)]
15pub enum SubCommands {
16    /// Shows the Ascii table
17    Table {
18        /// The numbers base [max: 36]
19        #[arg(short, long, default_value_t = 16)]
20        base: u8,
21    },
22    ToChar {
23        /// The number(s) to be converted to a character
24        #[clap(num_args = 1.., value_delimiter = ' ')]
25        number: Option<Vec<String>>,
26        /// The numbers base [max: 36]
27        #[arg(short, long, default_value_t = 16)]
28        base: u8,
29        /// If it should print special characters as their name, or make them act as usual
30        /// (Example: 7 is BEL if false, or it will make a bell sound if true)
31        #[arg(short, long)]
32        ignore_specials: bool,
33    },
34    ToNum {
35        /// The char(s) to be converted to a character
36        #[clap(num_args = 1.., value_delimiter = ' ')]
37        char: Option<Vec<String>>,
38        /// The numbers base [max: 36]
39        #[arg(short, long, default_value_t = 16)]
40        base: u8,
41    },
42    WhatIs {
43        /// The number of the char you want to know more about, or the special characters name if
44        /// its a special character you want to know about (If so, use the -s flag)
45        character: Option<String>,
46        /// The numbers base [max is 36]
47        #[arg(short, long, default_value_t = 16)]
48        base: u8,
49        /// Set this flag if the string is the name of a special character, like CR
50        #[arg(short, long)]
51        special: bool,
52    },
53}
54
55impl Default for SubCommands {
56    fn default() -> Self {
57        Self::Table { base: 16 }
58    }
59}
60
61pub fn get_std_in() -> String {
62    std::io::stdin()
63        .lock()
64        .lines()
65        .fold("".to_string(), |acc, line| {
66            acc + &line.unwrap_or_default() + "\n"
67        })
68}