简体   繁体   中英

Pass argument store in variable to argparse

This is my script mytest.py.

import argparse
parser = argparse.ArgumentParser(description="Params")
parser.add_argument(
        "--value")

def test(args):
  print(args.value)

args = parser.parse_args()
test(args)

I want to pass argument store in variable val

val =1
!python mytest.py --value val

instead of printing 1 it print val . How to send 1 stored in variable val.

argparse always get argument as string, or list of strings on default, and what you do on your shell is irrelevant with python program. It is no wonder val is printed.

Use file that contains "1" and read that file to do what you intended to.

As jueon park said naming a variable in commandline wont work在此处输入图片说明

It would create an error like the above one.If you are calling the command from any programer will work but in cmd it won't work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM