GithubHelp home page GithubHelp logo

hutorny / simplearg Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 8 KB

SimpleArg is a header-only library for dispatching argc, argv to a user's class.

License: MIT License

C++ 100.00%
argc argv argv-parser header-only-library simple

simplearg's Introduction

SimpleArg

Brief

SimpleArg is a header-only library for dispatching argc, argv to a user's class. It does not impose any constraints on how the arguments may look like - with a double dash, a single dash, or no dash at all. The only assumption it makes is about = character, which is treated as an option/value delimiter, if such option syntax is used in the application. It also provides a simple solution for parsing configuration data by breaking the data it into a vector of arguments with str2argv function

Quick Start Guide

Immediate Use

SimpleArg can be used without a perdefined set of parameters

#include <simplearg/arguments.h>
int main(int argc, char* argv[]) {
    using namespace simplearg;
    Arguments args{argc-1, argv+1};
    if(args.contains("--help")) {
        std::cout << "Usage: ...\n";
        return 0;
    }
    
    std::string str {};
    unsigned val {};
    if(args.getall(val, str)) {
      std::cout << "Positional parameters:" << val << ',' << str << '\n';
    } else {
      std::cerr << args.errors() << '\n';
      return 1;
    }
    // ...
    return 0;
}

Dispatched Use

This mode requires some preparations steps:

1. Define a class that would be used as the dispatcher:

#include <simplearg/arguments.h>
using namespace simplearg;
struct OptionDispatcher {
    std::string myopt {};
    bool myoption(std::string_view name, Arguments& args) {
        return args.get(myopt);
    }
    bool mycommand(std::string_view name, Arguments& args) {
        int num {};
        std::string str {};
        if (! args.getall(num, str))
            return false;
        // using num, str ...
        return true;
    }
    bool help(std::string_view name, Arguments& args) {
        std::cout << "Usage: ...\n";
        return true;        
    }
    
};

2. Define option names

static constexpr simplearg::Parameters<OptionDispatcher, 3> myparams = {{
    { &OptionDispatcher::myoption,            // Method to be invoked
      "--myoption=",                          // Option name
      "a named option with a value",          // Description
      "-o= --o=" },                           // Space delimited aliases
    { &OptionDispatcher::mycommand,           // Method to be invoked
      "mycommand",                            // Verb name
      "a positional command with positional parameters", 
      "c"},
    { &OptionDispatcher::help, "help", "prints this help", "--help -h -?" },
}};
  • Note : dispatcher with an empty option name will be used as a fallback and called for all arguments, not matched with any other option

3. Parse arguments:

int main(int argc, char* argv[]) {
    Arguments args{argc-1, argv+1};
    OptionDispatcher od {};
    if (!args.parse(od, myparams)) {
        std::cerr << args.errors() << '\n';
        return 1;
    }
    //...
    return 0;
}

4. Printing Help

SimpleArg facilitates a print function that prints parameters with their descriptions:

print(std::cout << "Usage:\n", myparams);

simplearg's People

Contributors

hutorny avatar

Watchers

 avatar  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.