Skip to content

Instantly share code, notes, and snippets.

@hoehermann
Created July 27, 2023 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehermann/08d56f62a0b04e88697feba2f362884b to your computer and use it in GitHub Desktop.
Save hoehermann/08d56f62a0b04e88697feba2f362884b to your computer and use it in GitHub Desktop.
partial and conditional argument parsing
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