Search code examples
pythonargparseargs

How to check if a particular argument is supported in parser.parse_args()?


For example, How can I know whether there is an argument like args.manual_selection in args (args = parser.parse_args())


Solution

  • One way to do it would be just to convert args to a dictionary

    args = vars(parser.parse_args())

    Then you can check for the existence of the key, e.g.

    args.get("manual_selection", False)

    If that returns False, the key does not exist. Of course the default returned for non-existence must not overlap with valid user input.