I'm trying to use argparse to create an instance in which I type into my Unix console:
python getFood.py --food <(echo Bread) --calories yes
I have implemented the food option and want to add a calories yes or no option (binary input) using argparse which will determine whether to call the calories method from a class I imported.
My current code main routine is:
parser = argparse.ArgumentParser(description='Get food details.')
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
args = parser.parse_args()
This successfully allows me to use the first food option shown above which returns food details.
Basically I want to add a second binary option which if the user indicates true, will call an additional method. Any help for how I would edit my main routine argparse arguments? I am still very new to argparse.
You could simply add an argument with action='store_true', which will default args.calories to False if the --calories is not included. To further clarify, if the user adds --calories, args.calories will be set to True.
parser = argparse.ArgumentParser(description='Get food details.')
# adding the `--food` argument
parser.add_argument('--food', help='name of food to lookup', required=True, type=file)
# adding the `--calories` argument
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
# note: `dest` is where the result of the argument will go.
# as in, if `dest=foo`, then `--calories` would set `args.foo = True`.
# in this case, it's redundant, but it's worth mentioning.
args = parser.parse_args()
if args.calories:
# if the user specified `--calories`,
# call the `calories()` method
calories()
else:
do_whatever()
If, however, you would like to specifically check for yes or no, then replace the store_true in
parser.add_argument('--calories', action='store_true', dest='calories', help='...')
with store, as shown below
parser.add_argument('--calories', action='store', dest='calories', type='str', help='...')
This will then allow you to check later for
if args.calories == 'yes':
calories()
else:
do_whatever()
Do note that in this case I added type=str, which parses the argument as a string. Since you specified that the choices are either yes or no, argparse actually allows us to further specify the domain of possible inputs with choices:
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], help='...')
Now, if a user enters anything not in ['yes', 'no'], it will raise an error.
One last possibility is adding a default, such that users don't have to necessarily specify certain flags all the time:
parser.add_argument('--calories', action='store', dest='calories', type='str',
choices=['yes', 'no'], default='no', help='...')
Edit: As @ShadowRanger pointed out in the comments, in this case, dest='calories', action='store', and type='str' are the defaults, so you can omit them:
parser.add_argument('--calories', choices=['yes', 'no'], default='no', help='...')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With