使用 argparse 将多个 bash 数组传递给 Python
Pass multiple bash arrays to Python using argparse
我有一个 Python 脚本,我想从 bash 运行,它需要多个字符串 arrays/lists 输入。 在另一个问题中的回答解释了如何使用一个数组进行操作,并且效果很好。但是我怎样才能传递多个数组呢?到目前为止,这是我尝试过的方法:
Bash 脚本:
#!/bin/bash
declare -a first=("one" "two" "three")
declare -a second=("four" "five")
declare -a third=("six")
declare -a fourth=("seven" "eight")
python argsprob.py "${first[@]}" "${second[@]}" "${third[@]}" "${fourth[@]}"
Python 脚本:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('first', nargs='+')
parser.add_argument('second', nargs='+')
parser.add_argument('third', nargs='+')
parser.add_argument('fourth', nargs='+')
args = parser.parse_args()
print(args.first)
print(args.second)
print(args.third)
print(args.fourth)
输出
$ bash argsprob.sh
['one', 'two', 'three', 'four', 'five']
['six']
['seven']
['eight']
期望输出
['one','two','three']
['four','five']
['six']
['seven','eight']
你可能会说我不知道自己在做什么。我尝试了其他使用 argparse 的方法(更多参数、不同的 "nargs" 等),但其中 none 有效。感谢您的帮助!
与:
import argparse
import sys
print(sys.argv[1:])
parser = argparse.ArgumentParser()
parser.add_argument('--first', nargs='+')
parser.add_argument('--second', nargs='+')
parser.add_argument('--third', nargs='+')
parser.add_argument('--fourth', nargs='+')
args = parser.parse_args()
print(args)
来电:
1948:~/mypy$ python stack49282913.py --first "${first[@]}" --second "${second[@]}" --th "${third[@]}" --fo "${fourth[@]}"
['--first', 'one', 'two', 'three', '--second', 'four', 'five', '--th', 'six', '--fo', 'seven', 'eight']
Namespace(first=['one', 'two', 'three'], fourth=['seven', 'eight'], second=['four', 'five'], third=['six'])
我有一个 Python 脚本,我想从 bash 运行,它需要多个字符串 arrays/lists 输入。
Bash 脚本:
#!/bin/bash
declare -a first=("one" "two" "three")
declare -a second=("four" "five")
declare -a third=("six")
declare -a fourth=("seven" "eight")
python argsprob.py "${first[@]}" "${second[@]}" "${third[@]}" "${fourth[@]}"
Python 脚本:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('first', nargs='+')
parser.add_argument('second', nargs='+')
parser.add_argument('third', nargs='+')
parser.add_argument('fourth', nargs='+')
args = parser.parse_args()
print(args.first)
print(args.second)
print(args.third)
print(args.fourth)
输出
$ bash argsprob.sh
['one', 'two', 'three', 'four', 'five']
['six']
['seven']
['eight']
期望输出
['one','two','three']
['four','five']
['six']
['seven','eight']
你可能会说我不知道自己在做什么。我尝试了其他使用 argparse 的方法(更多参数、不同的 "nargs" 等),但其中 none 有效。感谢您的帮助!
与:
import argparse
import sys
print(sys.argv[1:])
parser = argparse.ArgumentParser()
parser.add_argument('--first', nargs='+')
parser.add_argument('--second', nargs='+')
parser.add_argument('--third', nargs='+')
parser.add_argument('--fourth', nargs='+')
args = parser.parse_args()
print(args)
来电:
1948:~/mypy$ python stack49282913.py --first "${first[@]}" --second "${second[@]}" --th "${third[@]}" --fo "${fourth[@]}"
['--first', 'one', 'two', 'three', '--second', 'four', 'five', '--th', 'six', '--fo', 'seven', 'eight']
Namespace(first=['one', 'two', 'three'], fourth=['seven', 'eight'], second=['four', 'five'], third=['six'])