Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Command Line Arguments Command Line Arguments Cmd.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++ How To Access

    main.cpp
    #include <iostream>
    int main (int const argc, char const*const* 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

    #include <iostream>
    int main (int const argc, char const*const* argv) {
      if (argc < 3) {
        std::cerr << "Usage:\\n " << argv[0]    << " <word> <times>\\n";
        return EXIT_FAILURE;
      }
      auto word  = std::string(argv[1]);
      int  times = atoi(argv[2]);
      for (int i = 0; i < times; ++i) {
        std::cout << word << ' ';
      }
      std::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 defs are 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 are 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
    • easy to write simple CLIs
    • 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