I have several different input files, saved with case numbers “.1”, “.2”, etc to differentiate them. I am building a script that operates on these files, and I would like to use argparse to allow the user to specify the particular case number to operate on, or use “_” to specify the last saved case (i.e. the input file with the largest case number). Something like;
> ls
file.1, file.2, file.3
> my_script.py 2
(operates on file.2)
> my_script.py _
(operate on file.3)
Is there a way I can specify “any integer” as one choice and “_” as the second choice? Something like;
parser = argparse.ArgumentParser()
parser.add_argument('case', choices=[anyint, '_'])
You could use the
typeargument toadd_argument(...)instead. For example:When I run this I get:
Alternately, you could build the choices list in code: