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?