[GRASS-dev] Re: [GRASSGUI] silly question - running a python script

Glynn Clements glynn at gclements.plus.com
Sun Jun 10 16:28:43 EDT 2007


Michael Barton wrote:

> Here is what is currently there...
> 
>  if __name__ == "__main__":
>      args = ""
>      for arg in sys.argv:
>          args += arg+" "
> 
>      try:
>          if ( sys.argv[1] != "@ARGS_PARSED@" ):
>              os.system("g.parser %s " % (args))
>      except IndexError:
>          os.system("g.parser %s" % (args))
> 
>      if sys.argv[1] == "@ARGS_PARSED@":
>          main();
> 
> I found an old email of yours I'd been saving (and couldn't initially find)
> that covers part of what needs to be done. Following that, I think it would
> change to...
> 
>  if __name__ == "__main__":
> #     No need to make a string here, right?
> #     args = ""
> #     for arg in sys.argv:
> #         args += arg+" "
> 
>      try:
>          if ( sys.argv[1] != "@ARGS_PARSED@" ):
>              os.execv("g.parser", [sys.argv[0]] + sys.argv)
>      except IndexError:
>          os.execv("g.parser", [sys.argv[0]] + sys.argv)
> 
>      if sys.argv[1] == "@ARGS_PARSED@":
>          main();
> 
> Is this correct or is it still missing something?

That looks about right.

Although the "try ... except IndexError" can be replaced with a length
check on sys.argv, i.e.:

    if __name__ == "__main__":
        if ( len(sys.argv) > 1 && sys.argv[1] != "@ARGS_PARSED@" ):
            os.execv("g.parser", [sys.argv[0]] + sys.argv)
	else:
            main()

-- 
Glynn Clements <glynn at gclements.plus.com>




More information about the grass-dev mailing list