【发布时间】:2015-12-10 07:11:25
【问题描述】:
我正在编写一个生成序列波形的函数,并希望让用户指定序列中每个正弦波的频率和持续时间。
如何定义一个位置参数解析器,它可以采用以下形式的参数,这说明了每个开关的传递顺序?:
waveSequence.py --sine 440 1 --silence 0.5 --sine 110 2
(这将产生 1 秒的 440Hz 正弦波,然后是 0.5 秒的静音,然后是 2 秒的 110Hz 正弦波。)
这样的开关还能回收吗?
解决它的一种方法可能是按位置读取参数。在 bash 中,我可以这样做:
if [ $1 -eq 'sine' ]; then
freq=$2
time=$3
shift
shift
shift
# Next, continue reading from 4th argument
elif [ $1 -eq 'silence' ]; then
# no freq, only time
time=$2
shift
shift
else
# other waveforms
fi
如何在 Python 中切换位置参数?
【问题讨论】:
-
也许命令行界面不是这里最好的方法?
标签: python-2.7 arguments command-line-arguments sequence