argparse: some mutually exclusive arguments in required group

前端 未结 3 2369
难免孤独
难免孤独 2021-02-19 08:56

I have a set of arguments that can logically be separated in 2 groups:

  • Actions: A1, A2, A3, etc.
  • Informations:
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 09:20

    mutually_exclusive_group is a simple xor logic test. You could define 2 separate groups, but it does not provide any means of working across/between the groups.

    I have worked on a patch to allow more complex logic and nested groups. The testing logic isn't that bad, but designing a good user interface is tricky, as is creating a meaningful usage line. So that enhancement probably will never see production.

    Testing arguments after parsing is perfectly good. It only becomes tricky if you can't distinguish between attributes with default values and ones the usage gave you - so the default default None is best. argparse is primarily a parser, figuring out what the user wants. Whether they want something legit (beyond the simplest cases) is a different issue.

提交回复
热议问题