Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need a command-line parser to fit my requirements

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?

like image 449
jiandingzhe Avatar asked Jan 01 '26 04:01

jiandingzhe


1 Answers

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.

like image 86
Mark B Avatar answered Jan 06 '26 00:01

Mark B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!