Code Monkey home page Code Monkey logo

dsam's Introduction

DSAM

Dreamer's Simple Argument Manager (DSAM, for short) is a simplistic library that provides a better experience when dealing with command line arguments.

It provides a set of functions that allow to better deal with these arguments, as well as make it easy to add new commands and use them.

What does it allow me to do?

  • Define how many arguments the command will have
  • Define range-based arguments
  • Define the name of the command
  • Easily obtain an argument from it's index
  • Automatically handle help messages

What are range-based arguments?

To put it simply, the amount of arguments a command can have becomes a range, which reduces the amount of code required to define commands with many arguments.

How do I install this library?

Just download the rust source file and paste it on your project.

Example

mod dsam;

use dsam::*;

fn main() {
    // WARNING: Make sure that when creating the vector it is a vector of dsam::Command.
    let mut commands: Vec<Command> = Vec::new();
    // These first 3 are not range-based.
    commands.push(Command::new("command1", vec![1], false)); 
    commands.push(Command::new("command2", vec![1, 2], false));
    commands.push(Command::new("command3", vec![0], false));
    // The true makes this a range-based argument.
    // This command will be able to have between 1 and 5 (inclusive) arguments.
    commands.push(Command::new("command4", vec![1, 5], true));

    // Initialize an instance of the manager with the given commands.
    let manager = ArgumentManager::new(commands);

    // Check if the current command is valid (the library fetches it from std::env::args()), stopping the program if not.
    if !manager.is_command_valid() {
        panic!("Either the command is invalid or the amount of arguments is invalid.");
    }

    // Fetches the name of the command (0 for the command, 1 and beyond for the arguments).
    let command: &String = &manager.get_element(0);

    // Uses a match to decide what to do with each command.
    match command.as_str() {
        "command1" => {
            println!("Command 1 executed.");
        }
        "command2" => {
            println!("Command 2 executed.");
        }
        "command3" => {
            println!("Command 3 executed.");
        }
        "command4" => {
            println!("Command 4 executed.");
        }
        &_ => {
            panic!("Unknown command.");
        }
    }
}

dsam's People

Contributors

thedreamer123 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.