Beginner's Guide
    First Steps
    Input & Output
    Basic Custom Types
    Diagnostics
    Standard Library
    Code Organization
    Powerful Custom Types
    Generic Programming
    Memory Management
    Software Design Basics

    Command Line ArgumentsCommand Line ArgumentsCmd.Line Args

    What & Why?

    • space-separated strings behind program call
    • used to send information to a program when it starts
    • especially useful for automation/scripting
    $ ls
    $ rm *.txt
    $ find -name *.jpg
    $ g++ -o out xy.cpp

    How To Access in C++ (and C) How To Access

    main.cpp
    #include <iostream>
    int main (int argc, char* argv[]) {
      for(int i = 0; i < argc; ++i) {
        std::cout << argv[i] << '\n';
      }
    }
    $ g++ -o exe main.cpp
    $ ./exe 12 abc -z 3
    ./exe
    12
    abc
    -z
    3
    compile program 'exe'
    run 'exe' with argsarguments
    argv[0]  program call itself
    argv[1]  1st argument
    argv[2]
    argv[3]
    argv[4]
    • names argc and argv are only a convention
    • each element of argv is a C-string: a C-array of char
    • argv itself is a C-array of C-strings
    • argv[0] contains the program call (platform dependent)

    Conversion to std::string, int, … Argument Conversion Conversion

    int main (int argc, char* argv[]) {
      if(argc < 3) {
        cerr << "Usage:\ " << argv[0]    << " <word> <times>\\n";
        return EXIT_FAILURE;
      }
      auto word  = string(argv[1]);
      int  times = atoi(argv[2]);
    
      for(int i = 0; i < times; ++i) {
        cout << word << ' ';
      }
      cout << '\n';
    }
    $ ./say 
    Usage: ./say <word> <times>
    $ ./say ho! 3
    ho! ho! ho!
    execute without args
    too few args ⇒ usage message
    execute with args
    program output

    String → Number Conversion Functions String Conversions String Conversion

    Command Line Argument Parsing Libraries Args Parsing Libraries Libraries

    Library
    CLI formats
    generates usage
    handles errors
    opinions
    Argument Aggregator fixed
    • convoluted syntax
    • hard to build complex CLIs
    Argh! arbitrary
    • minimalistic
    • easy to use
    argparse fixed
    • C Library
    • hard to build complex CLIs
    Args flexible
    • convoluted syntax
    • hard to build complex CLIs
    Argtable fixed
    • C Library
    • hard to build complex CLIs
    • CLI def. hard to read
    Boost Program Options flexible
    • mature
    • supports config files
    • C++98-era syntax & technical debt
    CLI11 fixed
    • extensive feature set
    • supports config files
    • complex CLI defs. hard to read
    CLIPP arbitrary
    • relatively nice-looking, DSL-like syntax
    • complex CLIs possible
    • error handling could be better
    • not very frequently updated
    cxxopts fixed
    • hard to build complex CLIs
    DocOpt fixed
    • great readability of CLI def.
    • untyped CLI definition
    • clumsy input querying
    getopt fixed
    • C Library
    • convoluted syntax
    getopt_long fixed
    • C Library
    • convoluted syntax
    GFlags fixed
    • convoluted syntax
    TCLAP flexible
    • convoluted syntax
    • hard to build complex CLIs
    The Lean Mean C++ Option Parser fixed
    • truly convoluted syntax
    • hard to build complex CLIs