Skip to content

A C++ command-line argument parser. It supports subcommands, positional arguments, optional arguments and more

License

Notifications You must be signed in to change notification settings

kingsimba/nc-argparse

 
 

Repository files navigation

Introduction

nc-argparse is a very compact command line argument parser written in C++.

With very few lines of code, you can create an executable which supports UNIX style arguments, something like Git . And the code will be beautiful and maintainable.

For example, you can detect a optional flag:

int main(int argc, char** argv)
{
   ArgParser parser;
   parser.parse(argc, argv);

   if (parser.hasArg("h", "help"))
      return printHelp();
   ...
}

Or to get an argument with a default value:

parser.setDefault("mode", "fast");
const char* mode = parser.getArg("mode");

Demo Program

The demo provides a good start point for a command line program. It has merely 140 lines of code. But it can do all of the following things and more.

It supports "-h":

$ ./nc-argparse -h
An example program to demonstrate how to use argparse.

Syntax:

    argparse SUBCMD <OPTIONS>
    argparse -h/--help
    argparse SUBCMD -h/--help

Subcommands:

    compile     Compile a file into another file
    test        Run Google Test

It supports subcommands:

$ ./nc-argparse compile -h
Compile a source file into a target file.

Syntax:

    argparse compile SRC DEST <OPTIONS>

    SRC                 Source File
    DEST                Target File
    --mode MODE         "fast" or "slow"
    -i --interactive    Interactive mode

$ ./nc-argparse test
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from ArgParser
[ RUN      ] ArgParser.simpleOneArgument
[       OK ] ArgParser.simpleOneArgument (0 ms)
[ RUN      ] ArgParser.basic
[       OK ] ArgParser.basic (0 ms)
[ RUN      ] ArgParser.subcommand
[       OK ] ArgParser.subcommand (1 ms)
[----------] 3 tests from ArgParser (1 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 3 tests.

It supports positional arguments and optional arguments:

$ ./nc-argparse compile a.dat b.dat --mode fast -i
Compiling a.dat into b.dat in 'fast' mode ... Done

It supports detection of unknown arguments:

$ ./nc-argparse compile a.dat b.dat --mode fast -L
error: Unknown argument: L

About

A C++ command-line argument parser. It supports subcommands, positional arguments, optional arguments and more

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 99.9%
  • C 0.1%