XiaoHu
XiaoHu

Reputation: 57

python2error: unrecognized arguments:

def build(string, path, name, logo=""):
       qr = qrcode.QRCode(
           version = 2,
           error_correction = qrcode.constants.ERROR_CORRECT_H,
           box_size = 10,
           border = 1
       )
       qr.add_data(string)
       qr.make( fit = True)
       img = qr.make_image()
       img = img.convert("RGBA")
       icon = Image.open('img/'+logo+'.png')
       img_w, img_h = img.size
       factor = 4
       size_w = int(img_w / factor)
       size_h = int(img_h / factor)
       icon_w, icon_h = icon.size
       if icon_w > size_w: icon_w = size_w
       if icon_h > size_h: icon_h = size_h
       icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
       w = int((img_w - icon_w) / 2)
       h = int((img_h - icon_h) / 2)
       icon = icon.convert("RGBA")
       newimg = Image.new("RGBA", (icon_w + 8, icon_h + 8), (255, 255, 255))
       img.paste(newimg, (w-4, h-4), newimg)
       img.paste(icon, (w, h), icon)
       img.save(path + name + '.png', quality = 100)

if __name__ == "__main__":
       argparser = argparse.ArgumentParser()
       args = argparser.parse_args()
       argparser.add_argument('Words')
       argparser.add_argument('-d', '--directory', default = os.getcwd())
       argparser.add_argument('-n', '--name')
       argparser.add_argument('-l', '--logo')
       build(args.Words, args.directory, args.name, args.logo)

When i execute it

python code.py http://www.xx.com -n client -l client -d ./

it shows that

usage: code.py [-h]

code.py: error: unrecognized arguments: http://www.xx.com -n client -l client -d ./

How can i make it work properly? please help me!

Upvotes: 0

Views: 968

Answers (1)

jacg
jacg

Reputation: 2120

The line

args = argparser.parse_args()

needs to be moved after the configuration of the parser, in other words, after the lines

argparser.add_argument('Words')
argparser.add_argument('-d', '--directory', default = os.getcwd())
argparser.add_argument('-n', '--name')
argparser.add_argument('-l', '--logo')

You were trying to use the parser before you told the parser about the positional argument, -d, -n and -l. So, obviously, it didn't recognize them when asked to parse them.

Upvotes: 1

Related Questions