Index: /TOOLS/MOSAIX/update_xml.py
===================================================================
--- /TOOLS/MOSAIX/update_xml.py	(revision 5915)
+++ /TOOLS/MOSAIX/update_xml.py	(revision 5916)
@@ -1,2 +1,3 @@
+#!/usr/bin/env python
 ### ===========================================================================
 ###
@@ -25,25 +26,5 @@
 #
 import xml.etree.ElementTree
-import getopt, sys
-
-##
-def usage () :
-    texte = """%(prog)s usage :
-python %(prog)s [-d] [-i iodef.xml] [-o iodef_new.xml] -n <node in Xpath syntax> -k <key>  -v <value>
-python %(prog)s [-d] [-i iodef.xml] [-o iodef_new.xml] -n <node in Xpath syntax> -t <value>
- -d         | --debug         : debug
- -i <file>  | --input=<file>  : input file  (default: iodef.xml)
- -o <file>  | --output=<file> : output file (default: overwrite input file)
- -n <node>  | --node=<node>   : node in Xpath syntax
- -f <field> | --field=<field> : xml field to update
- -v <value> | --value=<value> : new value for xml field
- -t <text>  | --text=<text>   : will replace the 'text' part of the Xpath by <text>
-examples :  
-    python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T
-    python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t ${dstDomainType}
-    """
-    #print ( texte % ( sys.argv[0], sys.argv[0], sys.argv[0], sys.argv[0] ) )
-    #print ( texte %   ( 6*[sys.argv[0]] ))
-    print ( texte % { 'prog':sys.argv[0] } )
+import argparse, sys, textwrap
     
 # Check version of Python
@@ -54,61 +35,47 @@
   sys.exit (1)
   
-## Default input parameters
-FileIn   = 'iodef.xml'
-FileOut  = None
-Node     = None 
-Key      = None
-Text     = None
-Value    = None
-Debug    = False
+# Creating a parser to read the command line arguments
+# The first step in using the argparse is creating an ArgumentParser object:
+parser = argparse.ArgumentParser (description = """
+examples : 
+     python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T
+     python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]'   -t dstDomainType
+     python %(prog)s -i iodef.xml -e commands.txt (not implemented yet)
+   """ + "\n" + "SVN : " + __Revision__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog='-------- This is the end of the help message --------')
 
-## Command line options
-try:
-    myopts, myargs = getopt.getopt ( sys.argv[1:], 'i:o:n:k:v:t:dh', [ 'input=', 'output=', 'node=', 'key=', 'value=', 'text=', 'debug=', '--help' ] )
-except getopt.GetoptError as cmdle :
-    print ( "Command line error : "+cmdle )
-    usage ()
-    sys.exit(1)
+# Adding arguments
+group1 = parser.add_mutually_exclusive_group (required=True)
 
-for myopt, myval in myopts :
-    if myopt in [ '-h', '--help'   ] : usage () ; sys.exit (0) ; 
-    if myopt in [ '-i', '--input'  ] : FileIn   = myval
-    if myopt in [ '-o', '--output' ] : FileOut  = myval
-    if myopt in [ '-n', '--node'   ] : Node     = myval
-    if myopt in [ '-k', '--key'    ] : Key      = myval
-    if myopt in [ '-t', '--text'   ] : Text     = myval
-    if myopt in [ '-v', '--value'  ] : Value    = myval
-    if myopt in [ '-d', '--debug'  ] : Debug    = True
+parser.add_argument ( '-i', '--input'  , help="input file"            , default='iodef.xml', type=str, metavar='<input_file>' )
+parser.add_argument ( '-o', '--output' , help="output file"           , default=None       , type=str, metavar='<output_file>'  )
+parser.add_argument ( '-n', '--node'   , help="xml node in Xpath syntax", default=None, required=True, type=str, metavar='<xml_node>')
+group1.add_argument ( '-k', '--key'    , help="xml key to update"     , default=None , type=str , metavar='<xml_key>' )
+group1.add_argument ( '-t', '--text'   , help="will replace the 'text' part of the Xpath by <text>", default=None, type=str, metavar='<text>' )
+parser.add_argument ( '-v', '--value'  , help="new value for xml key", default=None, type=str, metavar='<value>' )
+parser.add_argument ( '-d', '--debug'  , action="store_true", default=False )
+parser.add_argument ( '-V', '--verbose', action="store_true", default=False )
 
-## Some coherency checking of command line parameters
-ErrorCount = 0
+# Parse command line
+myargs = parser.parse_args()
+Verbose  = myargs.verbose
 
-if FileIn == None :
-    print ( "Error : please specify input file by -i <file>" )
-    ErrorCount += 1
+if Verbose : print ( "Command line arguments : " , myargs )
 
-if Node == None :
-    print ( "Error : please specify -n <node>" )
-    ErrorCount += 1
-    
-if Key == None and Text == None :
-    print ( "Error : please specify either -t <text> or -k <key> -v <value>" )
-    ErrorCount += 1
+FileIn   = myargs.input
+FileOut  = myargs.output
+Node     = myargs.node
+Key      = myargs.key
+Text     = myargs.text
+Value    = myargs.value
+Debug    = myargs.debug
 
-if Key != None and Text != None :
-    print ( "Error : please specify only one option between -t "+Text+" and -k "+Key )
-    ErrorCount += 1
-
+# Error handling not dealed by argparse
 if Key != None and Value == None :
-    print ( "Error : please specify -v <value> when -k "+Key+" is given")
-    ErrorCount += 1
- 
-if ErrorCount > 0 :
-    usage ()
-    sys.exit (1)
+    print ( "Error. When -k|--key=<key> is specified, you must specify -v|--value=<xml_value>" )
+    sys.exit -1)
 
 if FileOut == None : FileOut = FileIn
 
-## Remove white spaces at beginning and end of line
+# Remove whitespaces at both ends
 Node = Node.rstrip().lstrip()
 
@@ -120,13 +87,13 @@
 
 ## Check that one and only one node is found
-if len(nodeList) == 0 :
+if len (nodeList) == 0 :
     print ( "Error : node not found" )
-    print ( "Node  : "+Node )
+    print ( "Node  : " + Node )
     sys.exit (1)
     
-if len(nodeList) > 1 :
-    print ( "Error : "+len(nodeList)+" occurences of node found" )
-    print ( "Node  : "+Node )
-    sys.exit (1)
+if len (nodeList) > 1 :
+    print ( "Error : " + len (nodeList)+" occurences of node found" )
+    print ( "Node  : " + Node )
+    sys.exit (2)
 
 ## Update element
@@ -134,12 +101,12 @@
 
 if Debug :
-    print ( 'Node  : '+Node  )
-    print ( 'Key   : '+Key   )
-    print ( 'Value : '+Value )
+    print ( 'Node  : ' + Node  )
+    print ( 'Key   : ' + Key   )
+    print ( 'Value : ' + Value )
 
 if Text != None :
     if Debug :
-        print ( 'Attributes of node: '+str(elem.attrib) )
-        print ( 'Text              : '+str(elem.text)   )
+        print ( 'Attributes of node: ' + str (elem.attrib) )
+        print ( 'Text              : ' + str (elem.text)   )
     elem.text = Text
 
@@ -147,8 +114,7 @@
     # To do : check that Key exist (it is added if not : do we want that ?)
     if Debug :
-        print ( 'Attributes of node: '+str(elem.attrib) )
+        print ( 'Attributes of node: ' + str (elem.attrib) )
     elem.attrib.update ( { Key:Value } )
     
-
 ## Writes XML tree to file
 iodef.write ( FileOut )
