为了使我的脚本更通用,所以我添加了一些标志。显然,我的问题是,只有在输入-h时,help才能工作。当未选择标志时,我希望启用-h。
例如:
python 0_log_cleaner.py
Traceback (most recent call last):
File "0_log_cleaner.py", line 51, in <module>
getFiles(options.path,options.org_phrase,options.new_phrase,options.org_AN,options.new_AN,options.dst_path)
File "0_log_cleaner.py", line 37, in getFiles
for filename in os.listdir(path):
TypeError: coercing to Unicode: need string or buffer, NoneType found但是如果我加上-h,我得到:
python 0_log_cleaner.py -h用法:示例:
python 0_log_cleaner.py --sp original_logs/ --dp clean_logs/ --od CNAME --nd New_CNAME --oan 10208 --nan NewAN
Options:
-h, --help show this help message and exit
--sp=PATH Path to the source logs ie original_logs/
--dp=DST_PATH Path to where sanitized logs will be written to ie
clean_logs
--od=ORG_PHRASE original domain name ie www.clientName.com, use the command
-od clientName
--nd=NEW_PHRASE domain name to replace -od. ie -od clientName -nd domain
makes all log that use to be www.clientName.com into
www.domain.com
--oan=ORG_AN original AN number
--nan=NEW_AN AN number to replace original. ie -oan 12345 -nan AAAA1
replaces all instances of the AN number 12345 with AAAA1编辑3回答我的代码的示例以生成^
import argparse
import sys
usage = "Description of function"
parser = argparse.ArgumentParser(description=usage)
parser.add_argument("--sp", dest="path", help='Path to the source logs ie logs/')
...
...(additional add arugments)
args = parser.parse_args()
def getFiles(path,org_phrase,new_phrase,org_AN,new_AN,dst_path):
if not len(sys.argv) > 1:
parser.print_help()
else:
run your logic发布于 2017-04-07 22:18:50
在不知道您使用的解析方法的情况下,我将假设如下(如果我错了,请注释我,或者用一些代码编辑您的问题,说明您如何处理解析):
parsed成为那个变量。parsed是否存在任何选项标志。您可能没有检查参数的不存在:
parsed = '' <- empty string
# or if you are using a list:
# parsed = []
if parsed: <- if parsed is not empty ("" or []) returns true
Do your stuff here, because you have options now
else: <- Differently options were not provided
Invoke the same method that you invoke when the option is -h同样,正如@dhke建议的那样,如果您还没有使用argparse解析,请考虑使用它!
编辑#1:为特定情况翻译的:
args = parser.parse_args() <-- ending line of your provided code
if not args:
parser.print_help()
else:
Do your stuff发布于 2017-04-08 02:53:28
从这里借来:检查是否传递了任何参数
最后的代码如下所示:
import argparse
import sys
usage = "Description of function"
parser = argparse.ArgumentParser(description=usage)
parser.add_argument("--sp", dest="path", help='Path to the source logs ie logs/')
...
...(additional add arugments)
args = parser.parse_args()
def getFiles(path,org_phrase,new_phrase,org_AN,new_AN,dst_path):
if not len(sys.argv) > 1:
parser.print_help()
else:
run your logic发布于 2017-12-13 21:01:14
如果有人仍然对一个(非常简单的)解决方案感兴趣:
parser = argparse.ArgumentParser()
parser.add_argument("jfile", type=str, help="Give the JSON file name.")
parser.add_argument("--output", type=str, help="Type in the final excel files name.")
try:
args = parser.parse_args()
return args
except:
parser.print_help()我的教授希望脚本能强制使用-h /-帮助页面,即使争论太少。而不是像"python SCRIPT.py -h“那样。所以我在这里所做的是:“尝试解析这些参数,如果它有效,将它们返回到主方法。否则,如果失败(除了),就打印help()。好吗?很好”。;)
https://stackoverflow.com/questions/43287664
复制相似问题