Coder Social home page Coder Social logo

args's Introduction

Args

Simple and typesafe commandline parsing for C++14.

Quickstart

Simply provide a class with fields that is to be filled by command-line arguments:

struct hello
{
    static const char* help()
    {
        return "Simple program that greets NAME for a total of COUNT times.";
    }
    int count;
    std::string name;

    hello() : count(1)
    {}

    template<class F>
    void parse(F f)
    {
        f(count, "--count", "-C", args::help("Number of greetings."));
        f(name, "--name", "-N", args::help("The person to greet."), args::required());
    }

    void run()
    {
        for(int i=0;i<count;i++) printf("%s\n", name.c_str());
    }
};

int main(int argc, char const *argv[]) 
{
    args::parse<hello>(argc, argv);
}

The command then could be run like this:

$ hello --name Paul --count 3
Paul
Paul
Paul

The args library will auto-generate help info:

$ hello --help
Usage: hello [options...]

  Simple program that greets NAME for a total of COUNT times.  

Options: 

            -h, --help Show help  
 --count, -C [integer] Number of greetings.  
   --name, -N [string] The person to greet.

In addition, nested commands can be created:

struct cli : args::group<cli>
{
    static const char* help()
    {
        return "Command-line interface to manage a database";
    }
};

struct initdb : cli::command<initdb>
{
    initdb() {}
    static const char* help()
    {
        return "Initialize database";
    }
    void run()
    {
        printf("Initialize database\n");
    }
};

struct dropdb : cli::command<dropdb>
{
    dropdb() {}
    static const char* help()
    {
        return "Delete database";
    }
    void run()
    {
        printf("Delete database\n");
    }
};

int main(int argc, char const *argv[]) 
{
    args::parse<cli>(argc, argv);
}

So each subcommand can be ran like this:

$ cli initdb
Initialize database
$ cli dropdb
Delete database

In addition help is generated for subcommands as well:

$ cli --help
Usage: cli [options...] [command]

  Command-line interface to manage a database  

Options: 

 -h, --help Show help  

Commands: 

     dropdb Delete database  
     initdb Initialize database  

args's People

Contributors

pfultz2 avatar dmry avatar pavelosipov 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.