#8 cmd_line_parser options


I just created a simple application:
#include "dlib/cmd_line_parser.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
// check command line options
dlib::cmd_line_parser::print_1a cmd_printer;
cmd_printer.add_option( _T("help"), _T("shows this
message"), 1 );
cmd_printer.parse(argc, argv);

cout << cmd_printer.get_number_of_arguments();
// setup config
// setup log
// setup libraries
// start server

return 0;


But now when I run it with "test --help=1" it crashes (as I
don't handle the thrown error).

The problem is that it expects the command line thingies
to be parameters. So it expects "--help 1" instead of "--


  • Davis

    Davis - 2005-11-09

    Logged In: YES

    This is the intended behavior. In your example it considers
    help=1 to be the name of an option, but that option doesn't
    exist so you get an error.

    It would be easy to allow that sort of syntax but it does
    restrict what you can pass as an option a bit. Since to
    make it consistent you would want to also allow someone to
    say help =1 or help = 1 (note the spaces). But what
    if you want to have an argument that can take an arbitrary
    string? Allowing this extended syntax prevents you from
    passing strings that start with a = character. Or I could
    change it to require that long named options with arguments
    always require an = character to follow them.

    I think the syntax that doesn't use the = character is more
    common but I am not sure. I will have to think about it. :)

  • Steven Van Ingelgem

    Logged In: YES

    Oh well, it's just something you have to know. Nothing more. If
    users want to type in help==1, and then the program says
    like... Hein!? I don't know what you're talking about... Well,
    then they will see the help & do what they have to do.

    It's just something you (as a coder) and your customers have
    to know.

  • Davis

    Davis - 2005-11-13
    • status: open --> closed
  • Davis

    Davis - 2005-11-13

    Logged In: YES

    After looking at some other command line tools and thinking
    about it a bit more I decided to add this. So this will
    appear in the next release.



Log in to post a comment.