Skip to content

djoezeke/Argparser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ARGPARSER

Modern C/C++ Argument Parser.

license last-commit repo-top-language repo-language-count


Table of Contents

πŸ“ Overview

Argparser is a lightweight and feature-rich command-line argument parsing library for C/C++. It offers a modern and intuitive interface allowing for straightforward argument configuration and parsing.

✨ Features

  • Parse keyword arguments
  • Parse positional arguments
  • Parse flags
  • Set default values for arguments
  • Print help messages

Argparser distinguishes 3 different types of arguments:

Type Functin Description
arg add_arg named positional arguments (e.g. file)
flag add_flag a boolean argument that is by default false (e.g. --verbose)
kwarg add_kwarg keyworded-arguments that require a key and a value, (e.g. --variable=5)

πŸ“ Supported Syntax

--help --verbose --input=file.txt
-hvi=file.txt
-h --verbose -i file.txt
--long value

πŸš€ Getting Started

β˜‘οΈ Prerequisites

Compiler Min Version
GNU G++ 13
Clang 17

βš™οΈ Installation

To use Argparser in your project, simply include the Argparser.h header file in your source files.

#include "Argparser.h"

πŸ€– Usage

#include "Argparser.h"

int main(int argc, char *argv[])
{
    ArgumentParser parser;

    init_parser(&parser, NULL, NULL, NULL, NULL, 1);

    add_flag(&parser, 'v', "verbose", "Enable verbose mode");
    add_flag(&parser, 's', "store", "Save file Name");
    add_kwarg(&parser, 'c', "count", 0, NULL, "Number of times");

    parse_args(&parser, argc, argv);

    int verbose = get_flag(&parser, "verbose");
    int store = get_flag(&parser, "store");
    int help = get_flag(&parser, "help");
    const char *count = get_kwarg(&parser, "count");

    if (help)
        print_help(&parser, 1, 1, 1, 1);
    if (count)
        printf("Count: %s\n", count);
    if (store)
        printf("Store: %d\n", store);
    if (verbose)
        printf("Verbose: %d\n", verbose);

    free_parser(&parser);

    return 0;
}

Example Help

$ ./sample --help
Welcome to Argparser
Usage: ./sample file.txt -v [FILE] [-h | -v ] [--output FILE]  [options...]
            file.txt : Output path [required]
            -v : Verbose Output [default: false]

Options:
               -k : An implicit int parameter [implicit: "3", required]
       -a,--alpha : An optional float parameter with default value [default: 0.6]
        -b,--beta : An optional float parameter with std::optional return [default: none]
     -n,--numbers : An int vector, comma separated [required]
          --files : multiple arguments [required]
       -c,--color : An Enum input [allowed: <red, blue, green>, required]
     -v,--verbose : A flag to toggle verbose [implicit: "true", default: false]
           --help : print help [implicit: "true", default: false]

πŸ§ͺ Testing

πŸ”° Contributing

Contributing Guidelines
  1. Fork the Repository: Start by forking the project repository to your github account.
  2. Clone Locally: Clone the forked repository to your local machine using a git client.
    git clone --recursive https://github.com/djoezeke/argparser
  3. Create a New Branch: Always work on a new branch, giving it a descriptive name.
    git checkout -b new-feature-x
  4. Make Your Changes: Develop and test your changes locally.
  5. Commit Your Changes: Commit with a clear message describing your updates.
    git commit -m 'Implemented new feature x.'
  6. Push to github: Push the changes to your forked repository.
    git push origin new-feature-x
  7. Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
  8. Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
Contributor Graph


πŸ™Œ Acknowledgments

Argparser is inspired by Python Argparse and aims to provide a simple and lightweight solution for C/C++ projects.

References

More Reading


πŸ“ƒ License

This project is protected under the MIT License. For more details, refer to the LICENSE file.

About

A Straight forward Command-line arguments parsing library for modern C/C++ that would make your day.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published