python - 如何使用 vanila python 在命令行上获取参数?
问题描述
假设我在终端上运行它时有一个名为 this.py 的文件
this.py
我怎样才能让所有的论点都通过?如:
this.py -foo
我不想使用 argparse,只是 vanila python
可以说 this.py 是这样的:
#user/python_projects/this.py/
arguments = """ now here I need to find a way of a list of the arguments passed"""
def main(arguments):
if arguments[0] == 'foo':
print('bar')
else:
print('I like foo ):')
main(arguments)
如果我使用
this.py -foo
那么预期的结果是:'bar'
解决方案
如果您使用sys
,您应该能够使其工作:
$ cat this.py
import sys
arguments = sys.argv
def main(arguments):
if arguments[1] == 'foo':
print('bar')
else:
print('I like foo ):')
main(arguments)
$ python this.py foo
bar
请注意,argument[0]
当我认为您真正想要时使用您的示例argument[1]
推荐阅读
- mongodb - MongoDB拆分聚合结果
- scala - 对 Spark 数据框中的多列执行算术运算
- jsf - 想要从jsf下拉框中选择一个值,点击添加按钮后需要在底部下拉框中显示
- php - Laravel 无法访问所有 cookie
- c# - 在不丢失精度的情况下在 PostgreSQL 中存储 DateTime
- html - 表单的 post 方法的这种格式是什么?
- fullcalendar - 当开始或结束时间不同时,呈现重叠事件的完整日历事件顺序
- c# - AJAX 请求期间异常的自定义处理导致 HttpException
- elasticsearch - 如何在 Kibana (ElasticSearch) 中编写 OR 查询?
- django - Django OAuth Toolkit 的自定义(可交换)模型