command-ifyer 0.1.4

A library for making command line interfaces out of libraries
Documentation
/*

Copyright 2025 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
/*

Copyright (c) 2025 𝕍𝕖𝕝𝕠𝕔𝕚𝕗𝕪𝕖𝕣

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/// Decides what to execute
/// Version must == 0 or == 1
/// Behaviour for version not being == 0 or == 1 is undefined
/// When it is executed it returns a number that is determined by comparing arg to command nicknames (capital insensitive) and if arg is in command nicknames it returns Some(i) where i == the index that it is at
/// Else if all_is_255 && arg == "all" than Some(255) is returned
/// Else if arg is a decimal integer it returns arg as a integer
/// Else it returns None if version == 0 or Some(245) if version == 1
/// # Examples
///
/// ```
/// asserteq!(Some(0),command_ifyer::what_to_execute(0,"fOO",&["Foo","Bar"],true));
/// asserteq!(Some(1),command_ifyer::what_to_execute(0,"1",&["hi","HeLlo"],false));
/// asserteq!(Some(255),command_ifyer::what_to_execute(0, "all", &["hi","HeLlo"]),true);
/// asserteq!(Some(3),command_ifyer::what_to_execute(0, "all", &["hi","HeLlo","All"], true))
/// asserteq!(None,command_ifyer::what_to_execute(0,"When is the Testificate Man full lenght movie going to come out?",&["Hi","HeLlo"],false))
/// ```
/// ```
/// let args: Vec<String> = std::env::args().collect();
/// let mut thing_to_execute = Some(255);
/// if args.len() > 1 {
///     thing_to_execute = command_ifyer::what_to_execute(0, &args[1], &["Foo", "Bar"], true);
/// }
/// if thing_to_execute == Some(0)|| thing_to_execute == Some(255) {
///    println("foo");
/// }
/// if thing_to_execute == Some(255) || thing_to_execute == Some(1) {
///     println!("bar");
/// }
/// ```
pub fn what_to_execute(
    version: u128,
    arg: &String,
    command_nicknames: &[&str],
    all_is_255: bool,
) -> Option<usize> {
    if version != 0 {
        panic!("917421232 version not supported.");
    }
    for i in 0..command_nicknames.len() {
        if arg.to_lowercase() == command_nicknames[i].to_lowercase() {
            return Some(i);
        }
    }
    if arg.to_lowercase() == "all".to_lowercase() && all_is_255 {
        return Some(255);
    }
    //    let parsed_arg = arg.trim().parse();
    if arg.trim().parse::<usize>().is_ok() {
        let more_parsed_arg: usize = arg.trim().parse().expect("129394860");
        return Some(more_parsed_arg);
    }
    None
}