GithubHelp home page GithubHelp logo

drateots / argparse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamolnng/argparse

0.0 1.0 0.0 40 KB

A simple C++ header only command line argument parser

License: GNU General Public License v3.0

C++ 96.87% CMake 3.13%

argparse's Introduction

argparse

Build Status

A simple header only command line argument parser

Usage

Here is a simple example

#include "argparse.h"

#include <iostream>
#include <iterator>

int main(int argc, char* argv[]) {
  // run as: [program name] "0 -c" abc -a 1 -sdfl --flag -v 1 2.7 3 4 9 8.12 87
  // [program name] -sdfv 1 -o "C:\Users\User Name\Directory - Name\file.dat" "C:\Users\User Name 2\Directory 2 - Name 2\file2.dat" C:/tmp/tmp.txt
  ArgumentParser parser("Argument parser example");
  parser.add_argument("-a", "an integer");
  parser.add_argument("-s", "an combined flag", true);
  parser.add_argument("-d", "an combined flag", true);
  parser.add_argument("-f", "an combined flag", true);
  parser.add_argument("--flag", "a flag");
  parser.add_argument("-v", "a vector", true);
  parser.add_argument("-l", "--long", "a long argument", false);
  parser.add_argument("--files", "input files", false);
  try {
    parser.parse(argc, argv);
  } catch (const ArgumentParser::ArgumentNotFound& ex) {
    std::cout << ex.what() << std::endl;
    return 0;
  }
  if (parser.is_help()) return 0;
  std::cout << "a: " << parser.get<int>("a") << std::endl;
  std::cout << "flag: " << std::boolalpha << parser.get<bool>("flag")
            << std::endl;
  std::cout << "d: " << std::boolalpha << parser.get<bool>("d") << std::endl;
  std::cout << "long flag: " << std::boolalpha << parser.get<bool>("l")
            << std::endl;
  auto v = parser.getv<double>("v");
  std::cout << "v: ";
  std::copy(v.begin(), v.end(), std::ostream_iterator<double>(std::cout, " "));
  double sum;
  for (auto& d : v) sum += d;
  std::cout << " sum: " << sum << std::endl;
  auto f = parser.getv<std::string>("files");
  std::cout << "files: ";
  std::copy(f.begin(), f.end(),
            std::ostream_iterator<std::string>(std::cout, " | "));
  std::cout << std::endl;
  f = parser.getv<std::string>("");
  std::cout << "free args: ";
  std::copy(f.begin(), f.end(),
            std::ostream_iterator<std::string>(std::cout, " "));
  std::cout << std::endl;
  return 0;
}

Compiling

Just add argparse.h to your include path. No longer requires compiler support for <regex>

argparse's People

Contributors

jamolnng avatar vadikrobot avatar vsirunyan 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.