Required arguments in argparse: at least one of a group

Rob Day at
Sat Mar 23 17:27:53 CET 2013

I don't know about argparse, but if you use docopt
( then this is easy to do with something like:

"""Usage: --file <myfile> --dir <mydir> --pattern <mypattern> --dir <mydir> --file <myfile> --pattern <mypattern> --dir <mydir>

On 23 March 2013 16:04, Marco <m.b at> wrote:
> Is there the possibility using the argparse module to group two or more
> arguments in order to have at least one of them required? For instance, I
> would like to have not an error only in the following cases:
>   python --file myfile --dir mydir
>   python --pattern mypattern --dir mydir
>   python --file myfile --pattern mypattern --dir mydir
> where --dir is required, and --file _or_ --parser have to be specified. In
> other words, I want the parser prints an error message just in this case:
>   python --dir mydir
> Thanks in advance, Marco
> --
> Marco
> --

Robert K. Day at

More information about the Python-list mailing list