Menu

#34 Design around OptionalUnlabeledTracker is incorrect

1.2.x
open
nobody
None
5
2022-02-11
2021-08-15
KOLANICH
No
  1. It is a singleton, so there are issues when creating multiple CmdLine objects within the same app.
  2. It is used in the constructor of the positional args rather then on adding thm to a CmdLine. It means the order in which the args objecs were created matters. But what really should matter is the order in which they are specified to be parsed.

Discussion

  • Daniel Aarno

    Daniel Aarno - 2021-08-16

    See also https://sourceforge.net/p/tclap/bugs/25/ for related issues with static variables. Fixing this in a backwards compatible way may be a bit tricky (because the Arg doesn't necessarily have access to the CmdLine object when it is created), and given that it's a pretty rare use-case (multiple CmdLine objects + Unlabeled arguments) I'm not convinced it's worth it. It should be fixed for 2.0 though. However, non-breaking patches would be welcomed.

     

    Last edit: Daniel Aarno 2021-08-16
  • David Okamoto

    David Okamoto - 2022-02-11

    I think this may work in 1.4:

    In CmdLine.h, add a member variable (bool _optionalUnlabeled;), initialized to false in the constructor, then check in this method:

    inline void CmdLine::addToArgList(Arg *a) {
        for (ArgListIterator it = _argList.begin(); it != _argList.end(); it++)
            if (*a == *(*it))
                throw(SpecificationException(
                    "Argument with same flag/name already exists!", a->longID()));
    
        if (!a->hasLabel()) {
            if (_optionalUnlabeled)
                throw(SpecificationException(
                    "You can't specify ANY Unlabeled Arg following an optional "
                    "Unlabeled Arg", a->longID()));
    
            if (!a->isRequired()) _optionalUnlabeled = true;
        }
    
        a->addToList(_argList);
    
        if (a->isRequired()) _numRequired++;
    }
    

    and remove the check in the unlabeled objects.

     

Log in to post a comment.

MongoDB Logo MongoDB