title: How to use boost program_options to read an integer array?
body: <p>I am using Ubuntu and boost v1.50.</p>
  
  <p>Previously I used boost program_options to feed a set of options into a program like so:</p>
  
  <pre><code>#!/bin/bash
  
  ./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5
  </code></pre>
  
  <p>So I am dealing with a mix of single integers, strings and integer arrays.
  This worked fine.</p>
  
  <p>However, after "improving" the code by introducing local variables in bash, I have:</p>
  
  <pre><code>#!/bin/bash
  a1=1
  a2="2"
  a3={1,2,3}
  a4={1,2}
  a5=5
  
  ./prog --arg1 $a1 --arg2 $a2 --arg3 $a3 --arg4 $a4 --arg5 $a5
  </code></pre>
  
  <p>Executing this results in an error:</p>
  
  <pre><code>error: the argument ('{1,2,3}') for option '--arg3' is invalid
  </code></pre>
  
  <p>I have set up the boost program_options like this:</p>
  
  <pre><code>namespace po = boost::program_options;
  using namespace std;
  try{
      po::options_description desc("Allowed options");
      desc.add_options()
          ("help", "produce help message")
          ("arg1", po::value&lt;int&gt;(&amp;arg1)-&gt;required(), "doc1")
          ("arg2", po::value&lt;string&gt;(&amp;arg2)-&gt;default_value("test"), "doc2")
          ("arg3", po::value&lt;vector&lt;int&gt; &gt;(&amp;arg3)-&gt;multitoken(), "doc3")
          ("arg4", po::value&lt;vector&lt;int&gt; &gt;(&amp;arg4)-&gt;multitoken(), "doc4")
          ("arg5", po::value&lt;int&gt;(&amp;arg5)-&gt;default_value(1), "doc5")
          ;
  
      po::variables_map vm;        
      po::store(po::parse_command_line(ac, av, desc), vm);
      po::notify(vm);    
  
      if(vm.count("help")) cout &lt;&lt; desc &lt;&lt; "\n";
  }
  catch(exception&amp; e){
      cerr &lt;&lt; "error: " &lt;&lt; e.what() &lt;&lt; "\n";
      errorflag=1;
  }
  catch(...){
      cerr &lt;&lt; "Exception of unknown type!\n";
      errorflag=1;
  }
  </code></pre>
  
  <p>Where did I go wrong? Is multitoken not appropriate in this context? What can I use instead? Is it not possible to read integer arrays? </p>
  
  <p>I tried omitting multitoken but then it fails also.
  Using quotation marks around the local variable in the bash script does not help either.</p>
  
  <p>If I change the array input from {a,b,c} to "a b c", it is ok. However, I already have a large number of entries in the other format and I am would like to continue using it as other programs depend on it too.</p>
  
  <p>I think it should be doable, since it worked without the local variables. Does someone know how?</p>
  
  <p>EDIT: I have been mistaken. "a b c" does NOT work as input :(</p>
  
  <p>EDIT 2: I came up with a little workaround:
  I convert {a,b,c} -> a b c using </p>
  
  <pre><code>argnew=`echo ${arg:1:-1} | tr ',' ' '`
  </code></pre>
  
  <p>and the feeding it to the program works fine. Is that the best solution?</p>