2017-04-07 29 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

相關問題