Created
August 11, 2019 03:57
-
-
Save anirudhb/2a6361d2f06d87f7d4ceeade8f7dcf8a to your computer and use it in GitHub Desktop.
Parsing arbitrary arguments using argparse
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 sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--add_argument", help="Add argument for test subcommand", | |
dest="ext_args", action="append") | |
subparsers = parser.add_subparsers(help="sub-command help") | |
test_parser = subparsers.add_parser("test", help="Test subcommand") | |
(kargs, xargs) = parser.parse_known_args() | |
for arg in kargs.ext_args: | |
print(arg) | |
test_parser.add_argument("--" + arg, "-" + arg, action="store_true") | |
(nkargs, _) = test_parser.parse_known_args(xargs) | |
print("nkargs:") | |
print(nkargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment