R2spec-2.3
By Pierre-Yves on Sunday, August 10 2008, 19:53 - Général - Permalink
Small update with some code improvement in arguments management
Petite mise à jour avec quelques optiomisation du code dans la gestion des arguments
English version (Français ci-dessous)
I have made few changes on my project R2spec, changing the arguments/options management from getopt to OptionParser. It works nicely and save quite some code, there is the two versions:
First version
def getArgument(): try: opts, args = getopt.getopt(sys.argv1:, "u:s:", "url=", "source=") except getopt.GetoptError:
print USAGE sys.exit(1)
url = None source = None
for option, argument in opts: if option in ("-u", "--url"): url = argument if option in ("-s", "--source"): source = argument
return (source,url)
New version
def getArgument(): # Handle the parameters parser = OptionParser(version="%prog 2.3") parser.add_option("-u", "--url", dest="url", help="Url of the R library") parser.add_option("-s", "--source", dest="source", help="The source file (tarball) of the R library") (options, args) = parser.parse_args() return (options.source, options.url)
As you can see it improves quite the code !
In addition, it handles the --help
and the --version
by itself nothing to do !! \o/ :-p
Tester and feed back are always welcome ! :-)
The new version are available there:
French version
Je me suis occupé un petit peu ce week end et est fait quelques changements dans mon projet R2spec. J'ai notamment changé la gestion des arguments donnés du module getopt à OptionParser. Ça marche pas trop mal et améliore bien le code. Voici les deux version pour comparaison:
Première version
def getArgument(): try: opts, args = getopt.getopt(sys.argv1:, "u:s:", "url=", "source=") except getopt.GetoptError:
print USAGE sys.exit(1)
url = None source = None
for option, argument in opts: if option in ("-u", "--url"): url = argument if option in ("-s", "--source"): source = argument
return (source,url)
Nouvelle version
def getArgument(): # Handle the parameters parser = OptionParser(version="%prog 2.3") parser.add_option("-u", "--url", dest="url", help="Url of the R library") parser.add_option("-s", "--source", dest="source", help="The source file (tarball) of the R library") (options, args) = parser.parse_args() return (options.source, options.url)
Comme vous pouvez voir le code est pas mal améliorer !
En plus, plus de problème pour la création du --help
ou du --version
, ils sont crée directement, rien à faire !! \o/ :-p
Testeurs et commentaires sont les bienvenus :-)
Les nouvelles versions sont disponible ici: