[mythtv-users] User Jobs Problems Processing Command Line Options in Python

Ian Barton lists at manor-farm.org
Wed Oct 1 13:12:56 UTC 2008


Ian Barton wrote:
> I am writing a user job script in Python. This works correctly when run
> from a terminal. However, as a user job the Python getopt library seems
> to be throwing a wobbly.
> 
> Sample code (write_log_entry writes debugging stuff to a text file):
> 
>    try:
> 
>         opts, args = getopt.getopt(sys.argv[1:], "hfvcs:v", ["help",
> "filename=", "video-dir=", "channel-id=", "start-time="])
>         write_log_entry("Before opt")
>         for opt, arg in opts:
>             write_log_entry("arg" + arg)
>             if opt in ("-h", "--help"):
>                 write_log_entry("Exit.")
>                 useage()
>                 sys.exit()
>             elif opt in ("-f", "--filename"):
>                 filename = arg
>             elif opt in ("-v", "--video-dir"):
>                 videodir = arg
>             elif opt in ("-c", "--channel-id"):
>                 channel = arg
>             elif opt in ("-s", "--start-time"):
>                 starttime = arg
>     except getopt.GetoptError, err:
>         write_log_entry("Error parsing command line: " + str(err))
>         print str(err)
>         sys.exit(2)
> 
> My user job is:
> 
> /usr/local/bin/test.py -f %FILE% -v %DIR%  -c %CHANID%  -s "%STARTTIME%"
> 
> Sample output from the debug file:
> 
> 2008-10-01 11:46:05: Before opt
> 2008-10-01 11:46:05: arg%FILENAME%
> 2008-10-01 11:46:05: arg
> 2008-10-01 11:46:05: After for opt
> 
> Python is only seeing one argument, not four and %FILE% isn't being
> expanded into a file name. I presume that sys.argv doesn't contain what
> I think it does. Anyone got any suggestions?
> 

It does seem to be a problem with getopt, as the following prints the
command line options as expected:

 for arg in sys.argv:
        write_log_entry(arg)

Ian.


More information about the mythtv-users mailing list