Pergunta

It's a question about C/C++ command line parser.

I used the command line parsers provided in glib and Boost, but I found them not satisfying. I have two special requirements:

  1. multiple values following one key, so that I can use file glob on command line like this:

    my_program --input dir/*.txt
    
  2. customized value type, like this:

    typedef enum { FORMAT_A, FORMAT_B, FORMAT_C } InputFormat;
    InputFormat option_format;
    

I want my user can specify the format in command line --format format_a. The parser need to allow customized callback function to recognize the string value and set the enum value.

It seems boost supports 1 but not 2. It only allow you define an additional parser that traverse the tokens one by one, but not by KV pairs. And glib supports 2 but not 1. It only allows multiple calling like --input foo --input bar.

Any more libs that support both 1 and 2? Or any suggestions on advanced use of glib or boost to achieve both 1 and 2?

Foi útil?

Solução

Boost can in fact handle requirement 2. You'll need to create your own child of value_semantic with an appropriate parser and pass an instance of that into add_options rather than using the typical value<int>() mechanism.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top