Mailing List Archive

Mutually exclusive options with argparse.
See the following code snippets [1] for implementation of the exclusive options with argparse:

def query_builder(args):
if args.r and args.s:
sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' + Term.ENDC)
sum_status = sum(1 for x in [args.failure, args.code != -1] if x)
if sum_status > 1:
sys.exit(Term.FAIL + ('Only one of --failure and --code has to be set') + Term.ENDC)

[1] https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391

It seems that the above method is awkward, but I'm out of idea to work out more graceful solutions. Any comment/suggestion/enhancement will be highly appreciated.

Regards,
HY
--
https://mail.python.org/mailman/listinfo/python-list
Re: Mutually exclusive options with argparse. [ In reply to ]
Does a mutually exclusive group, as described in "Mutual exclusion" at
https://docs.python.org/3/library/argparse.html meet your needs?

On Wed, Sep 1, 2021 at 9:48 AM hongy...@gmail.com <hongyi.zhao@gmail.com>
wrote:

> See the following code snippets [1] for implementation of the exclusive
> options with argparse:
>
> def query_builder(args):
> if args.r and args.s:
> sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' +
> Term.ENDC)
> sum_status = sum(1 for x in [args.failure, args.code != -1] if x)
> if sum_status > 1:
> sys.exit(Term.FAIL + ('Only one of --failure and --code has to be
> set') + Term.ENDC)
>
> [1]
> https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391
>
> It seems that the above method is awkward, but I'm out of idea to work out
> more graceful solutions. Any comment/suggestion/enhancement will be highly
> appreciated.
>
> Regards,
> HY
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
+1-202-507-9867, Twitter @lcongdon
--
https://mail.python.org/mailman/listinfo/python-list
Re: Mutually exclusive options with argparse. [ In reply to ]
On Wednesday, September 1, 2021 at 11:20:21 PM UTC+8, Lee Congdon wrote:
> Does a mutually exclusive group, as described in "Mutual exclusion" at
> https://docs.python.org/3/library/argparse.html meet your needs?

Thanks for letting me know about this feature. I'll give it a try.

>
> On Wed, Sep 1, 2021 at 9:48 AM hongy...@gmail.com <hongy...@gmail.com>
> wrote:
> > See the following code snippets [1] for implementation of the exclusive
> > options with argparse:
> >
> > def query_builder(args):
> > if args.r and args.s:
> > sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' +
> > Term.ENDC)
> > sum_status = sum(1 for x in [args.failure, args.code != -1] if x)
> > if sum_status > 1:
> > sys.exit(Term.FAIL + ('Only one of --failure and --code has to be
> > set') + Term.ENDC)
> >
> > [1]
> > https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391
> >
> > It seems that the above method is awkward, but I'm out of idea to work out
> > more graceful solutions. Any comment/suggestion/enhancement will be highly
> > appreciated.
> >
> > Regards,
> > HY
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> +1-202-507-9867, Twitter @lcongdon
--
https://mail.python.org/mailman/listinfo/python-list
Re: Mutually exclusive options with argparse. [ In reply to ]
On Thursday, September 2, 2021 at 10:16:09 AM UTC+8, hongy...@gmail.com wrote:
> On Wednesday, September 1, 2021 at 11:20:21 PM UTC+8, Lee Congdon wrote:
> > Does a mutually exclusive group, as described in "Mutual exclusion" at
> > https://docs.python.org/3/library/argparse.html meet your needs?
> Thanks for letting me know about this feature. I'll give it a try.

The exact document URL is located at here [1].

[1] https://docs.python.org/3/library/argparse.html#mutual-exclusion

> >
> > On Wed, Sep 1, 2021 at 9:48 AM hongy...@gmail.com <hongy...@gmail.com>
> > wrote:
> > > See the following code snippets [1] for implementation of the exclusive
> > > options with argparse:
> > >
> > > def query_builder(args):
> > > if args.r and args.s:
> > > sys.exit(Term.FAIL + 'Only one of -re and -sql should be set' +
> > > Term.ENDC)
> > > sum_status = sum(1 for x in [args.failure, args.code != -1] if x)
> > > if sum_status > 1:
> > > sys.exit(Term.FAIL + ('Only one of --failure and --code has to be
> > > set') + Term.ENDC)
> > >
> > > [1]
> > > https://github.com/hongyi-zhao/recent2/blob/5486afbd56a6b06bb149a3ea969fb33d9d8b288f/recent2.py#L391
> > >
> > > It seems that the above method is awkward, but I'm out of idea to work out
> > > more graceful solutions. Any comment/suggestion/enhancement will be highly
> > > appreciated.
> > >
> > > Regards,
> > > HY
> > > --
> > > https://mail.python.org/mailman/listinfo/python-list
> > >
> >
> >
> > --
> > +1-202-507-9867, Twitter @lcongdon
--
https://mail.python.org/mailman/listinfo/python-list