I'm trying to pass a bash variable to be parsed by a python script, as follows:
#!/bin/bash
command="--var1 var1 --var2 'var 2'"
echo $command
python test.py $command
python script (test.py)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--var1", action="store", dest="var1")
parser.add_argument("--var2", action="store", dest="var2")
args = parser.parse_args()
print args.var1, args.var2
However, it fails due to there being whitespace, which I'm not sure how to escape error: unrecognized arguments: 2'. Running python test.py --var1 var1 --var2 'var 2' from the command line works fine though. Is there any way around this?
actionandstorein this simple case, the variables will be created by the parser implicitly (as names of your options), so this should be enough:parser.add_argument("--var1")andparser.add_argument("--var2")