Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41742 > unrolled thread

Required arguments in argparse: at least one of a group

Started byMarco <m.b@gmail.com>
First post2013-03-23 17:04 +0100
Last post2013-03-24 14:41 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Required arguments in argparse: at least one of a group Marco <m.b@gmail.com> - 2013-03-23 17:04 +0100
    Re: Required arguments in argparse: at least one of a group Rob Day <robert.day@merton.oxon.org> - 2013-03-23 16:27 +0000
      Re: Required arguments in argparse: at least one of a group Marco <m.b@gmail.com> - 2013-03-24 21:02 +0100
    Re: Required arguments in argparse: at least one of a group rurpy@yahoo.com - 2013-03-24 14:41 -0700

#41742 — Required arguments in argparse: at least one of a group

FromMarco <m.b@gmail.com>
Date2013-03-23 17:04 +0100
SubjectRequired arguments in argparse: at least one of a group
Message-ID<kikjpg$ch5$1@speranza.aioe.org>
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 finder.py --file myfile --dir mydir
   python finder.py --pattern mypattern --dir mydir
   python finder.py --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 finder.py --dir mydir

Thanks in advance, Marco
-- 
Marco

[toc] | [next] | [standalone]


#41746

FromRob Day <robert.day@merton.oxon.org>
Date2013-03-23 16:27 +0000
Message-ID<mailman.3648.1364056081.2939.python-list@python.org>
In reply to#41742
I don't know about argparse, but if you use docopt
(http://docopt.org/) then this is easy to do with something like:

"""Usage:

finder.py --file <myfile> --dir <mydir>
finder.py --pattern <mypattern> --dir <mydir>
finder.py --file <myfile> --pattern <mypattern> --dir <mydir>
"""

On 23 March 2013 16:04, Marco <m.b@gmail.com> 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 finder.py --file myfile --dir mydir
>   python finder.py --pattern mypattern --dir mydir
>   python finder.py --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 finder.py --dir mydir
>
> Thanks in advance, Marco
> --
> Marco
> --
> http://mail.python.org/mailman/listinfo/python-list



-- 
Robert K. Day
robert.day@merton.oxon.org

[toc] | [prev] | [next] | [standalone]


#41801

FromMarco <m.b@gmail.com>
Date2013-03-24 21:02 +0100
Message-ID<kinm40$9gn$1@speranza.aioe.org>
In reply to#41746
On 03/23/2013 05:27 PM, Rob Day wrote:

> I don't know about argparse, but if you use docopt
> (http://docopt.org/) then this is easy to do with something like:
>
> """Usage:
>
> finder.py --file <myfile> --dir <mydir>
> finder.py --pattern <mypattern> --dir <mydir>
> finder.py --file <myfile> --pattern <mypattern> --dir <mydir>
> """

Thanks Rob, but I was looking for a solution in the stdlib

-- 
Marco

[toc] | [prev] | [next] | [standalone]


#41802

Fromrurpy@yahoo.com
Date2013-03-24 14:41 -0700
Message-ID<8ef40685-bcd1-4f5f-900c-2cb8d31c0950@googlegroups.com>
In reply to#41742
On 03/23/2013 10:04 AM, Marco 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 finder.py --file myfile --dir mydir
>    python finder.py --pattern mypattern --dir mydir
>    python finder.py --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 finder.py --dir mydir

How about something like:

  p = argparse.ArgumentParser (description="...")
  p.add_argument ('--dir', help="A dir (required).", required=True)
  p.add_argument ('--pattern', help="A pattern.  If not given, --file is required.")
  p.add_argument ('--file', help="A filename.  If not given, --pattern is required.")
  args = p.parse_args()
  if not args.file and not args.pattern:
    p.error ('Either --pattern or --file is required.')

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web