Pythonコマンドの引数は、getoptのモジュールによって行わ取得する方法を詳細に説明しました

この記事では、主に、コマンドパラメータがgetoptのモジュールによって行わ取得する方法のpythonの関連情報を紹介するために、紙のサンプルコードでは、非常に詳細に説明され、習得したり、作業するために私たちのすべてのための学習の一定の基準値を持って、我々は以下の友達を必要とします小さなシリーズと一緒にそれを学ぶ学びます。
序文

Pythonスクリプトとシェルスクリプトが異なるパラメータに応じて、コマンドラインパラメータとして取得してもよいし、異なるロジックを実行します。

通常、我々はのgetoptモジュールの実装により異なるコマンドとパラメータを取得することができます。以下は、詳細な紹介を見てみるために、言ってあまり話をしませんでした。
ここに方法は次のとおりです。

私は新しいスクリプトtest.pyを作成するには、このモジュールを使用して説明しましょう

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import getopt
if __name__=='__main__':
 print sys.argv
 opts, args = getopt.getopt(sys.argv[1:], "ht:q:", ["url=",'out'])
 print opts
 print args

エグゼクティブ順:./test3.py -t 20171010-20171011 -q 24 -h --url=https://www.baidu.com --out file1 file2
結果:

['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com', '--out', 'file1', 'file2']
[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']

私たちは、公式文書のgetoptのモジュールを参照してください

def getopt(args, shortopts, longopts = [])
 
Parses command line options and parameter list. args is the
argument list to be parsed, without the leading reference to the
running program. Typically, this means "sys.argv[1:]". shortopts
is the string of option letters that the script wants to
recognize, with options that require an argument followed by a
colon (i.e., the same format that Unix getopt() uses). If
specified, longopts is a list of strings with the names of the
long options which should be supported. The leading '--'
characters should not be included in the option name. Options
which require an argument should be followed by an equal sign
('=').
 
The return value consists of two elements: the first is a list of
(option, value) pairs; the second is the list of program arguments
left after the option list was stripped (this is a trailing slice
of the first argument). Each option-and-value pair returned has
the option as its first element, prefixed with a hyphen (e.g.,
'-x'), and the option argument as its second element, or an empty
string if the option has no argument. The options occur in the
list in the same order in which they were found, thus allowing
multiple occurrences. Long and short options may be mixed.

getoptの方法は3つのパラメータを必要としています。

最初のパラメータは、私たちがsys.argvので行っ関連するパラメータを取得することができ、解析のコマンドライン引数に起こっている引数です

['D:/GitReposity/hope_crontab_repo/sla_channel/test3.py', '-t', '20171010-20171011', '-q', '24', '-h', '--url=https://www.baidu.com']

パラメータリストの最初の値を見ることができ、残りのパラメータがスペース区切りのコマンドラインパラメータで、スクリプトの実行の完全なパス名です。有効なパラメータを得るために、パラメータ値は通常引数sys.argvの[1]をとっています。

考えQHT上記の例のように、シンボルの先頭にパラメータ - - 初めに、スクリプトのようQHTコマンドが短く、短いコマンド第2のパラメータは、彼のコマンドライン引数を含むように、短いコマンドshortopts演算子でありますそれはそれのパラメータを一致させる方法ですか?、コマンドが続いた後にここで使用される「:Q HT」の例としてshotoptsに見ることができるコマンド、パラメータ、引数なしここで、H、T及びQの必要パラメータを必要とするかどうかを宣言するために、コマンド・ラインは、TおよびQ続い彼らは、パラメータqコマンドパラメータが24であり、すなわち、コマンドパラメータtが20171010から20171011で、コマンドパラメータです。

3番目のパラメータは、変更パラメータが配列され、longoptsのある演算子のセット長いコマンドを表しています。このセットはで起動するコマンドライン含まれています - サインパラメータ、URLと出ている長いコマンドを、長い終わりがあるときに、コマンドが、彼は、このような「= URL」として、パラメータを必要と=の後、彼は次のコマンドラインを一致させますパラメータhttps://www.baidu.com。

この方法は、2つの要素の配列を返します。最初の値を返し、shortopts longoptsのマッチングタプルとそのコマンドライン引数によって。戻り値の例:

[('-t', '20171010-20171011'), ('-q', '24'), ('-h', ''), ('--url', 'https://www.baidu.com'), ('--out', '')]
['file1', 'file2']

第2の戻り値は、この例のコマンドラインパラメータ、戻り値に一致しません。

['file1', 'file2']

私たちは、独自のコードでできる値を戻すことにより、異なるロジック、スクリプトの豊富な可用性を設計するために依存します。

913066266、高齢者が学んでいる方法を見て:私たちは、Pythonのバックルqunを学ぶお勧めします!基本的なWeb開発Pythonスクリプトからなど、爬虫類、ジャンゴ、データマイニング、へ[PDF、実際のソースコード]、ゼロベースのプロジェクトの戦闘データに仕上げています。どんな小さなPythonのパートナーに与えられました!毎日、ダニエルは学び、細部に注意を払う必要性に、クリックにご参加くださいする方法のいくつかを共有するために、タイミングPythonの技術を説明する学習者集会のPython

公開された47元の記事 ウォン称賛53 ビュー50000 +

おすすめ

転載: blog.csdn.net/haoxun03/article/details/104270451