私は、アルゴリズムに関するいくつかの基本的な知識を持ち、最初のプログラミング言語としてPythonを学ぶための真剣な試みを始めました。私がリポジトリを管理するための小さなスクリプトを作成することを決めたのは、何か役に立つものを見つけることである。python - 初心者 - プログラム内のoptparseの統合
基本的な事柄: - 有効/無効YUMリポジトリ - 現在のYUMリポジトリ 上の優先順位の変更 - 追加/削除リポジトリ
ファイルを解析中と置換/データは非常に簡単です追加/削除、私はよ'optparse'を使って一つのことを(主に多分知識の不足で)苦労しています...現在利用可能なリポジトリをリストするオプション(-l)に追加したいのですが...これを行う簡単な関数を作った仕事(非常に精巧なものではない)がありますが、optparseの '-l'と '接続'できません。誰でもこれを作る方法の例/提案を提供することができますか?
現在のスクリプトは、このようなものである:(ドキュメント、例)を改善するための任意の提案は大歓迎です
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import optparse
import ConfigParse
repo_file = "/home/nmarques/my_repos.repo"
parser = optparse.OptionParser()
parser.add_option("-e", dest="repository", help="Enable YUM repository")
parser.add_option("-d", dest="repository", help="Disable YUM repository")
parser.add_option("-l", dest="list", help="Display list of repositories", action="store_true")
(options, args) = parser.parse_args()
def list_my_repos()
# check if repository file exists, read repositories, print and exit
if os.path.exists(repo_file):
config = ConfigParser.RawConfigParser()
config.read(repo_file)
print "Found the following YUM repositories on " + os.path.basename(repo_file) + ":"
for i in config.sections():
print i
sys.exit(0)
# otherwise exit with code 4
else:
print "Main repository configuration (" + repo_file +") file not found!"
sys.exit(4)
list_my_repos()
。もう一度主な目標は、script.py -l
を実行するとlist_my_repos()
を実行できるということです。
可能であれば、 'argparse'を使うべきです。' optparseモジュールは廃止され、さらに開発されることはありません。 argparseモジュールを使用して開発を続けます」。 – Daenyth