Using python's argparse library, I would like to process the first few command-line arguments and use them to generate a list of choices for the other command-line arguments.
How can I process the first few arguments without argparse complaining about extra arguments that it doesn't expect (which I plan to add later)?
For example, I have a script that gets the username and password from the command-line, uses those to access available properties of an API, and then uses that list to restrict the values of a third argument:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('username', help='Your username.')
parser.add_argument('password', help='Your password.')
args = parser.parse_args() # Error here because a third argument exists on the command-line
response = requests.get(
url='https://api.example.com/properties',
auth=(args.username, args.password)
)
parser.add_argument(
'property',
choices=response.json()['properties'], # Validates the input
help='The property you want to access.'
)
args = parser.parse_args()
I know I can just add all the arguments at once, and then validate the third argument myself manually, but I'm wondering if there's a way to natively do what I'm asking in the argparse library?
argparseis as the name sugests a parser. It has functionalities to verify the data to parse, but this verification has is limits. Doing HTTP lookups is definitely outside of this limits. You have to do it in code. Also consider that your approach would not allow to display help without giving the credentials as arguments, but you don't know how until you see the help.parse_known_args()function (which I wasn't aware of), which I can use to parse chunks of arguments at a time. @KlausD. I see your point about incomplete help documentation. I'll stick to adding all the arguments at once and verifying it myself. Thank you both.parse_known_argsis the first thing that came to mind as I started to read your question. The relevant docs section: docs.python.org/3/library/argparse.html#partial-parsing. Theintermixedsubclass that I suggested in the linked SO is probably overkill. I haven't gotten much feedback on that since it was proposed.