3

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?

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
jiandingzhe
  • 1,881
  • 15
  • 35
  • 3
    A list of input files should normally be *non*-option arguments. – zwol Jul 24 '13 at 13:30
  • 1
    You'll need to explain more what you mean by "custom value types." To me, it seems [Boost already supports any value types you want](http://stackoverflow.com/q/8820109/33732). – Rob Kennedy Jul 24 '13 at 13:44
  • 1
    Your first requirement is not such a good idea, since there may be a file named `--some-other-parameter`. For this, the parsing is not unique - it is only unique if the parameter is only allowed at the end of the command line. – urzeit Jul 24 '13 at 13:48
  • 1
    But it is a valid requirement to have two variable length lists of arguments. Most programs will take a list of files as the only argument, but perhaps you had a program that performs one action with a one list of files, and another action with a second list. You need to differentiate one list form the other, and the only way to do so is as OP suggests. – Trenin Jul 24 '13 at 14:00
  • @urzeit I am aware of the possible strange file names. However in my scene of application it is not quite possible to have such strange file names, and if the program find something awful it will exclaim on the user. – jiandingzhe Jul 25 '13 at 01:48
  • @Zack But what if I have two list of files? A list of Foo data and a list of Bar data... – jiandingzhe Jul 25 '13 at 02:03
  • @jiandingzhe `command foo1 foo2 foo3 . bar1 bar2 bar3` or something like that. Note that the separator is not an option either. Options are for things that are *optional*. – zwol Jul 25 '13 at 02:28

1 Answers1

1

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188