All Projects → fmenozzi → argparser

fmenozzi / argparser

Licence: MIT license
Simple command-line parser for C/C++ programs

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to argparser

option-parser
A Lightweight, header-only CLI option parser for C++
Stars: ✭ 16 (-68%)
Mutual labels:  argument-parser, option-parser, command-line-parser
declarative-parser
Modern, declarative argument parser for Python 3.6+
Stars: ✭ 31 (-38%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Argagg
A simple C++11 command line argument parser
Stars: ✭ 180 (+260%)
Mutual labels:  argument-parser, option-parser, command-line-parser
dropt
dropt is yet another C library for parsing command-line options.
Stars: ✭ 39 (-22%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Programoptions.hxx
Single-header program options parsing library for C++11
Stars: ✭ 112 (+124%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Kotlin Argparser
Easy to use and concise yet powerful and robust command line argument parsing for Kotlin
Stars: ✭ 431 (+762%)
Mutual labels:  argument-parser, option-parser, command-line-parser
CmdLine2
Command line argument parser (C++14)
Stars: ✭ 18 (-64%)
Mutual labels:  argument-parser, option-parser, command-line-parser
Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+3216%)
Mutual labels:  argument-parser, option-parser, command-line-parser
command-line-commands
Add a git-like command interface to your app.
Stars: ✭ 40 (-20%)
Mutual labels:  option-parser, command-line-parser
Console CommandLine
Full featured command line options and arguments parser.
Stars: ✭ 19 (-62%)
Mutual labels:  argument-parser, option-parser
minimist2
TypeScript/JavaScript ES6 rewrite of popular Minimist argument parser
Stars: ✭ 20 (-60%)
Mutual labels:  argument-parser, command-line-parser
Caporal.js
A full-featured framework for building command line applications (cli) with node.js
Stars: ✭ 3,279 (+6458%)
Mutual labels:  argument-parser, command-line-parser
Argparse
Argparse for golang. Just because `flag` sucks
Stars: ✭ 294 (+488%)
Mutual labels:  argument-parser, command-line-parser
args
Simple and type-safe commandline argument parser for C++14
Stars: ✭ 63 (+26%)
Mutual labels:  argument-parser, command-line-parser
go-getoptions
Fully featured Go (golang) command line option parser with built-in auto-completion support.
Stars: ✭ 41 (-18%)
Mutual labels:  argument-parser, option-parser
Argumentparser
Faster, easier, more declarative parsing of command line arguments in Objective-C/Foundation.
Stars: ✭ 251 (+402%)
Mutual labels:  argument-parser, command-line-parser
Command Line Args
A mature, feature-complete library to parse command-line options.
Stars: ✭ 525 (+950%)
Mutual labels:  option-parser, command-line-parser
Clap
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
Stars: ✭ 7,174 (+14248%)
Mutual labels:  argument-parser, command-line-parser
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+1274%)
Mutual labels:  argument-parser, option-parser
Argh
Argh! A minimalist argument handler.
Stars: ✭ 752 (+1404%)
Mutual labels:  argument-parser, command-line-parser

NOTE: The old C code is located in the c/ directory. While it's still functional (as far as I can tell), it should be considered more-or-less deprecated for use in C++ programs.

Simple command-line parser for C++11 programs. See test/ for info on running test suite.

Example

example.cpp:

#include <iostream>
#include <string>

#include "argparser.hpp"

int main(int argc, char* argv[]) {
    ap::parser p(argc, argv);
    p.add("-f", "--firstname",  "First name",     ap::mode::REQUIRED);
    p.add("-l", "--lastname",   "Last name",      ap::mode::REQUIRED);
    p.add("-v", "--verbose",    "Verbose output", ap::mode::BOOLEAN);
    p.add("-s", "--salutation", "Salutation");    // Defaults to ap::mode::OPTIONAL
    
    auto args = p.parse();

    if (!args.parsed_successfully()) {
	std::cerr << "Unsuccessful parse\n";	
        return -1;
    }
   
    // Index into map with either shortarg or longarg 
    auto first = args["-f"];
    auto last  = args["--lastname"];

    // Optional args are empty if not specified
    auto salutation = (args["-s"].empty() ? "Hello" : args["-s"]);
    
    if (std::stoi(args["-v"])) {
        // Verbose output
        std::cout << salutation << ", " << first << " " << last << "!\n";
    } else {
        // Regular output
        std::cout << salutation << ", " << first << "\n";
    }
    
    return 0;
}

Compile:

g++ example.cpp -o example -std=c++11

Run:

$ ./example -h
Usage: ./example [-h,--help] -f,--firstname -l,--lastname [-v,--verbose] [-s,--salutation] 

Arguments:
    -h, --help          Show this help message and exit
    -f, --firstname     First name
    -l, --lastname      Last name
    -v, --verbose       Verbose output
    -s, --salutation    Salutation

$ ./example -f John --lastname=Doe
Hello, John

$ ./example -f John --lastname=Doe -v
Hello, John Doe!

$ ./example -f John --lastname=Doe -v --salutation=Hey
Hey, John Doe!

$ ./example
Unsuccessful parse

$ ./example -f John
Unsuccessful parse

$ ./example -l Doe
Unsuccessful parse

Details

  • ap::parser::parse() returns map-like object that provides read-only access to parsed arguments
  • Args have one of three modes
    • ap::mode::OPTIONAL: Arg may or may not be populated (default mode)
      • Unused args are assigned the empty string
    • ap::mode::REQUIRED: Failure to populate arg results in unsuccessful parse
    • ap::mode::BOOLEAN: Used to track presence or absence of arg (i.e. does not populate value)
      • Value in resulting map is "1" if arg was passed and "0" otherwise
  • Args can be assigned either with a space or an equals sign
    • In above example, -f, --firstname can be assigned values using any of the following schemes:
      • ./example -f John
      • ./example -f=John
      • ./example --firstname John
      • ./example --firstname=John
  • Boolean flags can be chained together
    • e.g. if the Boolean shortargs -a, -b, -c are added, then any of the following invocations of the program are valid:
      • ./example -a -b -c
      • ./example -abc
      • ./example -ab -c
  • Rules for arg string formatting (failure to abide by rules results in unsuccessful parse):
    • Short arg must be a single dash - followed by a single character
      • Valid: -a
      • Invalid: -, a, --a, -aa
    • Long arg must be two dashes -- followed by any number of additional characters
      • Valid: --l, --longarg
      • Invalid: longarg, -longarg, -, --
    • One of either short arg or long arg (but not both) may be the empty string
    • The help string is not optional (i.e. cannot use empty string)
    • Arg strings -h, --help are reserved
  • A help string is automatically constructed (and corresponding help args -h and --help automatically provided)
    • Attempting to manually add arg strings -h, --help will result in a parse failure
    • If help args are passed, help string is displayed and the program is terminated
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].