2017-04-07 45 views
2

试图让我的脚本更通用,所以我添加了一些标志。我的问题是,只有当你输入-h时,该帮助才有效。我想在没有选择标志时调用-h。python脚本envoke -h或--help如果没有选项

例如:

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 
+0

你解析你的选择如何?如果您仍在使用['optparse'](https://docs.python.org/2/library/optparse.html):切换到(不推荐使用)[argparse](https://docs.python .org/2.7/library/argparse.html)和[覆盖'.error()'](http://stackoverflow.com/questions/3636967/python-argparse-how-can-i-display-help-automatically-在错误)。 – dhke

+0

@dhke刚刚被认为是argparse – chowpay

回答

0

不知道你用解析方法,我将承担以下(评论我,如果我错了,或者你如何处理你的解析一些代码编辑你的问题):

  1. 你是解析所有内容并将其放入一个变量中。让parsed是那个变量。
  2. 您正在检查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 
+0

耶看起来像我正在使用optionparser,因为我问dhke。我需要改变什么来切换?谢谢 – chowpay

+0

并不多,请阅读这些示例https:// docs。python.org/2/library/argparse.html#example –

+0

谢谢!确定切换了它(请参阅我的编辑)我现在将添加您的代码 – chowpay

2

从这里借:Argparse: Check if any arguments have been passed

下面是最终代码的样子:

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 
1

如果有人仍然对(非常简单)解决方案感兴趣:

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/--help页面。而不是像“python SCRIPT.py -h”。 所以我在这里做的就是:“尝试解析参数,如果有效,将它们返回到主方法,否则,如果你失败(除了),打印帮助(),好吗?好的。 ;)

+0

哦,在“parser.print_help()”之后应该有一个“exit()”。否则,该脚本将生成 - 除了butiful帮助页面 - 不同的错误消息(因为他正在尝试继续工作)。 – Julianos