Python argparse 需要一个或其他参数

发布于 2025-01-20 13:25:44 字数 644 浏览 3 评论 0 原文

我正在尝试在Argparse中设置以下条件:

  1. 如果 - StartDate 然后需要 - endDate
  2. 如果 - montry 然后需要 - 年
  3. 如果 - Week_num 然后需要 - 年

仅需要上述项目符号项目之一,即1或2或3如果指定了多个,则出错。因此,基本上用户可以指定开始日期和结束日期,或一个月和年度或每周和年。

例如:

$ myprog -startdate 01-01-2022 - enddate 01-07-2022

$ myprog- myprog- minth 1 - 年度2022

$ myprog - 周1年 - 2022年

如果用户这样做,则应该有一个错误:

$ myprog-周1- enddate 01-07-2022

我不确定如何使用ArgParse完成此操作。我研究了互斥的群体,但这似乎不允许配对。

I am trying to set the following conditions in argparse:

  1. If --startdate then require --enddate,
  2. If --month then require --year
  3. If --week_num then require --year

Only require one of the above bullet items, i.e, either 1 or 2 or 3, and if more than one is specified then error out. So basically the user can specify the start and end date, or the month and year or the week and year.

For example:

$ myprog --startdate 01-01-2022 --enddate 01-07-2022

OR

$ myprog --month 1 --year 2022

OR

$ myprog --week 1 --year 2022

If a user does something like this, then there should be an error:

$ myprog --week 1 --enddate 01-07-2022

I am not sure how to accomplish this using argparse. I looked into mutually exclusive groups, but that seems to not allow for pair.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文