4

I currently use some old C library for getting program options and would like to replace that with some proper C++ (mainly to become independent of that library, which is a real burden). I was looking into using boost.program_options, but am not sure it can support all I want. Some things I want is:

  1. allow the following command-line syntax: myprogram option=value (in particular, I don't really want the --option value syntax)

  2. use a default value if no value is provided (obviously this can be done in my program, but support in the options library would be nice)

  3. allow default options (which are always present even if I don't give them) and an automatic help output consisting of all the options and their descriptions

  4. allow mathematical parsing, i.e. (command line) myprogram option1=Pi option2=3/5 option3=sqrt(2) to give 3.1415..., 0.6, and 1.415... in my program

  5. allow single values to be expanded. Let option_3Dpoint correspond to an std::array<double,3>, I want both myprogram option_3Dpoint=0,0,0 and myprogram option_3Dpoint=0 (expanding to 0,0,0) to work

Which of these can be supported by boost.program_options? Are there any alternatives?

Xeo
  • 129,499
  • 52
  • 291
  • 397
Walter
  • 44,150
  • 20
  • 113
  • 196
  • all is supported, but you have to parse it (if you do not want to use `--option value` syntax) – BЈовић Sep 25 '12 at 10:56
  • 4
    I think your major problem will be with `myprogram option=value`. You can have `--option=value` or `-o=value`, but for `option=value` you will need positional parameters and parse the `"option=value"` string yourself I fear. 2 and 3 are provided, 4 would need manual parsing of the option argument (you can pass a custom parser), and I think 5 could work through special-casing. – Xeo Sep 25 '12 at 10:56
  • @Xeo is there another library (boost or otherwise) to do the parsing (for point 4)? – Walter Sep 25 '12 at 11:03
  • @Walter: Sorry, that I don't know. However, there should be plenty of calculator code flying around that provides this and all you'd need to do would be to split the string after the `=`. – Xeo Sep 25 '12 at 11:06

1 Answers1

3

boost.program_options is very good library. You can use to parse config files aswell. Answers:

  1. Dont know but seems no builtin support.
  2. Yes.
  3. Yes.
  4. No unless you make your own expression evaluation handler or use some other boost libs to do this.
  5. Yes, you will need to write your own handler which creates 3DPoint object from string like 0,0,0
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • *No unless you make your own expression evaluation handler or use some other boost libs to do this.* which other boost libs? – Walter Sep 26 '12 at 12:15
  • Dunno, maybe even spirit;) Someone using it for this btw [link](http://agentzlerich.blogspot.com/2011/06/using-boost-spirit-21-to-evaluate.html) – Galimov Albert Sep 26 '12 at 13:42
  • this looks interesting. where can I get their library? – Walter Sep 26 '12 at 22:57