Created
July 27, 2023 21:32
-
-
Save hoehermann/08d56f62a0b04e88697feba2f362884b to your computer and use it in GitHub Desktop.
partial and conditional argument parsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--format', required=True, choices=['binary', 'xml', 'text']) | |
args, rem_args = parser.parse_known_args() | |
if (args.format == "text"): | |
parser.add_argument('--encoding', required=True) | |
parser.parse_args(rem_args, namespace=args) | |
# this does not work since required argument has already been consumed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment