简体   繁体   English

解析python命令行参数

[英]Parsing python command line arguments

Complete newbie question. 完成新手问题。

I am developing a class for searching a movie database using an API. 我正在开发用于使用API​​搜索电影数据库的类。

Here is how you make a query using the movie title and year from within the Python interpreter: 这是在Python解释器中使用电影标题和年份进行查询的方法:

import moviesearch

movie_db = moviesearch.MovieSearch()
result = movie_db.search('Clockwork Orange', year='1971')
print result

Doing this particular query yields one result from the service I am using. 进行此特定查询会从我正在使用的服务中产生一个结果。

To save typing for testing, I have created the following script m.py: 为了保存用于测试的输入,我创建了以下脚本m.py:

from sys import argv
import moviesearch

script, movie_name, params = argv

movie_db = moviesearch.MovieSearch()
result = movie_db.search(movie_name, params)
print result

When I execute this script like so: 当我像这样执行此脚本时:

python m.py 'Clockwork Orange', year='1971'

the service yields two results. 该服务产生两个结果。

So this means that there is something wrong with the way I am formatting my parameters when I am testing the class with the script. 因此,这意味着在使用脚本测试类时,格式化参数的方式存在问题。 Can someone please tell me what I am doing wrong? 有人可以告诉我我做错了吗?

I am not including the class for querying the movie database because I don't think that's necessary for figuring out what's wrong. 我不包括用于查询电影数据库的类,因为我认为找出问题所在不是必需的。 What I need to know is how to properly format my parameters when using the script so that it executes exactly as it does in the example above where I am using the class directly from the Python interpreter. 我需要知道的是如何在使用脚本时正确格式化参数,以使其执行完全与上面示例中的操作完全一样,在上面的示例中,我直接从Python解释器中使用类。

Thanks in advance for any help. 在此先感谢您的帮助。

When you do this in the shell (I'm assuming a *nix platform with an sh-compatible shell; Windows is probably slightly different): 在外壳中执行此操作时(我假设一个* nix平台具有与sh兼容的外壳; Windows可能略有不同):

python m.py 'Clockwork Orange', year='1971'

The sys.argv list will be: sys.argv列表将是:

['m.py', 'Clockwork Orange,', 'year=1971']

So, when you do this: 因此,当您这样做时:

script, movie_name, params = argv

You end up with movie_name set to the string 'Clockwork Orange,' (with an extra comma), and params set to the string 'year=1971' . 您最终将movie_name设置为字符串'Clockwork Orange,' (带有一个逗号),并将params设置为字符串'year=1971' So, your call is ultimately: 因此,您的电话最终是:

result = movie_db.search('Clockwork Orange,', 'year=1971')

This is not the same as: 这与以下内容不同:

result = movie_db.search('Clockwork Orange', year='1971')

So, how do you get what you want? 那么,您如何得到想要的?

Well, you can parse the params, with code something like this: 好了,您可以使用以下代码解析参数:

params = dict(pair.split('=') for pair in argv[2:])
result = movie_db.search(movie_name, **params)

The first line will give you a dict with {'year': '1971'} . 第一行将为您提供{'year': '1971'}dict Then you can use the ** syntax to forward that dict as the keyword arguments to the function. 然后,您可以使用**语法将该dict作为该函数的关键字参数转发。

Finally, you should take out the extra comma in the arguments you use to run the script. 最后,您应该在用于运行脚本的参数中删除多余的逗号。 (You could write code to strip it, but it seems like it's just a typo, so the better fix is to not make that typo.) (您可以编写代码来剥离它,但看来这只是一个错字,所以更好的解决方法是不要做那个错字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM