TypeError: __init__() got an unexpected keyword argument 'choices' in python's argparser

Question:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url',help='Passing one url')
parser.add_argument('-t', '--type', action='store_true',help='To download pages/posts', choices=['pages', 'posts'])
args = parser.parse_args()

url = args.url

if args.type == "pages":
    url_link = url + "/wp-json/wp/v2/pages/?per_page=100"
if args.type == "posts":
    url_link = url + "/wp-json/wp/v2/posts/?per_page=100"

If the user chooses "pages" then the pages url_link should be used. If they choose "posts", it should pass "posts" url_link. If they don’t choose anything, an error handling of "Please choose your type" in –type error.

I tried doing this but it says choices in unknown. Weird… Please help.

Asked By: Manal Shaikh

||

Answers:

It doesn’t make sense to specify choices when the action is 'store_true'.

Perhaps you meant:

parser.add_argument('-t', '--type', help='To download pages/posts',
                    choices=['pages', 'posts'])
Answered By: khelwood
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.