Home > Enterprise >  Pass argument store in variable to argparse
Pass argument store in variable to argparse

Time:11-06

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.

CodePudding user response:

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.

CodePudding user response:

As jueon park said naming a variable in commandline wont work enter image description here

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

  • Related