Menu

UnlabeledValueArgs not starting with a '-'

2019-05-31
2019-06-04
  • Sanford Freedman

    The UnlabeledValueArg arg type currently matches against arguments starting with a '-'. Could either a new class or an optional parameter be added to indicate an argument should not be allowed to start with an '-'? For instance,

    namespace TCLAP{
    template<typename T>
        class UnlabeledStringValueArg : public UnlabeledValueArg<T>{
            public:
                using UnlabeledValueArg<T>::UnlabeledValueArg;
    
                virtual bool processArg(int* i, std::vector<std::string>& args){
                    if(args[*i][0] == '-') return(false);
                    return(UnlabeledValueArg<T>::processArg(i, args));
                }
        };
    }
    
     

    Last edit: Sanford Freedman 2019-05-31
  • Daniel Aarno

    Daniel Aarno - 2019-06-04

    Can you describe a use case where this would be useful? Perhaps there is a different way.

     
  • Sanford Freedman

    A simple example would be any application expecting a filename. If a user accedentically had a '-' at the start of a filename argument, then they were probably attempting to use a flag instead. While an application can easily catch this, having the command line parser directly catch it is cleaner and results in a more sensible error/usage message. For instance, the error mesage when doing a 'ls -dummy' is:

    ls: option requires an argument -- 'y'
    Try 'ls --help' for more information.
    

    instead of something similar to (the following is not real):

    ls: cannot access '-dummy': No such file or directory
    

    While the option to allow a leading '-' must be available (for instance to support negative numbers), there are many situations where a leading '-' is not appropriate.

     

Log in to post a comment.