Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Command line parser for C++17.

License

NotificationsYou must be signed in to change notification settings

jermp/cmd_line_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeQL

Command Line Parser for C++17

This is a single-header command line parser for C++17.

It offers, basically, the same funcionalities as other popular libraries, such ascxxopts,cmdline andargparse,but relies on the powerfulif constexpr construct of C++17 instead of dynamic casting.This results in a very compact code (~150 sloc).

Integration

Just add the fileinclude/parser.hpp to your own project.

Or if you use git:

git submodule add https://github.com/jermp/cmd_line_parser.git

Example

#include<iostream>#include"../include/parser.hpp"voidconfigure(cmd_line_parser::parser& parser) {    parser.add("perc",// name"A percentage value.",// description"-p",// shorthandtrue,// required argumentfalse// not boolean option (default is false)    );    parser.add("input_filename","An input file name.","-i",true);    parser.add("output_filename",// name"An output file name.",// description"-o",// shorthandfalse,false);    parser.add("num_trials","Number of trials.","-n",false,false);    parser.add("sorted","Sort output.","--sort",false,true// boolean option: a value is not expected after the shorthand    );    parser.add("buffered","Buffer input.","--buffer",false,true);    parser.add("ram","Amount of ram to use.","--ram",false,false);}intmain(int argc,char** argv) {// declare the parser    cmd_line_parser::parserparser(argc, argv);// configure the parserconfigure(parser);// parse command line and return on failurebool success = parser.parse();if (!success)return1;// now get some variablesauto perc = parser.get<float>("perc");// deduced type is floatauto input_filename =// deduced type is std::string        parser.get<std::string>("input_filename");auto sorted_output = parser.get<bool>("sorted");// deduced type is boolauto buffered_input = parser.get<bool>("buffered");// deduced type is boolsize_t ram =999;// some default valueif (parser.parsed("ram")) {        ram = parser.get<size_t>("ram");// deduced type is size_t    }    std::cout <<"perc:" << perc << std::endl;    std::cout <<"input_filename:" << input_filename << std::endl;    std::cout <<"sorted_output:" << sorted_output << std::endl;    std::cout <<"buffered_input:" << buffered_input << std::endl;    std::cout <<"ram:" << ram << std::endl;try {auto val = parser.get<int>("bar");// fail: no name 'bar' was specified        (void)val;// shut up, compiler!    }catch (std::runtime_errorconst& e) {        std::cerr << e.what() << std::endl;return1;    }return0;}

To compile and run the example, do as follows.

cd srcmkdir -p buildcd buildcmake ..make./example

Also run./test_parse for some testing.

Interface

/* Constructor.*/parser(int argc,char** argv);/* Parse command line; return false on failure.*/boolparse();/* Print help message.*/voidhelp()const;/* Add an argument with a name, a description, and a shorthand.   Return false if an argument with the same name already exists.   The last two boolean parameters are used to specify if the argument   is to be required and to be considered a boolean argument (no value after   the shorthand).*/booladd(std::stringconst& name, std::stringconst& descr,          std::stringconst& shorthand,bool is_required,bool is_boolean =false);/* Return true is an option with the given name was parsed; false otherwise.*/boolparsed(std::stringconst& name)const;/* Get a variable of type T by name.   It throws an exception if an argument with the specified name   is not found.*/template<typename T>Tget(std::stringconst& name)const;

About

Command line parser for C++17.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp