What is the best way to create a date args

  • Hello

    I need to set a parameters to be a date :
       test -d "2013/05/16"

    What is the best way to do it ?

    Best regards,

    PS : I put format in "%Y/%m/%d" but this could also be changed.
        So I think to add a parameter -f "%Y/%m/%d"

  • Mike Smoot
    Mike Smoot

    Using a ValueArg<String> for either -d or -f should work fine.  You'll just need to cast the string to a date type. If you're looking for something like ValueArg<Date>, then that would be more work.  In that case you'd need to make sure Date supported operator<< and had a proper ArgTrait. I'd almost certainly recommend just using ValueArg<String>.

  • Daniel Aarno
    Daniel Aarno

    As usual "best" can be many things :) You basically have two options. For something simple you do what mes5k suggested, simply read in the value as a string and parse the string into some representation that you use in your program. The alternative is to use a Date type to represent your date, in which case the responsibility for parsing the date argument can be delegated to the Date class (or a helper function). For the second approach you can check out http://tclap.sourceforge.net/manual.html#USING_ARGTRAITS and the vector examples provided in test11 and test12.