アルゴリズムに関する基本的な知識を備えた最初のプログラミング言語として、Python を学ぶ真剣な試みを始めました。始めるには何か役に立つことを見つけるのが最善の方法だと誰もが勧めているので、リポジトリを管理するための小さなスクリプトを作成することにしました。
基本事項: - YUM リポジトリの有効化/無効化 - 現在の YUM リポジトリの優先度の変更 - リポジトリの追加/削除
ファイルの解析とデータの置換/追加/削除は非常に簡単ですが、「optparse」を使用して1つのことで苦労しています(主に知識不足で)...オプション(-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()
です。