Giter VIP home page Giter VIP logo

cyanargparser's Introduction

CyanArgParser - My Argument Parser

This is a simple C++17 argument parser made mainly for my own use as well as practice, hence its design and featureset will probably only appease me. I have however attempted to make the interface as intuitive as possible.

How to use

You must always declare a parser instance, I like to enclose all of my code beneath my namespace cyan and then its library.

#include "argParser.hpp"

//You will probably find it convienent to use a namespace alias
namespace cap = cyan::argParser;

int main(int argc, char* argv[]) {
	cap::Parser parser;
}

Creating and customizing arguments

You create arguments be calling the AddArgument function of the parser instance, you then may call any number of optional functions on the argument itself to customize it.

parser.AddArgument("verboseLogging")//name of the setting
    ->AddAlias("--v")//how it can be accessed from the command line
    ->AddAlias("--verbose");//an alternate alias

parser.AddArgument("writeToFile")
    ->AddAlias("--f");

Input

Currently the only other option is SetInput, which means the argument is accompanied with a string value from the command line. You must supply a default value in case it is unset.

parser.AddArgument("memoryLimit")
    ->AddAlias("--m")
    ->SetInput("2048");

Arguments with values are set by default using the tilde character (~).

app.exe --m~1024

You may change this character by using a parser constructor overload:

int main(int argc, char* argv[]) {
	cap::Parser parser('=');
}

Parsing arguments

After declaring your arguments, you must then call the Parse function of the parser instance, passing in the main arguments. It will return an std::map of your schema that you can access.

auto args = parser.Parse(argc, argv);

if (args.at("verboseLogging")->set) {
    std::cout << "verbose is set" << std::endl;
}

if (args.at("writeToFile")->set) {
    std::cout << "verbose is set" << std::endl;
}

if (args.at("memoryLimit")->set) {
    std::string val = std::any_cast<std::string>(args.at("memoryLimit")->GetValue());
    std::cout << "Memory: " << val << std::endl;
}

Building

This project uses CMake, you can convert it to a visual studio project to build it using the CMake-gui program, nothing weird should happen, I've tested it.

cyanargparser's People

Contributors

2004seraph 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.