Code Monkey home page Code Monkey logo

cmdline's Introduction

cmdline - a tiny command line utility for c++

cmdline is a tiny simple command line utility for C++ (C++11 or more). cmdline parses given arguments by each operator() (operator[]) methods on demand, so no need to define strict type of variable for parsing beforehand.

  • Header-only
  • Tiny
  • Simple

Installation

Include using CMake

See CMakeLists.txt in this repository.

Include to source codes directry

Put the cmdline.h file to somewhere from the root directory, for example in the include directory on root, then specify the #include directive with the relative path from the souce code.

// In source code file
#include "./include/cmdline.h"
...
...

Include using compiler options

Use -I option, the directory to be added to the list of directories searched for include files.

See the below sample.

Usage

Sample Code

Save as sample.cpp

#include "cmdline.h"
#include <iostream>

int main(int argc, char *argv[]) {
  CmdLineParser cmd(argc, argv);

  // bool
  const bool b = cmd("--bool", false);
  const bool bnv = cmd["--bool-no-arg"];

  // string
  const std::string s = cmd("--str", "default string");

  // int
  const int i = cmd("--int", 789);

  // float
  const float f = cmd("--float", 789.12f);

  // double
  const double d = cmd("--double", 789.123);

  std::cout << cmd.dump() << std::endl;
}

Compile sample.cpp with g++ (C++11)

$ g++ -std=c++11 -I./include sample.cpp -o sample

Play

No arguments
$ ./sample

--bool : false
--bool-no-arg : false
--double : 789.123000
--float : 789.119995
--int : 789
--str : default string

With arguments
$ ./sample --bool true --bool-no-arg --double 123.456 --float 123.456 --int 123 --str value

--bool : true
--bool-no-arg : true
--double : 123.456000
--float : 123.456001
--int : 123
--str : value

cmdline's People

Contributors

atatb 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.