How to use OptionParser from Python module optparse

Asked by Jonathan Antognini

i try use my own script help parameters

for example:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", action="store_false", dest="option", default=True, help="Up system")
(options, args) = parser.parse_args()
print sys.argv[1:]

but if y try use parameters -h, return me
Missing argument for option: args
Exception in thread "main" java.lang.NullPointerException
 at org.sikuli.ide.SikuliIDE.main(SikuliIDE.java:699)

any way to solve this problem?

thank you very much =)

Question information

Language:
English Edit question
Status:
Expired
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Jonathan Antognini (jantogni) said :
#1

i try execute somethings like this:

sh sikuli-ide.sh -r test.sikuli -args -h

when i try execute this line, in command line, send me a error showed before.

i use ubuntu 10.10, and sikuli x rc1 version.

Revision history for this message
Jonathan Antognini (jantogni) said :
#2

also try execute like this

java -jar sikuli-script.jar /home/jantogni/Test/sikuli/runOMC.sikuli -h

and show me the options menu, also show me this exception

Exception in thread "MainThread" Traceback (most recent call last):
  File "/home/jantogni/Test/sikuli/runOMC.sikuli/runOMC.py", line 693, in <module>
    main(sys.argv)
  File "/home/jantogni/Test/sikuli/runOMC.sikuli/runOMC.py", line 650, in main
    (options, arguments) = parser.parse_args()
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 1387, in parse_args
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 1387, in parse_args
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 1431, in _process_args
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 1538, in _process_short_opts
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 773, in process
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 797, in take_action
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/optparse.py", line 1561, in exit

Revision history for this message
RaiMan (raimund-hocke) said :
#3

--1. this will never work in IDE, since in IDE no args are given to the script. So your script can only from command line (use any defined method, that gives the args through to your script)

--2. OptionParser throws an exception, when an option is given, that is not defined by you (this is the case for -h, even if this yields a default action)
so you have to script like that:
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", action="store_false", dest="option", default=True, help="Up system")
try:
 (options, args) = parser.parse_args()
except:
 print "option exception"
print sys.argv[1:]

in the except block you might implement corrective actions (or just exit(), will be silent again with rc2)

Revision history for this message
Jonathan Antognini (jantogni) said :
#4

thanks very much,
now send me only this error:

Exception in thread "MainThread" Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jantogni/Documentos/automatic_test/sikuli/Sikuli-X-1.0rc1-linux/Sikuli-IDE/sikuli-script.jar/Lib/sikuli/Sikuli.py", line 153, in exit
SystemExit: 0

how i can solve this problem?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

not really an error (unnecessary Java exception when script ends), you have to live with it until rc2.

Revision history for this message
Jonathan Antognini (jantogni) said :
#6

hi Raiman, i download the sikuli x 1.0rc2 and test this problem,

and now the exception showed is the next:

Exception in thread "MainThread" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 at java.lang.String.substring(String.java:1937)
 at org.sikuli.script.ScriptRunner.getPyFrom(ScriptRunner.java:97)
 at org.sikuli.script.ScriptRunner.runPython(ScriptRunner.java:91)
 at org.sikuli.script.SikuliScript.main(SikuliScript.java:102)

regards

Revision history for this message
RaiMan (raimund-hocke) said :
#7

Is Sikuli-IDE running?

The above error happens, if sikuli-script.jar is run with a not exisiting .sikuli or another not supported filename.

How do you use sikuli-script.jar?

Revision history for this message
Jonathan Antognini (jantogni) said :
#8

sikuli ide is not running.

i use sikuli-script.jar like this:

sudo java -jar sikuli-script.jar /home/user/test.sikuli -h

that show me a help display. but send me this error.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

Only a question: why do you need sudo to run java? (has nothing to do with your problem though)

accidentally I just completed to install Ubuntu 10.10 64-Bit in VirtualBox on my MacPro :-))

Got Java 64-Bit (installed with UbuntuSoftwareCenter-CanonicalPartners) and OpenCV (using apt-get).

using sikuli-ide.sh: Sikuli-IDE starts up and works without problems (click actions not working in VirtualBox).

java -jar sikuli-script.jar test.sikuli (all in the Sikuli-IDE folder for this test) works without problems too.

So I think you have to revise your installation.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

Update (if anyone reads the above comment):

configuration: Sikuli X-1.0rc2 (zipped 64Bit) on Ubuntu 10.10 64Bit (Java 64Bit) running in Virtual Box 4.02 on Mac OS X 10.6

--- click actions work,
but the mouse cursor is not moved to the click location (slow motion shows it yet with the blinking red circles).

Revision history for this message
Jonathan Antognini (jantogni) said :
#11

i use sudo because habit of rc:
when i try (in the rc1) take a screenshot, the framework was closing give this problem:

Error: Illegal min or max specification!

Fatal error: No error trap defined!
Signal_termination_handler called with signal 5002

In the rc2 i haven't problem with this :P.

Revision history for this message
RaiMan (raimund-hocke) said :
#12

Sorry to say: your installation seems to have some odds - you should never be forced to use sudo to run the java command.

So if I understand right, RC2 is running.

If yes: Have fun.

Revision history for this message
Jonathan Antognini (jantogni) said :
#13

yes, RC2 is running.

about the problem? any idea to solve?

Revision history for this message
RaiMan (raimund-hocke) said :
#14

why talk about problems with rc1 when we have rc2 now?

Revision history for this message
Jonathan Antognini (jantogni) said :
#15

this exception is in RC2

Exception in thread "MainThread" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 at java.lang.String.substring(String.java:1937)
 at org.sikuli.script.ScriptRunner.getPyFrom(ScriptRunner.java:97)
 at org.sikuli.script.ScriptRunner.runPython(ScriptRunner.java:91)
 at org.sikuli.script.SikuliScript.main(SikuliScript.java:102)

Revision history for this message
RaiMan (raimund-hocke) said :
#16

in a command like this:

java -jar sikuli-script.jar /home/user/test.sikuli

test.sikuli does not exist or cannot be found with the path or does not contain a test.py

Revision history for this message
Jonathan Antognini (jantogni) said :
#17

the problem is the next, i use option parse, and set in line command:

java -jar sikuli-script.jar /home/user/test.sikuli -h

and show me the option set in the /home/user/test.sikuli/test.py

Revision history for this message
Launchpad Janitor (janitor) said :
#18

This question was expired because it remained in the 'Open' state without activity for the last 15 days.